-
-
Notifications
You must be signed in to change notification settings - Fork 635
/
Copy pathshared.ts
516 lines (467 loc) · 15.1 KB
/
shared.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import { flatMap } from "@zwave-js/shared";
import type { Format, TransformFunction } from "logform";
import * as path from "node:path";
import { MESSAGE, configs } from "triple-beam";
import winston from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
import type Transport from "winston-transport";
import type { ConsoleTransportInstance } from "winston/lib/winston/transports";
import { colorizer } from "./Colorizer";
import {
CONTROL_CHAR_WIDTH,
LOG_WIDTH,
type LogConfig,
type LogContext,
type MessageRecord,
type ZWaveLogInfo,
type ZWaveLogger,
channelPadding,
directionPrefixPadding,
nonUndefinedLogConfigKeys,
stringToNodeList,
timestampFormatShort,
timestampPadding,
timestampPaddingShort,
} from "./shared_safe";
const { combine, timestamp, label } = winston.format;
const loglevels = configs.npm.levels;
const isTTY = process.stdout.isTTY;
const isUnitTest = process.env.NODE_ENV === "test";
export class ZWaveLoggerBase<TContext extends LogContext = LogContext> {
constructor(loggers: ZWaveLogContainer, logLabel: string) {
this.container = loggers;
this.logger = this.container.getLogger(logLabel);
}
public logger: ZWaveLogger<TContext>;
public container: ZWaveLogContainer;
}
export class ZWaveLogContainer extends winston.Container {
private fileTransport: DailyRotateFile | undefined;
private consoleTransport: ConsoleTransportInstance | undefined;
private loglevelVisibleCache = new Map<string, boolean>();
private logConfig: LogConfig & { level: string } = {
enabled: true,
level: getTransportLoglevel(),
logToFile: !!process.env.LOGTOFILE,
maxFiles: 7,
nodeFilter: stringToNodeList(process.env.LOG_NODES),
transports: undefined as any,
filename: require.main
? path.join(
path.dirname(require.main.filename),
`zwavejs_%DATE%.log`,
)
: path.join(__dirname, "../../..", `zwavejs_%DATE%.log`),
forceConsole: false,
};
constructor(config: Partial<LogConfig> = {}) {
super();
this.updateConfiguration(config);
}
public getLogger(label: string): ZWaveLogger {
if (!this.has(label)) {
this.add(label, {
transports: this.getAllTransports(),
format: createLoggerFormat(label),
// Accept all logs, no matter what. The individual loggers take care
// of filtering the wrong loglevels
level: "silly",
});
}
return this.get(label) as unknown as ZWaveLogger;
}
public updateConfiguration(config: Partial<LogConfig>): void {
// Avoid overwriting configuration settings with undefined if they shouldn't be
for (const key of nonUndefinedLogConfigKeys) {
if (key in config && config[key] === undefined) {
delete config[key];
}
}
const changedLoggingTarget = (config.logToFile != undefined
&& config.logToFile !== this.logConfig.logToFile)
|| (config.forceConsole != undefined
&& config.forceConsole !== this.logConfig.forceConsole);
if (typeof config.level === "number") {
config.level = loglevelFromNumber(config.level);
}
const changedLogLevel = config.level != undefined
&& config.level !== this.logConfig.level;
if (
config.filename != undefined
&& !config.filename.includes("%DATE%")
) {
config.filename += "_%DATE%.log";
}
const changedFilename = config.filename != undefined
&& config.filename !== this.logConfig.filename;
if (config.maxFiles != undefined) {
if (
typeof config.maxFiles !== "number"
|| config.maxFiles < 1
|| config.maxFiles > 365
) {
delete config.maxFiles;
}
}
const changedMaxFiles = config.maxFiles != undefined
&& config.maxFiles !== this.logConfig.maxFiles;
this.logConfig = Object.assign(this.logConfig, config);
// If the loglevel changed, our cached "is visible" info is out of date
if (changedLogLevel) {
this.loglevelVisibleCache.clear();
}
// When the log target (console, file, filename) was changed, recreate the internal transports
// because at least the filename does not update dynamically
// Also do this when configuring the logger for the first time
const recreateInternalTransports = (this.fileTransport == undefined
&& this.consoleTransport == undefined)
|| changedLoggingTarget
|| changedFilename
|| changedMaxFiles;
if (recreateInternalTransports) {
this.fileTransport?.destroy();
this.fileTransport = undefined;
this.consoleTransport?.destroy();
this.consoleTransport = undefined;
}
// When the internal transports or the custom transports were changed, we need to update the loggers
if (recreateInternalTransports || config.transports != undefined) {
this.loggers.forEach((logger) =>
logger.configure({ transports: this.getAllTransports() })
);
}
}
public getConfiguration(): LogConfig {
return this.logConfig;
}
/** Tests whether a log using the given loglevel will be logged */
public isLoglevelVisible(loglevel: string): boolean {
// If we are not connected to a TTY, not logging to a file and don't have any custom transports, we won't see anything
if (
!this.fileTransport
&& !this.consoleTransport
&& (!this.logConfig.transports
|| this.logConfig.transports.length === 0)
) {
return false;
}
if (!this.loglevelVisibleCache.has(loglevel)) {
this.loglevelVisibleCache.set(
loglevel,
loglevel in loglevels
&& loglevels[loglevel] <= loglevels[this.logConfig.level],
);
}
return this.loglevelVisibleCache.get(loglevel)!;
}
public destroy(): void {
for (const key in this.loggers) {
this.close(key);
}
this.fileTransport = undefined;
this.consoleTransport = undefined;
this.logConfig.transports = [];
}
private getAllTransports(): Transport[] {
return [
...this.getInternalTransports(),
...(this.logConfig.transports ?? []),
];
}
private getInternalTransports(): Transport[] {
const ret: Transport[] = [];
// If logging is disabled, don't log to any of the default transports
if (!this.logConfig.enabled) {
return ret;
}
// Log to file only when opted in
if (this.logConfig.logToFile) {
if (!this.fileTransport) {
this.fileTransport = this.createFileTransport();
}
ret.push(this.fileTransport);
}
// Console logs can be noise, so only log to console...
if (
// when in production
!isUnitTest
// and stdout is a TTY while we're not already logging to a file
&& ((isTTY && !this.logConfig.logToFile)
// except when the user explicitly wants to
|| this.logConfig.forceConsole)
) {
if (!this.consoleTransport) {
this.consoleTransport = this.createConsoleTransport();
}
ret.push(this.consoleTransport);
}
return ret;
}
private createConsoleTransport(): ConsoleTransportInstance {
return new winston.transports.Console({
format: createDefaultTransportFormat(
// Only colorize the output if logging to a TTY, otherwise we'll get
// ansi color codes in logfiles or redirected shells
isTTY || isUnitTest,
// Only use short timestamps if logging to a TTY
isTTY,
),
silent: this.isConsoleTransportSilent(),
});
}
private isConsoleTransportSilent(): boolean {
return process.env.NODE_ENV === "test" || !this.logConfig.enabled;
}
private isFileTransportSilent(): boolean {
return !this.logConfig.enabled;
}
private createFileTransport(): DailyRotateFile {
const ret = new DailyRotateFile({
filename: this.logConfig.filename,
auditFile: `${
this.logConfig.filename
.replace("_%DATE%", "_logrotate")
.replace(/\.log$/, "")
}.json`,
datePattern: "YYYY-MM-DD",
createSymlink: true,
symlinkName: path
.basename(this.logConfig.filename)
.replace(`_%DATE%`, "_current"),
zippedArchive: true,
maxFiles: `${this.logConfig.maxFiles}d`,
format: createDefaultTransportFormat(false, false),
silent: this.isFileTransportSilent(),
});
ret.on("new", (newFilename: string) => {
console.log(`Logging to file:
${newFilename}`);
});
ret.on("error", (err: Error) => {
console.error(`Error in file stream rotator: ${err.message}`);
});
return ret;
}
/**
* Checks the log configuration whether logs should be written for a given node id
*/
public shouldLogNode(nodeId: number): boolean {
// If no filters are set, every node gets logged
if (!this.logConfig.nodeFilter) return true;
return this.logConfig.nodeFilter.includes(nodeId);
}
}
function getTransportLoglevel(): string {
return process.env.LOGLEVEL! in loglevels ? process.env.LOGLEVEL! : "debug";
}
/** Performs a reverse lookup of the numeric loglevel */
function loglevelFromNumber(numLevel: number | undefined): string | undefined {
if (numLevel == undefined) return;
for (const [level, value] of Object.entries(loglevels)) {
if (value === numLevel) return level;
}
}
/** Creates the common logger format for all loggers under a given channel */
export function createLoggerFormat(channel: string): Format {
return combine(
// add the channel as a label
label({ label: channel }),
// default to short timestamps
timestamp(),
);
}
/** Prints a formatted and colorized log message */
export function createLogMessagePrinter(shortTimestamps: boolean): Format {
return {
transform: ((info: ZWaveLogInfo) => {
// The formatter has already split the message into multiple lines
const messageLines = messageToLines(info.message);
// Also this can only happen if the user forgot to call the formatter first
if (info.secondaryTagPadding == undefined) {
info.secondaryTagPadding = -1;
}
// Format the first message line
let firstLine = [
info.primaryTags,
messageLines[0],
info.secondaryTagPadding < 0
? undefined
: " ".repeat(info.secondaryTagPadding),
// If the secondary tag padding is zero, the previous segment gets
// filtered out and we have one less space than necessary
info.secondaryTagPadding === 0 && info.secondaryTags
? " " + info.secondaryTags
: info.secondaryTags,
]
.filter((item) => !!item)
.join(" ");
// The directional arrows and the optional grouping lines must be prepended
// without adding spaces
firstLine =
`${info.timestamp} ${info.label} ${info.direction}${firstLine}`;
const lines = [firstLine];
if (info.multiline) {
// Format all message lines but the first
lines.push(
...messageLines.slice(1).map(
(line) =>
// Skip the columns for the timestamp and the channel name
(shortTimestamps
? timestampPaddingShort
: timestampPadding)
+ channelPadding
// Skip the columns for directional arrows
+ directionPrefixPadding
+ line,
),
);
}
info[MESSAGE as any] = lines.join("\n");
return info;
}) as unknown as TransformFunction,
};
}
/** Formats the log message and calculates the necessary paddings */
export const logMessageFormatter: Format = {
transform: ((info: ZWaveLogInfo) => {
const messageLines = messageToLines(info.message);
const firstMessageLineLength = messageLines[0].length;
info.multiline = messageLines.length > 1
|| !messageFitsIntoOneLine(info, info.message.length);
// Align postfixes to the right
if (info.secondaryTags) {
// Calculate how many spaces are needed to right-align the postfix
// Subtract 1 because the parts are joined by spaces
info.secondaryTagPadding = Math.max(
// -1 has the special meaning that we don't print any padding,
// because the message takes all the available space
-1,
LOG_WIDTH
- 1
- calculateFirstLineLength(info, firstMessageLineLength),
);
}
if (info.multiline) {
// Break long messages into multiple lines
const lines: string[] = [];
let isFirstLine = true;
for (let message of messageLines) {
while (message.length) {
const cut = Math.min(
message.length,
isFirstLine
? LOG_WIDTH - calculateFirstLineLength(info, 0) - 1
: LOG_WIDTH - CONTROL_CHAR_WIDTH,
);
isFirstLine = false;
lines.push(message.slice(0, cut));
message = message.slice(cut);
}
}
info.message = lines.join("\n");
}
return info;
}) as unknown as TransformFunction,
};
/** The common logger format for built-in transports */
export function createDefaultTransportFormat(
colorize: boolean,
shortTimestamps: boolean,
): Format {
const formats: Format[] = [
// overwrite the default timestamp format if necessary
shortTimestamps
? timestamp({ format: timestampFormatShort })
: undefined,
logMessageFormatter,
colorize ? colorizer() : undefined,
createLogMessagePrinter(shortTimestamps),
].filter((f): f is Format => !!f);
return combine(...formats);
}
/**
* Calculates the length the first line of a log message would occupy if it is not split
* @param info The message and information to log
* @param firstMessageLineLength The length of the first line of the actual message text, not including pre- and postfixes.
*/
function calculateFirstLineLength(
info: ZWaveLogInfo,
firstMessageLineLength: number,
): number {
return (
[
CONTROL_CHAR_WIDTH - 1,
firstMessageLineLength,
(info.primaryTags || "").length,
(info.secondaryTags || "").length,
]
// filter out empty parts
.filter((len) => len > 0)
// simulate adding spaces between parts
.reduce((prev, val) => prev + (prev > 0 ? 1 : 0) + val)
);
}
/**
* Tests if a given message fits into a single log line
* @param info The message that should be logged
* @param messageLength The length that should be assumed for the actual message without pre and postfixes.
* Can be set to 0 to exclude the message from the calculation
*/
export function messageFitsIntoOneLine(
info: ZWaveLogInfo,
messageLength: number,
): boolean {
const totalLength = calculateFirstLineLength(info, messageLength);
return totalLength <= LOG_WIDTH;
}
export function messageToLines(message: string | string[]): string[] {
if (typeof message === "string") {
return message.split("\n");
} else if (message.length > 0) {
return message;
} else {
return [""];
}
}
/** Splits a message record into multiple lines and auto-aligns key-value pairs */
export function messageRecordToLines(message: MessageRecord): string[] {
const entries = Object.entries(message);
if (!entries.length) return [];
const maxKeyLength = Math.max(...entries.map(([key]) => key.length));
return flatMap(entries, ([key, value]) =>
`${key}:${
" ".repeat(
Math.max(maxKeyLength - key.length + 1, 1),
)
}${value}`
.split("\n")
.map((line) => line.trimEnd()));
}
/** Wraps an array of strings in square brackets and joins them with spaces */
export function tagify(tags: string[]): string {
return tags.map((pfx) => `[${pfx}]`).join(" ");
}
/** Unsilences the console transport of a logger and returns the original value */
export function unsilence(logger: winston.Logger): boolean {
const consoleTransport = logger.transports.find(
(t) => (t as any).name === "console",
);
if (consoleTransport) {
const ret = !!consoleTransport.silent;
consoleTransport.silent = false;
return ret;
}
return false;
}
/** Restores the console transport of a logger to its original silence state */
export function restoreSilence(
logger: winston.Logger,
original: boolean,
): void {
const consoleTransport = logger.transports.find(
(t) => (t as any).name === "console",
);
if (consoleTransport) {
consoleTransport.silent = original;
}
}