]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ |
c9d5c64f | 2 | import { mkdirpSync } from 'fs-extra' |
4d4e5cd4 C |
3 | import * as path from 'path' |
4 | import * as winston from 'winston' | |
fcf4569f | 5 | import { FileTransportOptions } from 'winston/lib/winston/transports' |
6dd9de95 | 6 | import { CONFIG } from '../initializers/config' |
fd8710b8 | 7 | import { omit } from 'lodash' |
837666fe | 8 | import { LOG_FILENAME } from '../initializers/constants' |
8c308c2b | 9 | |
65fcc311 | 10 | const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT |
320d6275 C |
11 | |
12 | // Create the directory if it does not exist | |
fd8710b8 | 13 | // FIXME: use async |
c9d5c64f | 14 | mkdirpSync(CONFIG.STORAGE.LOG_DIR) |
320d6275 | 15 | |
959dbbd7 C |
16 | function getLoggerReplacer () { |
17 | const seen = new WeakSet() | |
e20015d7 | 18 | |
959dbbd7 C |
19 | // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples |
20 | return (key: string, value: any) => { | |
21 | if (typeof value === 'object' && value !== null) { | |
22 | if (seen.has(value)) return | |
d5b7d911 | 23 | |
959dbbd7 C |
24 | seen.add(value) |
25 | } | |
26 | ||
27 | if (value instanceof Error) { | |
28 | const error = {} | |
29 | ||
a1587156 | 30 | Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] }) |
d5b7d911 | 31 | |
959dbbd7 C |
32 | return error |
33 | } | |
34 | ||
35 | return value | |
36 | } | |
23e27dd5 C |
37 | } |
38 | ||
276d03ed | 39 | const consoleLoggerFormat = winston.format.printf(info => { |
fd8710b8 | 40 | const obj = omit(info, 'label', 'timestamp', 'level', 'message') |
328e607d | 41 | |
959dbbd7 | 42 | let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) |
fd8710b8 | 43 | |
e20015d7 | 44 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' |
2af4fa4d | 45 | else additionalInfos = ' ' + additionalInfos |
23e27dd5 | 46 | |
2af4fa4d | 47 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` |
23e27dd5 C |
48 | }) |
49 | ||
e20015d7 | 50 | const jsonLoggerFormat = winston.format.printf(info => { |
959dbbd7 | 51 | return JSON.stringify(info, getLoggerReplacer()) |
276d03ed C |
52 | }) |
53 | ||
23e27dd5 | 54 | const timestampFormatter = winston.format.timestamp({ |
0647f472 | 55 | format: 'YYYY-MM-DD HH:mm:ss.SSS' |
23e27dd5 | 56 | }) |
bc0d801b C |
57 | const labelFormatter = (suffix?: string) => { |
58 | return winston.format.label({ | |
59 | label: suffix ? `${label} ${suffix}` : label | |
60 | }) | |
61 | } | |
23e27dd5 | 62 | |
fcf4569f | 63 | const fileLoggerOptions: FileTransportOptions = { |
566c125d | 64 | filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME), |
fcf4569f NB |
65 | handleExceptions: true, |
66 | format: winston.format.combine( | |
67 | winston.format.timestamp(), | |
68 | jsonLoggerFormat | |
69 | ) | |
70 | } | |
71 | ||
2f6b5e2d C |
72 | if (CONFIG.LOG.ROTATION.ENABLED) { |
73 | fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE | |
74 | fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES | |
fcf4569f NB |
75 | } |
76 | ||
bc0d801b C |
77 | const logger = buildLogger() |
78 | ||
79 | function buildLogger (labelSuffix?: string) { | |
80 | return winston.createLogger({ | |
81 | level: CONFIG.LOG.LEVEL, | |
82 | format: winston.format.combine( | |
83 | labelFormatter(labelSuffix), | |
84 | winston.format.splat() | |
85 | ), | |
86 | transports: [ | |
87 | new winston.transports.File(fileLoggerOptions), | |
88 | new winston.transports.Console({ | |
89 | handleExceptions: true, | |
90 | format: winston.format.combine( | |
91 | timestampFormatter, | |
92 | winston.format.colorize(), | |
93 | consoleLoggerFormat | |
94 | ) | |
95 | }) | |
96 | ], | |
97 | exitOnError: true | |
98 | }) | |
99 | } | |
8c308c2b | 100 | |
05e67d62 C |
101 | function bunyanLogFactory (level: string) { |
102 | return function () { | |
103 | let meta = null | |
c1e791ba RK |
104 | let args: any[] = [] |
105 | args.concat(arguments) | |
05e67d62 | 106 | |
a1587156 C |
107 | if (arguments[0] instanceof Error) { |
108 | meta = arguments[0].toString() | |
05e67d62 C |
109 | args = Array.prototype.slice.call(arguments, 1) |
110 | args.push(meta) | |
a1587156 C |
111 | } else if (typeof (args[0]) !== 'string') { |
112 | meta = arguments[0] | |
05e67d62 C |
113 | args = Array.prototype.slice.call(arguments, 1) |
114 | args.push(meta) | |
115 | } | |
116 | ||
a1587156 | 117 | logger[level].apply(logger, args) |
05e67d62 C |
118 | } |
119 | } | |
a1587156 | 120 | |
05e67d62 C |
121 | const bunyanLogger = { |
122 | trace: bunyanLogFactory('debug'), | |
123 | debug: bunyanLogFactory('debug'), | |
124 | info: bunyanLogFactory('info'), | |
125 | warn: bunyanLogFactory('warn'), | |
126 | error: bunyanLogFactory('error'), | |
127 | fatal: bunyanLogFactory('error') | |
128 | } | |
9f10b292 | 129 | // --------------------------------------------------------------------------- |
c45f7f84 | 130 | |
23e27dd5 | 131 | export { |
bc0d801b | 132 | buildLogger, |
23e27dd5 C |
133 | timestampFormatter, |
134 | labelFormatter, | |
276d03ed | 135 | consoleLoggerFormat, |
59390818 | 136 | jsonLoggerFormat, |
05e67d62 C |
137 | logger, |
138 | bunyanLogger | |
23e27dd5 | 139 | } |