]>
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' |
566c125d | 8 | import { LOG_FILENAME } from '@server/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 | ||
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 C |
56 | }) |
57 | const labelFormatter = winston.format.label({ | |
58 | label | |
59 | }) | |
60 | ||
fcf4569f | 61 | const fileLoggerOptions: FileTransportOptions = { |
566c125d | 62 | filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME), |
fcf4569f NB |
63 | handleExceptions: true, |
64 | format: winston.format.combine( | |
65 | winston.format.timestamp(), | |
66 | jsonLoggerFormat | |
67 | ) | |
68 | } | |
69 | ||
2f6b5e2d C |
70 | if (CONFIG.LOG.ROTATION.ENABLED) { |
71 | fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE | |
72 | fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES | |
fcf4569f NB |
73 | } |
74 | ||
85b4d9c5 | 75 | const logger = winston.createLogger({ |
23e27dd5 | 76 | level: CONFIG.LOG.LEVEL, |
e20015d7 C |
77 | format: winston.format.combine( |
78 | labelFormatter, | |
79 | winston.format.splat() | |
80 | ), | |
9f10b292 | 81 | transports: [ |
fcf4569f | 82 | new winston.transports.File(fileLoggerOptions), |
9f10b292 | 83 | new winston.transports.Console({ |
e8cb4409 | 84 | handleExceptions: true, |
23e27dd5 C |
85 | format: winston.format.combine( |
86 | timestampFormatter, | |
23e27dd5 | 87 | winston.format.colorize(), |
276d03ed | 88 | consoleLoggerFormat |
23e27dd5 | 89 | ) |
9f10b292 C |
90 | }) |
91 | ], | |
92 | exitOnError: true | |
93 | }) | |
8c308c2b | 94 | |
05e67d62 C |
95 | function bunyanLogFactory (level: string) { |
96 | return function () { | |
97 | let meta = null | |
c1e791ba RK |
98 | let args: any[] = [] |
99 | args.concat(arguments) | |
05e67d62 C |
100 | |
101 | if (arguments[ 0 ] instanceof Error) { | |
102 | meta = arguments[ 0 ].toString() | |
103 | args = Array.prototype.slice.call(arguments, 1) | |
104 | args.push(meta) | |
105 | } else if (typeof (args[ 0 ]) !== 'string') { | |
106 | meta = arguments[ 0 ] | |
107 | args = Array.prototype.slice.call(arguments, 1) | |
108 | args.push(meta) | |
109 | } | |
110 | ||
111 | logger[ level ].apply(logger, args) | |
112 | } | |
113 | } | |
114 | const bunyanLogger = { | |
115 | trace: bunyanLogFactory('debug'), | |
116 | debug: bunyanLogFactory('debug'), | |
117 | info: bunyanLogFactory('info'), | |
118 | warn: bunyanLogFactory('warn'), | |
119 | error: bunyanLogFactory('error'), | |
120 | fatal: bunyanLogFactory('error') | |
121 | } | |
9f10b292 | 122 | // --------------------------------------------------------------------------- |
c45f7f84 | 123 | |
23e27dd5 C |
124 | export { |
125 | timestampFormatter, | |
126 | labelFormatter, | |
276d03ed | 127 | consoleLoggerFormat, |
59390818 | 128 | jsonLoggerFormat, |
05e67d62 C |
129 | logger, |
130 | bunyanLogger | |
23e27dd5 | 131 | } |