]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ |
15a7eafb | 2 | import { mkdirpSync, stat } from 'fs-extra' |
2a6cf69c | 3 | import { omit } from 'lodash' |
41fb13c3 | 4 | import { join } from 'path' |
2a6cf69c | 5 | import { format as sqlFormat } from 'sql-formatter' |
41fb13c3 | 6 | import { createLogger, format, transports } from 'winston' |
fcf4569f | 7 | import { FileTransportOptions } from 'winston/lib/winston/transports' |
6dd9de95 | 8 | import { CONFIG } from '../initializers/config' |
837666fe | 9 | import { LOG_FILENAME } from '../initializers/constants' |
8c308c2b | 10 | |
65fcc311 | 11 | const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT |
320d6275 C |
12 | |
13 | // Create the directory if it does not exist | |
fd8710b8 | 14 | // FIXME: use async |
c9d5c64f | 15 | mkdirpSync(CONFIG.STORAGE.LOG_DIR) |
320d6275 | 16 | |
959dbbd7 C |
17 | function getLoggerReplacer () { |
18 | const seen = new WeakSet() | |
e20015d7 | 19 | |
959dbbd7 C |
20 | // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples |
21 | return (key: string, value: any) => { | |
a9d4c3c8 C |
22 | if (key === 'cert') return 'Replaced by the logger to avoid large log message' |
23 | ||
959dbbd7 C |
24 | if (typeof value === 'object' && value !== null) { |
25 | if (seen.has(value)) return | |
d5b7d911 | 26 | |
959dbbd7 C |
27 | seen.add(value) |
28 | } | |
29 | ||
1896bca0 C |
30 | if (value instanceof Set) { |
31 | return Array.from(value) | |
32 | } | |
33 | ||
34 | if (value instanceof Map) { | |
35 | return Array.from(value.entries()) | |
36 | } | |
37 | ||
959dbbd7 C |
38 | if (value instanceof Error) { |
39 | const error = {} | |
40 | ||
a1587156 | 41 | Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] }) |
d5b7d911 | 42 | |
959dbbd7 C |
43 | return error |
44 | } | |
45 | ||
46 | return value | |
47 | } | |
23e27dd5 C |
48 | } |
49 | ||
41fb13c3 | 50 | const consoleLoggerFormat = format.printf(info => { |
452b3bea | 51 | const toOmit = [ 'label', 'timestamp', 'level', 'message', 'sql', 'tags' ] |
1e743faa C |
52 | |
53 | const obj = omit(info, ...toOmit) | |
328e607d | 54 | |
959dbbd7 | 55 | let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) |
fd8710b8 | 56 | |
e20015d7 | 57 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' |
2af4fa4d | 58 | else additionalInfos = ' ' + additionalInfos |
23e27dd5 | 59 | |
d223dca0 C |
60 | if (info.sql) { |
61 | if (CONFIG.LOG.PRETTIFY_SQL) { | |
62 | additionalInfos += '\n' + sqlFormat(info.sql, { | |
63 | language: 'sql', | |
ba5a8d89 | 64 | indent: ' ' |
d223dca0 C |
65 | }) |
66 | } else { | |
67 | additionalInfos += ' - ' + info.sql | |
68 | } | |
2a6cf69c RK |
69 | } |
70 | ||
2af4fa4d | 71 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` |
23e27dd5 C |
72 | }) |
73 | ||
41fb13c3 | 74 | const jsonLoggerFormat = format.printf(info => { |
959dbbd7 | 75 | return JSON.stringify(info, getLoggerReplacer()) |
276d03ed C |
76 | }) |
77 | ||
41fb13c3 | 78 | const timestampFormatter = format.timestamp({ |
0647f472 | 79 | format: 'YYYY-MM-DD HH:mm:ss.SSS' |
23e27dd5 | 80 | }) |
bc0d801b | 81 | const labelFormatter = (suffix?: string) => { |
41fb13c3 | 82 | return format.label({ |
bc0d801b C |
83 | label: suffix ? `${label} ${suffix}` : label |
84 | }) | |
85 | } | |
23e27dd5 | 86 | |
fcf4569f | 87 | const fileLoggerOptions: FileTransportOptions = { |
41fb13c3 | 88 | filename: join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME), |
fcf4569f | 89 | handleExceptions: true, |
41fb13c3 C |
90 | format: format.combine( |
91 | format.timestamp(), | |
fcf4569f NB |
92 | jsonLoggerFormat |
93 | ) | |
94 | } | |
95 | ||
2f6b5e2d C |
96 | if (CONFIG.LOG.ROTATION.ENABLED) { |
97 | fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE | |
98 | fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES | |
fcf4569f NB |
99 | } |
100 | ||
bc0d801b C |
101 | const logger = buildLogger() |
102 | ||
103 | function buildLogger (labelSuffix?: string) { | |
41fb13c3 | 104 | return createLogger({ |
bc0d801b | 105 | level: CONFIG.LOG.LEVEL, |
41fb13c3 | 106 | format: format.combine( |
bc0d801b | 107 | labelFormatter(labelSuffix), |
41fb13c3 | 108 | format.splat() |
bc0d801b C |
109 | ), |
110 | transports: [ | |
41fb13c3 C |
111 | new transports.File(fileLoggerOptions), |
112 | new transports.Console({ | |
bc0d801b | 113 | handleExceptions: true, |
41fb13c3 | 114 | format: format.combine( |
bc0d801b | 115 | timestampFormatter, |
41fb13c3 | 116 | format.colorize(), |
bc0d801b C |
117 | consoleLoggerFormat |
118 | ) | |
119 | }) | |
120 | ], | |
121 | exitOnError: true | |
122 | }) | |
123 | } | |
8c308c2b | 124 | |
05e67d62 C |
125 | function bunyanLogFactory (level: string) { |
126 | return function () { | |
127 | let meta = null | |
c1e791ba RK |
128 | let args: any[] = [] |
129 | args.concat(arguments) | |
05e67d62 | 130 | |
a1587156 C |
131 | if (arguments[0] instanceof Error) { |
132 | meta = arguments[0].toString() | |
05e67d62 C |
133 | args = Array.prototype.slice.call(arguments, 1) |
134 | args.push(meta) | |
a1587156 C |
135 | } else if (typeof (args[0]) !== 'string') { |
136 | meta = arguments[0] | |
05e67d62 C |
137 | args = Array.prototype.slice.call(arguments, 1) |
138 | args.push(meta) | |
139 | } | |
140 | ||
a1587156 | 141 | logger[level].apply(logger, args) |
05e67d62 C |
142 | } |
143 | } | |
a1587156 | 144 | |
05e67d62 C |
145 | const bunyanLogger = { |
146 | trace: bunyanLogFactory('debug'), | |
147 | debug: bunyanLogFactory('debug'), | |
148 | info: bunyanLogFactory('info'), | |
149 | warn: bunyanLogFactory('warn'), | |
150 | error: bunyanLogFactory('error'), | |
151 | fatal: bunyanLogFactory('error') | |
152 | } | |
452b3bea | 153 | |
46320694 C |
154 | type LoggerTagsFn = (...tags: string[]) => { tags: string[] } |
155 | function loggerTagsFactory (...defaultTags: string[]): LoggerTagsFn { | |
452b3bea C |
156 | return (...tags: string[]) => { |
157 | return { tags: defaultTags.concat(tags) } | |
158 | } | |
159 | } | |
160 | ||
15a7eafb C |
161 | async function mtimeSortFilesDesc (files: string[], basePath: string) { |
162 | const promises = [] | |
163 | const out: { file: string, mtime: number }[] = [] | |
164 | ||
165 | for (const file of files) { | |
166 | const p = stat(basePath + '/' + file) | |
167 | .then(stats => { | |
168 | if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() }) | |
169 | }) | |
170 | ||
171 | promises.push(p) | |
172 | } | |
173 | ||
174 | await Promise.all(promises) | |
175 | ||
176 | out.sort((a, b) => b.mtime - a.mtime) | |
177 | ||
178 | return out | |
179 | } | |
180 | ||
9f10b292 | 181 | // --------------------------------------------------------------------------- |
c45f7f84 | 182 | |
23e27dd5 | 183 | export { |
46320694 C |
184 | LoggerTagsFn, |
185 | ||
bc0d801b | 186 | buildLogger, |
23e27dd5 C |
187 | timestampFormatter, |
188 | labelFormatter, | |
276d03ed | 189 | consoleLoggerFormat, |
59390818 | 190 | jsonLoggerFormat, |
15a7eafb | 191 | mtimeSortFilesDesc, |
05e67d62 | 192 | logger, |
452b3bea | 193 | loggerTagsFactory, |
05e67d62 | 194 | bunyanLogger |
23e27dd5 | 195 | } |