/
interfaces.ts
388 lines (338 loc) 路 10.1 KB
/
interfaces.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { InspectOptions } from "util";
import DateTimeFormatPartTypes = Intl.DateTimeFormatPartTypes;
/**
* All possible log levels
* @public
*/
export interface ILogLevel {
0: "silly";
1: "trace";
2: "debug";
3: "info";
4: "warn";
5: "error";
6: "fatal";
}
/**
* Log level IDs (0 - 6)
* @public
*/
export type TLogLevelId = keyof ILogLevel;
/**
* Log level names (sill - fatal)
* @public
*/
export type TLogLevelName = ILogLevel[TLogLevelId];
/**
* Hex colors of different log levels
* @public
*/
export type TLogLevelColor = {
[key in TLogLevelId]: TUtilsInspectColors;
};
/**
* Function that generates a request ID.
* @public
*/
export type TRequestIdFunction = () => string;
/**
* Constructor: logger settings
* all values are optional and will be pre-filled with default values
* @public
*/
export interface ISettingsParam {
/** Print log `pretty`, as a stringified `json` or prevent printing with `hidden`, default: `pretty` */
type?: "json" | "pretty" | "hidden";
/** Name of the instance name, default: _host name_ */
instanceName?: string;
/** Use the name of the caller type as the name of the logger, default: `false` */
setCallerAsLoggerName?: boolean;
/** Optional name of the logger instance, default: `undefined` */
name?: string;
/** Minimum output log level (e.g. debug), default: "silly" */
minLevel?: TLogLevelName;
/** Optional request ID for grouping logs by a logical request. default: `undefined` */
requestId?: string | TRequestIdFunction;
/** Expose stack with EVERY log message, default: `false` */
exposeStack?: boolean;
/** Get Code Frame of an Error and expose it, default: `true` */
exposeErrorCodeFrame?: boolean;
/** Capture lines before and after a code frame, default: `5` */
exposeErrorCodeFrameLinesBeforeAndAfter?: number;
/** Suppress any log output to std out / std err */
suppressStdOutput?: boolean;
/** Catch logs going to console (e.g. console.log). Last instantiated Log instance wins */
overwriteConsole?: boolean;
/** Overwrite colors of log messages of different log levels */
logLevelsColors?: TLogLevelColor;
/** Overwrite colors json highlighting */
prettyInspectHighlightStyles?: IHighlightStyles;
/** Options to be set for utils._inspectAndHideSensitive when output is set to pretty, default: `setting` */
prettyInspectOptions?: InspectOptions;
/** Options to be set for utils._inspectAndHideSensitive when output is set to json (\{ type: "json" \}) */
jsonInspectOptions?: InspectOptions;
/** DateTime pattern based on Intl.DateTimeFormat.formatToParts with additional milliseconds, default: `year-month-day hour:minute:second.millisecond` */
dateTimePattern?: string;
/** DateTime timezone, e.g. `utc`, or `Europe/Berlin`, `Europe/Moscow`. You can use `Intl.DateTimeFormat().resolvedOptions().timeZone` for local timezone, default: "utc" */
dateTimeTimezone?: string;
/** Prefix every log message of this logger. */
prefix?: unknown[];
/** Exclude case-insensitive keys for object passed to `tslog` that could potentially contain sensitive information (e.g. `password` or `Authorization`), default: ["password"] */
maskValuesOfKeys?: (number | string)[];
/** Mask all occurrences (case-sensitive) from logs (e.g. all secrets from ENVs etc.). Will be replaced with [***] */
maskAny?: (string | number)[];
/** String to use a placeholder to mask sensitive values. */
maskPlaceholder?: string;
/** Print log message in a new line below meta information, default: `false` */
printLogMessageInNewLine?: boolean;
/** Display date time at the beginning of a log message, default: `true` */
displayDateTime?: boolean;
/** Display log level, default: `true` */
displayLogLevel?: boolean;
/** Display instanceName or not, default: `false` */
displayInstanceName?: boolean;
/** Display name of the logger. Will only be visible if `name` was set, default: `true` */
displayLoggerName?: boolean;
/** Display requestId or not, default: `false` */
displayRequestId?: boolean;
/** Display file path, default "hideNodeModulesOnly" */
displayFilePath?: "hidden" | "displayAll" | "hideNodeModulesOnly";
/** Display function name, default: `true`*/
displayFunctionName?: boolean;
/** Display type information for each attribute passed. */
displayTypes?: boolean;
/** Overwrite default std out */
stdOut?: IStd;
/** Overwrite default std err */
stdErr?: IStd;
}
/**
* The actual settings object
* Based on ISettingsParam, however pre-filled with defaults in case no value was provided.
* @public
*/
export interface ISettings extends ISettingsParam {
type: "json" | "pretty" | "hidden";
instanceName?: string;
setCallerAsLoggerName: boolean;
name?: string;
requestId?: string | TRequestIdFunction;
minLevel: TLogLevelName;
exposeStack: boolean;
exposeErrorCodeFrame: boolean;
exposeErrorCodeFrameLinesBeforeAndAfter: number;
suppressStdOutput: boolean;
overwriteConsole: boolean;
logLevelsColors: TLogLevelColor;
prettyInspectHighlightStyles: IHighlightStyles;
prettyInspectOptions: InspectOptions;
jsonInspectOptions: InspectOptions;
dateTimePattern: string;
dateTimeTimezone: string;
prefix: unknown[];
maskValuesOfKeys: (number | string)[];
maskAny: (string | number)[];
maskPlaceholder: string;
printLogMessageInNewLine: boolean;
displayDateTime: boolean;
displayLogLevel: boolean;
displayInstanceName: boolean;
displayLoggerName?: boolean;
displayRequestId: boolean;
displayFilePath: "hidden" | "displayAll" | "hideNodeModulesOnly";
displayFunctionName: boolean;
displayTypes: boolean;
stdOut: IStd;
stdErr: IStd;
}
/**
* StdOut and StdErr have to implement a write function (e.g. Stream)
* @public
*/
export interface IStd {
/** stream.Writable */
write: (message: string) => void;
}
/**
* All relevant information about a log message
* @public
*/
export interface IStackFrame {
/** Relative path based on the main folder */
filePath: string;
/** Full path */
fullFilePath: string;
/** Name of the file */
fileName: string;
/** Line number */
lineNumber: number | null;
/** Column Name */
columnNumber: number | null;
/** Called from constructor */
isConstructor: boolean | null;
/** Name of the function */
functionName: string | null;
/** Name of the class */
typeName: string | null;
/** Name of the Method */
methodName: string | null;
}
/**
* All relevant information about a log message.
* @public
*/
export interface ILogObject extends IStackFrame {
/** Optional name of the instance this application is running on. */
instanceName?: string;
/** Optional name of the logger or empty string. */
loggerName?: string;
/** Name of the host */
hostname: string;
/** Optional unique request ID */
requestId?: string;
/** Timestamp */
date: Date;
/** Log level name (e.g. debug) */
logLevel: TLogLevelName;
/** Log level ID (e.g. 3) */
logLevelId: TLogLevelId;
/** Log arguments */
argumentsArray: (IErrorObject | unknown)[];
/** Optional Log stack trace */
stack?: IStackFrame[];
/** Convert a LogObject into a JSON, is used by JSON.stringify() */
toJSON: () => ILogObjectStringifiable;
}
/**
* LogObject that can safely be "JSON.stringifed". All circular structures have been "util.inspected" into strings
* @public
*/
export interface ILogObjectStringifiable extends ILogObject {
argumentsArray: (IErrorObjectStringifiable | string)[];
}
/**
* Object representing an error with a stack trace
* @public
*/
export interface IErrorObject {
/** Is this object an error? */
isError: true;
/** Name of the error*/
name: string;
/** Error message */
message: string;
/** additional Error details */
details: object;
/** native Error object */
nativeError: Error;
/** Stack trace of the error */
stack: IStackFrame[];
/** Code frame of the error */
codeFrame?: ICodeFrame;
}
/**
* ErrorObject that can safely be "JSON.stringifed". All circular structures have been "util.inspected" into strings
* @public
*/
export interface IErrorObjectStringifiable extends IErrorObject {
nativeError: never;
errorString: string;
}
/**
* List of attached transport logger with their respective min log level.
* @public
*/
export type TTransportLogger<T> = {
[key in TLogLevelName]: T;
};
export interface ITransportProvider {
minLevel: TLogLevelName;
transportLogger: TTransportLogger<(message: ILogObject) => void>;
}
/**
* Style and color options for utils._inspectAndHideSensitive.style
* @public
*/
export type TUtilsInspectColors =
| "reset"
| "bold"
| "dim"
| "italic"
| "underline"
| "blink"
| "inverse"
| "hidden"
| "strikethrough"
| "doubleunderline"
| "black"
| "red"
| "green"
| "yellow"
| "blue"
| "magenta"
| "cyan"
| "white"
| "bgBlack"
| "bgRed"
| "bgGreen"
| "bgYellow"
| "bgBlue"
| "bgMagenta"
| "bgCyan"
| "bgWhite"
| "framed"
| "overlined"
| "gray"
| "grey"
| "redBright"
| "greenBright"
| "yellowBright"
| "blueBright"
| "magentaBright"
| "cyanBright"
| "whiteBright"
| "bgGray"
| "bgRedBright"
| "bgGreenBright"
| "bgYellowBright"
| "bgBlueBright"
| "bgMagentaBright"
| "bgCyanBright"
| "bgWhiteBright";
/**
* Possible style settings of utils._inspectAndHideSensitive.styles
* Official Node.js typedefs are missing this interface.
* @public
*/
export interface IHighlightStyles {
name?: TUtilsInspectColors;
special?: TUtilsInspectColors;
number?: TUtilsInspectColors;
bigint?: TUtilsInspectColors;
boolean?: TUtilsInspectColors;
undefined?: TUtilsInspectColors;
null?: TUtilsInspectColors;
string?: TUtilsInspectColors;
symbol?: TUtilsInspectColors;
date?: TUtilsInspectColors;
regexp?: TUtilsInspectColors;
module?: TUtilsInspectColors;
}
/**
* Code frame of an error
* @public
* */
export interface ICodeFrame {
firstLineNumber: number;
lineNumber: number;
columnNumber: number | null;
linesBefore: string[];
relevantLine: string;
linesAfter: string[];
}
type WithMillisecond<T> = T & "millisecond";
/* Extend Intl.DateTimeFormatPart with milliseconds */
export interface IFullDateTimeFormatPart {
type: WithMillisecond<DateTimeFormatPartTypes>;
value: string;
}