1 // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2 import { mkdirpSync } from 'fs-extra'
3 import { omit } from 'lodash'
4 import * as path from 'path'
5 import { format as sqlFormat } from 'sql-formatter'
6 import * as winston from 'winston'
7 import { FileTransportOptions } from 'winston/lib/winston/transports'
8 import { CONFIG } from '../initializers/config'
9 import { LOG_FILENAME } from '../initializers/constants'
11 const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
13 // Create the directory if it does not exist
15 mkdirpSync(CONFIG.STORAGE.LOG_DIR)
17 function getLoggerReplacer () {
18 const seen = new WeakSet()
20 // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples
21 return (key: string, value: any) => {
22 if (key === 'cert') return 'Replaced by the logger to avoid large log message'
24 if (typeof value === 'object' && value !== null) {
25 if (seen.has(value)) return
30 if (value instanceof Error) {
33 Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
42 const consoleLoggerFormat = winston.format.printf(info => {
43 const toOmit = [ 'label', 'timestamp', 'level', 'message', 'sql' ]
45 const obj = omit(info, ...toOmit)
47 let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2)
49 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
50 else additionalInfos = ' ' + additionalInfos
53 if (CONFIG.LOG.PRETTIFY_SQL) {
54 additionalInfos += '\n' + sqlFormat(info.sql, {
59 additionalInfos += ' - ' + info.sql
63 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
66 const jsonLoggerFormat = winston.format.printf(info => {
67 return JSON.stringify(info, getLoggerReplacer())
70 const timestampFormatter = winston.format.timestamp({
71 format: 'YYYY-MM-DD HH:mm:ss.SSS'
73 const labelFormatter = (suffix?: string) => {
74 return winston.format.label({
75 label: suffix ? `${label} ${suffix}` : label
79 const fileLoggerOptions: FileTransportOptions = {
80 filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
81 handleExceptions: true,
82 format: winston.format.combine(
83 winston.format.timestamp(),
88 if (CONFIG.LOG.ROTATION.ENABLED) {
89 fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE
90 fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
93 const logger = buildLogger()
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()
103 new winston.transports.File(fileLoggerOptions),
104 new winston.transports.Console({
105 handleExceptions: true,
106 format: winston.format.combine(
108 winston.format.colorize(),
117 function bunyanLogFactory (level: string) {
121 args.concat(arguments)
123 if (arguments[0] instanceof Error) {
124 meta = arguments[0].toString()
125 args = Array.prototype.slice.call(arguments, 1)
127 } else if (typeof (args[0]) !== 'string') {
129 args = Array.prototype.slice.call(arguments, 1)
133 logger[level].apply(logger, args)
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')
145 // ---------------------------------------------------------------------------