-
Notifications
You must be signed in to change notification settings - Fork 331
/
logger.js
131 lines (105 loc) · 2.92 KB
/
logger.js
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
/* @flow */
import bunyan, {nameFromLevel, createLogger as defaultLogCreator}
from 'bunyan';
// Bunyan-related Flow types
export type TRACE = 10;
export type DEBUG = 20;
export type INFO = 30;
export type WARN = 40;
export type ERROR = 50;
export type FATAL = 60;
export type BunyanLogLevel =
TRACE | DEBUG | INFO | WARN | ERROR | FATAL;
export type BunyanLogEntry = {|
name: string,
msg: string,
level: BunyanLogLevel,
|};
export type Logger = {
debug: (msg: string, ...args: any) => void,
error: (msg: string, ...args: any) => void,
info: (msg: string, ...args: any) => void,
warn: (msg: string, ...args: any) => void,
};
// ConsoleStream types and implementation.
export type ConsoleStreamParams = {|
verbose?: boolean,
|};
export type ConsoleOptions = {|
localProcess?: typeof process,
|};
export class ConsoleStream {
verbose: boolean;
isCapturing: boolean;
capturedMessages: Array<string>;
constructor({verbose = false}: ConsoleStreamParams = {}) {
this.verbose = verbose;
this.isCapturing = false;
this.capturedMessages = [];
}
format({name, msg, level}: BunyanLogEntry): string {
const prefix = this.verbose ? `[${name}][${nameFromLevel[level]}] ` : '';
return `${prefix}${msg}\n`;
}
makeVerbose() {
this.verbose = true;
}
write(
packet: BunyanLogEntry,
{localProcess = process}: ConsoleOptions = {}
): void {
const thisLevel: BunyanLogLevel = this.verbose ? bunyan.TRACE : bunyan.INFO;
if (packet.level >= thisLevel) {
const msg = this.format(packet);
if (this.isCapturing) {
this.capturedMessages.push(msg);
} else {
localProcess.stdout.write(msg);
}
}
}
startCapturing() {
this.isCapturing = true;
}
stopCapturing() {
this.isCapturing = false;
this.capturedMessages = [];
}
flushCapturedLogs({localProcess = process}: ConsoleOptions = {}) {
for (const msg of this.capturedMessages) {
localProcess.stdout.write(msg);
}
this.capturedMessages = [];
}
}
export const consoleStream = new ConsoleStream();
// createLogger types and implementation.
export type BunyanStreamConfig = {|
type: string,
stream: ConsoleStream,
|};
export type CreateBunyanLogParams = {|
name: string,
level: BunyanLogLevel,
streams: Array<BunyanStreamConfig>,
|};
export type CreateBunyanLogFn = (params: CreateBunyanLogParams) => Logger;
export type CreateLoggerOptions = {|
createBunyanLog: CreateBunyanLogFn,
|};
export function createLogger(
filename: string,
{createBunyanLog = defaultLogCreator}: CreateLoggerOptions = {}
): Logger {
return createBunyanLog({
// Strip the leading src/ from file names (which is in all file names) to
// make the name less redundant.
name: filename.replace(/^src\//, ''),
// Capture all log levels and let the stream filter them.
level: bunyan.TRACE,
streams: [{
type: 'raw',
stream: consoleStream,
}],
});
}