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