-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
118 lines (96 loc) · 2.78 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2020 Yamboy1. All rights reserved. MIT license.
export * from "./sinks.ts";
export * from "./types.ts";
import { LogEntry, LogLevel, SinkFunction } from "./types.ts";
import { parseLogEntry } from "./parser.ts";
export interface LoggerOptions {
/** Minimum log level, any logs under this level will be ignored. Defaults to LogLevel.INFO */
minimumLevel: LogLevel;
/** Output format for text logs */
outputFormat: string;
}
/** The default text output format */
export const textFormat = "[{timestamp} {level}] {message}";
/** A json output format */
export const jsonFormat =
'{"message":"{message}","level":"{level}","timestamp":"{timestamp}","data":{params}}';
/** Create a new Logger */
export function createLogger(
{
minimumLevel = LogLevel.DEBUG,
outputFormat = textFormat,
}: Partial<LoggerOptions> = {},
): Logger {
return new LoggerImpl({
minimumLevel,
outputFormat,
});
}
export type LogFunction = (format: string, ...args: unknown[]) => void;
interface Sink {
sinkFunc: SinkFunction;
outputFormat: string;
}
/** THe main logger class */
export interface Logger {
/** Add a sink */
addSink(sinkFunc: SinkFunction, outputFormat?: string): this;
debug: LogFunction;
info: LogFunction;
warn: LogFunction;
error: LogFunction;
critical: LogFunction;
}
class LoggerImpl implements Logger {
private readonly minimumLevel: LogLevel;
private readonly outputFormat: string;
private sinks: Sink[] = [];
constructor({ minimumLevel, outputFormat }: LoggerOptions) {
this.minimumLevel = minimumLevel;
this.outputFormat = outputFormat;
}
addSink(
sinkFunc: SinkFunction,
outputFormat: string = this.outputFormat,
): this {
this.sinks.push({
sinkFunc,
outputFormat,
});
return this;
}
private log(level: LogLevel, format: string, ...args: unknown[]): void {
if (level < this.minimumLevel) return;
if (this.sinks.length === 0) {
// TODO(yamboy1): revisit this idea
// console.error(
// "[INTERNAL] Warning: No sinks are provided.",
// );
return;
}
for (let sink of this.sinks) {
const parsed = parseLogEntry(
level,
format,
sink.outputFormat,
...args,
);
sink.sinkFunc(parsed);
}
}
debug(format: string, ...args: unknown[]): void {
this.log(LogLevel.DEBUG, format, ...args);
}
info(format: string, ...args: unknown[]): void {
this.log(LogLevel.INFO, format, ...args);
}
warn(format: string, ...args: unknown[]): void {
this.log(LogLevel.WARN, format, ...args);
}
error(format: string, ...args: unknown[]): void {
this.log(LogLevel.ERROR, format, ...args);
}
critical(format: string, ...args: unknown[]): void {
this.log(LogLevel.CRITICAL, format, ...args);
}
}