-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathGhostLogger.js
More file actions
643 lines (576 loc) · 21.3 KB
/
GhostLogger.js
File metadata and controls
643 lines (576 loc) · 21.3 KB
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
const each = require('lodash/each');
const upperFirst = require('lodash/upperFirst');
const toArray = require('lodash/toArray');
const isObject = require('lodash/isObject');
const isEmpty = require('lodash/isEmpty');
const includes = require('lodash/includes');
const bunyan = require('bunyan');
const fs = require('fs');
const jsonStringifySafe = require('json-stringify-safe');
/**
* @description Ghost's logger class.
*
* The logger handles any stdout/stderr logs and streams it into the configured transports.
*/
class GhostLogger {
/**
* Properties in the options bag:
*
* name: Name of the logger. The name will appear in the raw log files with {"name": String...}
* domain: Is used for creating the file name.
* env: Is used for creating the file name.
* mode: Is used to print short or long log.
* level: The level defines the default level of all transports except of stderr.
* logBody: Disable or enable if the body of a request should be logged to the target stream.
* transports: An array of comma separated transports (e.g. stdout, stderr, geld, loggly, file)
* rotation: Enable or disable file rotation.
* path: Path where to store log files.
* filename: Optional filename template for log files. Supports {env} and {domain} placeholders.
* If not provided, defaults to {domain}_{env} format.
* loggly: Loggly transport configuration.
* elasticsearch: Elasticsearch transport configuration
* gelf: Gelf transport configuration.
* http: HTTP transport configuration
* useLocalTime: Use local time instead of UTC.
* metadata: Optional set of metadata to attach to each log line
* @param {object} options Bag of options
*/
constructor(options) {
options = options || {};
this.name = options.name || 'Log';
this.env = options.env || 'development';
this.domain = options.domain || 'localhost';
this.transports = options.transports || ['stdout'];
this.level = process.env.LEVEL || options.level || 'info';
this.logBody = options.logBody || false;
this.mode = process.env.MODE || options.mode || 'short';
this.path = options.path || process.cwd();
this.filename = options.filename || '{domain}_{env}';
this.loggly = options.loggly || {};
this.elasticsearch = options.elasticsearch || {};
this.gelf = options.gelf || {};
this.http = options.http || {};
this.useLocalTime = options.useLocalTime || false;
this.metadata = options.metadata || {};
// CASE: stdout has to be on the first position in the transport, because if the GhostLogger itself logs, you won't see the stdout print
if (this.transports.indexOf('stdout') !== -1 && this.transports.indexOf('stdout') !== 0) {
this.transports.splice(this.transports.indexOf('stdout'), 1);
this.transports = ['stdout'].concat(this.transports);
}
// CASE: special env variable to enable long mode and level info
if (process.env.LOIN) {
this.level = 'info';
this.mode = 'long';
}
// CASE: ensure we have a trailing slash
if (!this.path.match(/\/$|\\$/)) {
this.path = this.path + '/';
}
this.rotation = options.rotation || {
enabled: false,
period: '1w',
count: 100
};
this.streams = {};
this.setSerializers();
if (includes(this.transports, 'stderr') && !includes(this.transports, 'stdout')) {
this.transports.push('stdout');
}
this.transports.forEach((transport) => {
let transportFn = `set${upperFirst(transport)}Stream`;
if (!this[transportFn]) {
throw new Error(`${upperFirst(transport)} is an invalid transport`); // eslint-disable-line
}
this[transportFn]();
});
}
/**
* @description Setup stdout stream.
*/
setStdoutStream() {
const GhostPrettyStream = require('@tryghost/pretty-stream');
const prettyStdOut = new GhostPrettyStream({
mode: this.mode
});
prettyStdOut.pipe(process.stdout);
this.streams.stdout = {
name: 'stdout',
log: bunyan.createLogger({
name: this.name,
streams: [{
type: 'raw',
stream: prettyStdOut,
level: this.level
}],
serializers: this.serializers
})
};
}
/**
* @description Setup stderr stream.
*/
setStderrStream() {
const GhostPrettyStream = require('@tryghost/pretty-stream');
const prettyStdErr = new GhostPrettyStream({
mode: this.mode
});
prettyStdErr.pipe(process.stderr);
this.streams.stderr = {
name: 'stderr',
log: bunyan.createLogger({
name: this.name,
streams: [{
type: 'raw',
stream: prettyStdErr,
level: 'error'
}],
serializers: this.serializers
})
};
}
/**
* Setup stream for posting the message to a parent instance
*/
setParentStream() {
const {parentPort} = require('worker_threads');
const bunyanStream = {
// Parent stream only supports sending a string
write: (bunyanObject) => {
const {msg} = bunyanObject;
parentPort.postMessage(msg);
}
};
this.streams.parent = {
name: 'parent',
log: bunyan.createLogger({
name: this.name,
streams: [{
type: 'raw',
stream: bunyanStream,
level: this.level
}],
serializers: this.serializers
})
};
}
/**
* @description Setup loggly.
*/
setLogglyStream() {
const Bunyan2Loggly = require('bunyan-loggly');
const logglyStream = new Bunyan2Loggly({
token: this.loggly.token,
subdomain: this.loggly.subdomain,
tags: this.loggly.tags
});
this.streams.loggly = {
name: 'loggly',
match: this.loggly.match,
log: bunyan.createLogger({
name: this.name,
streams: [{
type: 'raw',
stream: logglyStream,
level: 'error'
}],
serializers: this.serializers
})
};
}
/**
* @description Setup ElasticSearch.
*/
setElasticsearchStream() {
const ElasticSearch = require('@tryghost/elasticsearch').BunyanStream;
const elasticSearchInstance = new ElasticSearch({
node: this.elasticsearch.host,
auth: {
username: this.elasticsearch.username,
password: this.elasticsearch.password
}
}, this.elasticsearch.index, this.elasticsearch.pipeline);
this.streams.elasticsearch = {
name: 'elasticsearch',
log: bunyan.createLogger({
name: this.name,
streams: [{
type: 'stream',
stream: elasticSearchInstance.getStream(),
level: this.elasticsearch.level
}],
serializers: this.serializers
})
};
}
setHttpStream() {
const Http = require('@tryghost/http-stream');
const httpStream = new Http({
url: this.http.url,
headers: this.http.headers || {},
username: this.http.username || '',
password: this.http.password || ''
});
this.streams.http = {
name: 'http',
log: bunyan.createLogger({
name: this.name,
streams: [{
type: 'raw',
stream: httpStream,
level: this.http.level
}],
serializers: this.serializers
})
};
}
/**
* @description Setup gelf.
*/
setGelfStream() {
const gelfStream = require('gelf-stream');
const stream = gelfStream.forBunyan(
this.gelf.host || 'localhost',
this.gelf.port || 12201,
this.gelf.options || {}
);
this.streams.gelf = {
name: 'gelf',
log: bunyan.createLogger({
name: this.name,
streams: [{
type: 'raw',
stream: stream,
level: this.level
}],
serializers: this.serializers
})
};
}
/**
* @description Sanitize domain for use in filenames.
* Replaces all non-word characters with underscores.
* @param {string} domain - The domain to sanitize
* @returns {string} Sanitized domain safe for filenames
* @example
* sanitizeDomain('http://my-domain.com') // returns 'http___my_domain_com'
*/
sanitizeDomain(domain) {
return domain.replace(/[^\w]/gi, '_');
}
/**
* @description Replace placeholders in filename template.
* @param {string} template - Filename template with placeholders
* @returns {string} Filename with placeholders replaced
*/
// TODO: Expand to other placeholders?
replaceFilenamePlaceholders(template) {
return template
.replace(/{env}/g, this.env)
.replace(/{domain}/g, this.sanitizeDomain(this.domain));
}
/**
* @description Setup file stream.
*
* By default we log into two files
* 1. file-errors: all errors only
* 2. file-all: everything
*/
setFileStream() {
const baseFilename = this.replaceFilenamePlaceholders(this.filename);
// CASE: target log folder does not exist, show warning
if (!fs.existsSync(this.path)) {
this.error('Target log folder does not exist: ' + this.path);
return;
}
if (this.rotation.enabled) {
if (this.rotation.useLibrary) {
const RotatingFileStream = require('@tryghost/bunyan-rotating-filestream');
const rotationConfig = {
path: `${this.path}${baseFilename}.log`,
period: this.rotation.period,
threshold: this.rotation.threshold,
totalFiles: this.rotation.count,
gzip: this.rotation.gzip,
rotateExisting: (typeof this.rotation.rotateExisting === 'undefined') ? this.rotation.rotateExisting : true
};
this.streams['rotation-errors'] = {
name: 'rotation-errors',
log: bunyan.createLogger({
name: this.name,
streams: [{
stream: new RotatingFileStream(Object.assign({}, rotationConfig, {
path: `${this.path}${baseFilename}.error.log`
})),
level: 'error'
}],
serializers: this.serializers
})
};
this.streams['rotation-all'] = {
name: 'rotation-all',
log: bunyan.createLogger({
name: this.name,
streams: [{
stream: new RotatingFileStream(rotationConfig),
level: this.level
}],
serializers: this.serializers
})
};
} else {
// TODO: Remove this when confidence is high in the external library for rotation
this.streams['rotation-errors'] = {
name: 'rotation-errors',
log: bunyan.createLogger({
name: this.name,
streams: [{
type: 'rotating-file',
path: `${this.path}${baseFilename}.error.log`,
period: this.rotation.period,
count: this.rotation.count,
level: 'error'
}],
serializers: this.serializers
})
};
this.streams['rotation-all'] = {
name: 'rotation-all',
log: bunyan.createLogger({
name: this.name,
streams: [{
type: 'rotating-file',
path: `${this.path}${baseFilename}.log`,
period: this.rotation.period,
count: this.rotation.count,
level: this.level
}],
serializers: this.serializers
})
};
}
} else {
this.streams['file-errors'] = {
name: 'file',
log: bunyan.createLogger({
name: this.name,
streams: [{
path: `${this.path}${baseFilename}.error.log`,
level: 'error'
}],
serializers: this.serializers
})
};
this.streams['file-all'] = {
name: 'file',
log: bunyan.createLogger({
name: this.name,
streams: [{
path: `${this.path}${baseFilename}.log`,
level: this.level
}],
serializers: this.serializers
})
};
}
}
// @TODO: res.on('finish') has no access to the response body
/**
* @description Serialize the log input.
*
* The goals are:
* - avoiding to log to much (pick useful information from request/response
* - removing/replacing sensitive data from logging to a stream/transport
*/
setSerializers() {
this.serializers = {
req: (req) => {
const requestLog = {
meta: {
requestId: req.requestId,
userId: req.userId
},
url: req.url,
method: req.method,
originalUrl: req.originalUrl,
params: req.params,
headers: this.removeSensitiveData(req.headers),
query: this.removeSensitiveData(req.query)
};
if (req.extra) {
requestLog.extra = req.extra;
}
if (this.logBody) {
requestLog.body = this.removeSensitiveData(req.body);
}
if (req.queueDepth) {
requestLog.queueDepth = req.queueDepth;
}
return requestLog;
},
res: (res) => {
return {
_headers: this.removeSensitiveData(res.getHeaders()),
statusCode: res.statusCode,
responseTime: res.responseTime
};
},
err: (err) => {
return {
id: err.id,
domain: this.domain,
code: err.code,
name: err.errorType,
statusCode: err.statusCode,
level: err.level,
message: err.message,
context: jsonStringifySafe(err.context),
help: jsonStringifySafe(err.help),
stack: err.stack,
hideStack: err.hideStack,
errorDetails: jsonStringifySafe(err.errorDetails)
};
}
};
}
/**
* @description Remove sensitive data.
* @param {Object} obj
*/
removeSensitiveData(obj) {
let newObj = {};
for (const key in obj) {
let value = obj[key];
try {
if (isObject(value)) {
value = this.removeSensitiveData(value);
}
if (key.match(/pin|password|pass|key|authorization|bearer|cookie/gi)) {
newObj[key] = '**REDACTED**';
} else {
newObj[key] = value;
}
} catch (err) {
newObj[key] = value;
}
}
return newObj;
}
/**
* @description Centralised log function.
*
* Arguments can contain lot's of different things, we prepare the arguments here.
* This function allows us to use logging very flexible!
*
* logging.info('HEY', 'DU') --> is one string
* logging.info({}, {}) --> is one object
* logging.error(new Error()) --> is {err: new Error()}
*/
log(type, args) {
let modifiedMessages = [];
let modifiedObject = {};
let modifiedArguments = [];
if (this.metadata) {
for (const key in this.metadata) {
modifiedObject[key] = this.metadata[key];
}
}
each(args, function (value) {
if (value instanceof Error) {
modifiedObject.err = value;
} else if (isObject(value)) {
each(Object.keys(value), function (key) {
modifiedObject[key] = value[key];
});
} else {
modifiedMessages.push(value);
}
});
if (this.useLocalTime) {
let currentDate = new Date();
currentDate.setMinutes(currentDate.getMinutes() - currentDate.getTimezoneOffset());
modifiedObject.time = currentDate.toISOString();
}
if (!isEmpty(modifiedObject)) {
if (modifiedObject.err) {
modifiedMessages.push(modifiedObject.err.message);
}
modifiedArguments.push(modifiedObject);
}
modifiedArguments.push(...modifiedMessages);
each(this.streams, (logger) => {
// If we have both a stdout and a stderr stream, don't log errors to stdout
// because it would result in duplicate logs
if (type === 'error' && logger.name === 'stdout' && includes(this.transports, 'stderr')) {
return;
}
/**
* @NOTE
* Only `loggly` offers the `match` option.
* And currently `loggly` is by default configured to only send errors (not configureable).
* e.g. level info would get ignored.
*
* @NOTE
* The `match` feature is not completed. We hardcode checking if the level/type is `error` for now.
* Otherwise each `level:info` would has to run through the matching logic.
*
* @NOTE
* Matching a string in the whole req/res object massively slows down the process, because it's a sync
* operation.
*
* If we want to extend the feature, we can only offer matching certain keys e.g. status code, headers.
* If we want to extend the feature, we have to do proper performance testing.
*
* `jsonStringifySafe` can match a string in an object, which has circular dependencies.
* https://github.com/moll/json-stringify-safe
*/
if (logger.match && type === 'error') {
if (new RegExp(logger.match).test(jsonStringifySafe(modifiedArguments[0].err || null).replace(/"/g, ''))) {
logger.log[type](...modifiedArguments);
}
} else {
logger.log[type](...modifiedArguments);
}
});
}
trace() {
this.log('trace', toArray(arguments));
}
debug() {
this.log('debug', toArray(arguments));
}
info() {
this.log('info', toArray(arguments));
}
warn() {
this.log('warn', toArray(arguments));
}
error() {
this.log('error', toArray(arguments));
}
fatal() {
this.log('fatal', toArray(arguments));
}
/**
* @description Creates a child of the logger with some properties bound for every log message
*/
child(boundProperties) {
const result = new GhostLogger({
name: this.name,
env: this.env,
domain: this.domain,
transports: [],
level: this.level,
logBody: this.logBody,
mode: this.mode
});
result.streams = Object.keys(this.streams).reduce((acc, id) => {
acc[id] = {
name: this.streams[id].name,
log: this.streams[id].log.child(boundProperties)
};
return acc;
}, {});
return result;
}
}
module.exports = GhostLogger;