Skip to content

Commit 23b769a

Browse files
committed
Extract Format interface from the console sink
1 parent 64118c6 commit 23b769a

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

format.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** A parsed format */
2+
export interface Format {
3+
/** The original format string */
4+
format: string;
5+
/** The parsed string */
6+
output: string;
7+
/** A map of raw variables from the string */
8+
variables: Map<string, unknown>;
9+
}
10+
11+
/** Parse a string format with variables */
12+
export const parseFormat = (format: string, ...args: unknown[]): Format => {
13+
const variables = new Map<string, unknown>();
14+
15+
const output = format.replace(/\{(.*?)\}/g, (_, p1: string) => {
16+
const arg = args.shift();
17+
variables.set(p1, arg);
18+
return `${arg}` || "";
19+
});
20+
21+
return {
22+
format,
23+
output,
24+
variables,
25+
};
26+
};

logger.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2020 Yamboy1. All rights reserved. MIT license.
22

3+
import { parseFormat } from "./format.ts";
34
import { LogLevel } from "./levels.ts";
45
import { Sink, consoleSink } from "./sinks.ts";
56

@@ -13,6 +14,8 @@ export function createLogger(
1314
});
1415
}
1516

17+
type LogFunction = (format: string, ...args: unknown[]) => void;
18+
1619
export interface LoggerOptions {
1720
minimumLevel: LogLevel;
1821
sinks: Sink[];
@@ -22,11 +25,11 @@ export interface Logger {
2225
/** Add a sink at runtime, in most cases, the sinks parameter from createLogger should be used instead. */
2326
addSink(sink: Sink): this;
2427

25-
debug(format: string, ...args: unknown[]): void;
26-
info(format: string, ...args: unknown[]): void;
27-
warning(format: string, ...args: unknown[]): void;
28-
error(format: string, ...args: unknown[]): void;
29-
critical(format: string, ...args: unknown[]): void;
28+
debug: LogFunction;
29+
info: LogFunction;
30+
warning: LogFunction;
31+
error: LogFunction;
32+
critical: LogFunction;
3033
}
3134

3235
class LoggerImpl {
@@ -43,17 +46,19 @@ class LoggerImpl {
4346
return this;
4447
}
4548

46-
private log(level: LogLevel, format: string, ...args: unknown[]): void {
49+
private log(level: LogLevel, formatString: string, ...args: unknown[]): void {
4750
if (level < this.minimumLevel) return;
4851

52+
const format = parseFormat(formatString, ...args);
53+
4954
if (this.sinks.length === 0) {
5055
console.error(
5156
"[Deno Structured Logging] Warning: No sinks are provided.",
5257
);
5358
}
5459

5560
for (let sink of this.sinks) {
56-
sink(level, format, ...args);
61+
sink(level, format);
5762
}
5863
}
5964

sinks.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,31 @@ import {
77
red,
88
bold,
99
} from "https://deno.land/std@0.51.0/fmt/colors.ts";
10+
import { Format } from "./format.ts";
1011
import { LogLevel } from "./levels.ts";
1112

1213
/** An output for the logger, sometimes known as a handler */
1314
export type Sink = (
1415
level: LogLevel,
15-
format: string,
16-
...args: unknown[]
16+
format: Format,
1717
) => void;
1818

1919
/** The default console sink */
2020
export const consoleSink: Sink = (
2121
level: LogLevel,
22-
format: string,
23-
...args: unknown[]
22+
format: Format,
2423
) => {
25-
const message = format.replace(/\{.*?\}/g, () => `${args.shift()}` || "");
26-
const timestamp = `[${new Date().toISOString()} ${LogLevel[level]}]`;
24+
const message = `[${new Date().toISOString()} ${
25+
LogLevel[level]
26+
}] ${format.output}`;
2727

28-
let color;
29-
switch (level) {
30-
case LogLevel.DEBUG:
31-
color = gray;
32-
break;
33-
case LogLevel.INFO:
34-
color = white;
35-
break;
36-
case LogLevel.WARNING:
37-
color = yellow;
38-
break;
39-
case LogLevel.ERROR:
40-
color = red;
41-
break;
42-
case LogLevel.CRITICAL:
43-
color = (str: string) => bold(red(str));
44-
break;
45-
default:
46-
color = white;
47-
break;
48-
}
28+
const color = ({
29+
[LogLevel.DEBUG]: gray,
30+
[LogLevel.INFO]: white,
31+
[LogLevel.WARNING]: yellow,
32+
[LogLevel.ERROR]: red,
33+
[LogLevel.CRITICAL]: (str: string) => bold(red(str)),
34+
})[level] ?? white;
4935

50-
console.log(color(`${timestamp} ${message}`));
36+
console.log(color(message));
5137
};

0 commit comments

Comments
 (0)