generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.ts
138 lines (126 loc) · 3.68 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { blue, datetime, red, reset, yellow } from "./deps.ts";
const LOG_LEVELS = {
debug: {
color: reset,
symbol: "✔",
},
info: {
color: blue,
symbol: "ℹ",
},
warn: {
color: yellow,
symbol: "⚠",
},
error: {
color: red,
symbol: "✖",
},
};
/**
* The type for the log level.
* These correspond to the built-in `console` method.
*/
export type LogLevel = "debug" | "info" | "warn" | "error";
/**
* Timestamp-Level-Log for Deno.
*
* @Example
* ```ts
* const log = new Log();
* log.debug("debug");
* log.info("info", true, null);
* log.warn(["warning", 1, 2.4]);
* try {
* Deno.readFileSync("hello.txt");
* } catch (error) {
* log.error({error});
* }
* ```
*/
export class Log {
private datetimeFormat: string;
private levelSign: (logLevel: LogLevel) => string;
private suffix: string[];
/**
* Log constructor.
* @param minLogLevel [default:"debug"] Minimum level to log.
* @param levelIndicator [default:"symbol"] Indicator type of log level.
* @param datetimeFormat [default:"YYYY-MM-ddTHH:mm:ssZ"] Format of the timestamp. To use this, follow [the documentation of Ptera](https://tak-iwamoto.github.io/ptera/format.html).
* @param addNewLine [default:false] Flag to add new line after the each log or not.
*
* @Example
* ```ts
* const log = new Log({ minLogLevel: "info", addNewLine: true });
* log.debug("This will not be displayed");
* log.info("System all green");
* ```
*/
constructor({
minLogLevel = "debug",
levelIndicator = "symbol",
datetimeFormat = "YYYY-MM-ddTHH:mm:ssZ",
addNewLine = false,
}: {
minLogLevel?: LogLevel;
levelIndicator?: "none" | "full" | "initial" | "symbol";
datetimeFormat?: string;
addNewLine?: boolean;
} = {}) {
for (const level of Object.keys(LOG_LEVELS) as LogLevel[]) {
if (minLogLevel === level) break;
this[level] = () => ({});
}
this.datetimeFormat = datetimeFormat;
this.suffix = addNewLine ? ["\n"] : [];
this.levelSign = {
none: () => "",
full: (logLevel: LogLevel) => " " + logLevel.toUpperCase().padEnd(5),
initial: (logLevel: LogLevel) => " " + logLevel[0].toUpperCase(),
symbol: (logLevel: LogLevel) => " " + LOG_LEVELS[logLevel].symbol,
}[levelIndicator];
}
/**
* Generate log prefix with the specified configurations.
* This is not needed for most normal users, but exported to test.
* @param date The date of the timestamp.
* @param logLevel The level of the output.
*/
_prefix(date: Date, logLevel: LogLevel) {
const timestamp = datetime(date).format(this.datetimeFormat);
const prefix = `${timestamp}${this.levelSign(logLevel)}`.trimStart();
return prefix ? [LOG_LEVELS[logLevel].color(prefix)] : [];
}
private output(date: Date, logLevel: LogLevel, args: unknown[]) {
const prefix = this._prefix(date, logLevel);
console[logLevel](...prefix, ...args, ...this.suffix);
}
/**
* Output `debug` level log with timestamp and level indicator.
* @param args What to output.
*/
debug(...args: unknown[]) {
this.output(new Date(), "debug", args);
}
/**
* Output `info` level log with timestamp and level indicator.
* @param args What to output.
*/
info(...args: unknown[]) {
this.output(new Date(), "info", args);
}
/**
* Output `warn` level log with timestamp and level indicator.
* @param args What to output.
*/
warn(...args: unknown[]) {
this.output(new Date(), "warn", args);
}
/**
* Output `error` level log with timestamp and level indicator.
* @param args What to output.
*/
error(...args: unknown[]) {
this.output(new Date(), "error", args);
}
}