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