1 // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2 import { mkdirpSync } from 'fs-extra'
3 import * as path from 'path'
4 import * as winston from 'winston'
5 import { FileTransportOptions } from 'winston/lib/winston/transports'
6 import { CONFIG } from '../initializers/config'
7 import { omit } from 'lodash'
8 import { LOG_FILENAME } from '../initializers/constants'
10 const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
12 // Create the directory if it does not exist
14 mkdirpSync(CONFIG.STORAGE.LOG_DIR)
16 function getLoggerReplacer () {
17 const seen = new WeakSet()
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 (key === 'cert') return 'Replaced by the logger to avoid large log message'
23 if (typeof value === 'object' && value !== null) {
24 if (seen.has(value)) return
29 if (value instanceof Error) {
32 Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
41 const consoleLoggerFormat = winston.format.printf(info => {
42 const obj = omit(info, 'label', 'timestamp', 'level', 'message')
44 let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2)
46 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
47 else additionalInfos = ' ' + additionalInfos
49 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
52 const jsonLoggerFormat = winston.format.printf(info => {
53 return JSON.stringify(info, getLoggerReplacer())
56 const timestampFormatter = winston.format.timestamp({
57 format: 'YYYY-MM-DD HH:mm:ss.SSS'
59 const labelFormatter = (suffix?: string) => {
60 return winston.format.label({
61 label: suffix ? `${label} ${suffix}` : label
65 const fileLoggerOptions: FileTransportOptions = {
66 filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
67 handleExceptions: true,
68 format: winston.format.combine(
69 winston.format.timestamp(),
74 if (CONFIG.LOG.ROTATION.ENABLED) {
75 fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE
76 fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
79 const logger = buildLogger()
81 function buildLogger (labelSuffix?: string) {
82 return winston.createLogger({
83 level: CONFIG.LOG.LEVEL,
84 format: winston.format.combine(
85 labelFormatter(labelSuffix),
86 winston.format.splat()
89 new winston.transports.File(fileLoggerOptions),
90 new winston.transports.Console({
91 handleExceptions: true,
92 format: winston.format.combine(
94 winston.format.colorize(),
103 function bunyanLogFactory (level: string) {
107 args.concat(arguments)
109 if (arguments[0] instanceof Error) {
110 meta = arguments[0].toString()
111 args = Array.prototype.slice.call(arguments, 1)
113 } else if (typeof (args[0]) !== 'string') {
115 args = Array.prototype.slice.call(arguments, 1)
119 logger[level].apply(logger, args)
123 const bunyanLogger = {
124 trace: bunyanLogFactory('debug'),
125 debug: bunyanLogFactory('debug'),
126 info: bunyanLogFactory('info'),
127 warn: bunyanLogFactory('warn'),
128 error: bunyanLogFactory('error'),
129 fatal: bunyanLogFactory('error')
131 // ---------------------------------------------------------------------------