-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Format interface from the console sink
- Loading branch information
Showing
3 changed files
with
52 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** A parsed format */ | ||
export interface Format { | ||
/** The original format string */ | ||
format: string; | ||
/** The parsed string */ | ||
output: string; | ||
/** A map of raw variables from the string */ | ||
variables: Map<string, unknown>; | ||
} | ||
|
||
/** Parse a string format with variables */ | ||
export const parseFormat = (format: string, ...args: unknown[]): Format => { | ||
const variables = new Map<string, unknown>(); | ||
|
||
const output = format.replace(/\{(.*?)\}/g, (_, p1: string) => { | ||
const arg = args.shift(); | ||
variables.set(p1, arg); | ||
return `${arg}` || ""; | ||
}); | ||
|
||
return { | ||
format, | ||
output, | ||
variables, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,45 +7,31 @@ import { | |
red, | ||
bold, | ||
} from "https://deno.land/[email protected]/fmt/colors.ts"; | ||
import { Format } from "./format.ts"; | ||
import { LogLevel } from "./levels.ts"; | ||
|
||
/** An output for the logger, sometimes known as a handler */ | ||
export type Sink = ( | ||
level: LogLevel, | ||
format: string, | ||
...args: unknown[] | ||
format: Format, | ||
) => void; | ||
|
||
/** The default console sink */ | ||
export const consoleSink: Sink = ( | ||
level: LogLevel, | ||
format: string, | ||
...args: unknown[] | ||
format: Format, | ||
) => { | ||
const message = format.replace(/\{.*?\}/g, () => `${args.shift()}` || ""); | ||
const timestamp = `[${new Date().toISOString()} ${LogLevel[level]}]`; | ||
const message = `[${new Date().toISOString()} ${ | ||
LogLevel[level] | ||
}] ${format.output}`; | ||
|
||
let color; | ||
switch (level) { | ||
case LogLevel.DEBUG: | ||
color = gray; | ||
break; | ||
case LogLevel.INFO: | ||
color = white; | ||
break; | ||
case LogLevel.WARNING: | ||
color = yellow; | ||
break; | ||
case LogLevel.ERROR: | ||
color = red; | ||
break; | ||
case LogLevel.CRITICAL: | ||
color = (str: string) => bold(red(str)); | ||
break; | ||
default: | ||
color = white; | ||
break; | ||
} | ||
const color = ({ | ||
[LogLevel.DEBUG]: gray, | ||
[LogLevel.INFO]: white, | ||
[LogLevel.WARNING]: yellow, | ||
[LogLevel.ERROR]: red, | ||
[LogLevel.CRITICAL]: (str: string) => bold(red(str)), | ||
})[level] ?? white; | ||
|
||
console.log(color(`${timestamp} ${message}`)); | ||
console.log(color(message)); | ||
}; |