]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ |
c9d5c64f | 2 | import { mkdirpSync } from 'fs-extra' |
2a6cf69c | 3 | import { omit } from 'lodash' |
4d4e5cd4 | 4 | import * as path from 'path' |
2a6cf69c | 5 | import { format as sqlFormat } from 'sql-formatter' |
4d4e5cd4 | 6 | import * as winston 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 | ||
30 | if (value instanceof Error) { | |
31 | const error = {} | |
32 | ||
a1587156 | 33 | Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] }) |
d5b7d911 | 34 | |
959dbbd7 C |
35 | return error |
36 | } | |
37 | ||
38 | return value | |
39 | } | |
23e27dd5 C |
40 | } |
41 | ||
276d03ed | 42 | const consoleLoggerFormat = winston.format.printf(info => { |
d223dca0 | 43 | const toOmit = [ 'label', 'timestamp', 'level', 'message', 'sql' ] |
1e743faa C |
44 | |
45 | const obj = omit(info, ...toOmit) | |
328e607d | 46 | |
959dbbd7 | 47 | let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2) |
fd8710b8 | 48 | |
e20015d7 | 49 | if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = '' |
2af4fa4d | 50 | else additionalInfos = ' ' + additionalInfos |
23e27dd5 | 51 | |
d223dca0 C |
52 | if (info.sql) { |
53 | if (CONFIG.LOG.PRETTIFY_SQL) { | |
54 | additionalInfos += '\n' + sqlFormat(info.sql, { | |
55 | language: 'sql', | |
56 | ident: ' ' | |
57 | }) | |
58 | } else { | |
59 | additionalInfos += ' - ' + info.sql | |
60 | } | |
2a6cf69c RK |
61 | } |
62 | ||
2af4fa4d | 63 | return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}` |
23e27dd5 C |
64 | }) |
65 | ||
e20015d7 | 66 | const jsonLoggerFormat = winston.format.printf(info => { |
959dbbd7 | 67 | return JSON.stringify(info, getLoggerReplacer()) |
276d03ed C |
68 | }) |
69 | ||
23e27dd5 | 70 | const timestampFormatter = winston.format.timestamp({ |
0647f472 | 71 | format: 'YYYY-MM-DD HH:mm:ss.SSS' |
23e27dd5 | 72 | }) |
bc0d801b C |
73 | const labelFormatter = (suffix?: string) => { |
74 | return winston.format.label({ | |
75 | label: suffix ? `${label} ${suffix}` : label | |
76 | }) | |
77 | } | |
23e27dd5 | 78 | |
fcf4569f | 79 | const fileLoggerOptions: FileTransportOptions = { |
566c125d | 80 | filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME), |
fcf4569f NB |
81 | handleExceptions: true, |
82 | format: winston.format.combine( | |
83 | winston.format.timestamp(), | |
84 | jsonLoggerFormat | |
85 | ) | |
86 | } | |
87 | ||
2f6b5e2d C |
88 | if (CONFIG.LOG.ROTATION.ENABLED) { |
89 | fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE | |
90 | fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES | |
fcf4569f NB |
91 | } |
92 | ||
bc0d801b C |
93 | const logger = buildLogger() |
94 | ||
95 | function buildLogger (labelSuffix?: string) { | |
96 | return winston.createLogger({ | |
97 | level: CONFIG.LOG.LEVEL, | |
98 | format: winston.format.combine( | |
99 | labelFormatter(labelSuffix), | |
100 | winston.format.splat() | |
101 | ), | |
102 | transports: [ | |
103 | new winston.transports.File(fileLoggerOptions), | |
104 | new winston.transports.Console({ | |
105 | handleExceptions: true, | |
106 | format: winston.format.combine( | |
107 | timestampFormatter, | |
108 | winston.format.colorize(), | |
109 | consoleLoggerFormat | |
110 | ) | |
111 | }) | |
112 | ], | |
113 | exitOnError: true | |
114 | }) | |
115 | } | |
8c308c2b | 116 | |
05e67d62 C |
117 | function bunyanLogFactory (level: string) { |
118 | return function () { | |
119 | let meta = null | |
c1e791ba RK |
120 | let args: any[] = [] |
121 | args.concat(arguments) | |
05e67d62 | 122 | |
a1587156 C |
123 | if (arguments[0] instanceof Error) { |
124 | meta = arguments[0].toString() | |
05e67d62 C |
125 | args = Array.prototype.slice.call(arguments, 1) |
126 | args.push(meta) | |
a1587156 C |
127 | } else if (typeof (args[0]) !== 'string') { |
128 | meta = arguments[0] | |
05e67d62 C |
129 | args = Array.prototype.slice.call(arguments, 1) |
130 | args.push(meta) | |
131 | } | |
132 | ||
a1587156 | 133 | logger[level].apply(logger, args) |
05e67d62 C |
134 | } |
135 | } | |
a1587156 | 136 | |
05e67d62 C |
137 | const bunyanLogger = { |
138 | trace: bunyanLogFactory('debug'), | |
139 | debug: bunyanLogFactory('debug'), | |
140 | info: bunyanLogFactory('info'), | |
141 | warn: bunyanLogFactory('warn'), | |
142 | error: bunyanLogFactory('error'), | |
143 | fatal: bunyanLogFactory('error') | |
144 | } | |
9f10b292 | 145 | // --------------------------------------------------------------------------- |
c45f7f84 | 146 | |
23e27dd5 | 147 | export { |
bc0d801b | 148 | buildLogger, |
23e27dd5 C |
149 | timestampFormatter, |
150 | labelFormatter, | |
276d03ed | 151 | consoleLoggerFormat, |
59390818 | 152 | jsonLoggerFormat, |
05e67d62 C |
153 | logger, |
154 | bunyanLogger | |
23e27dd5 | 155 | } |