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 (typeof value === 'object' && value !== null) {
22 if (seen.has(value)) return
27 if (value instanceof Error) {
30 Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
39 const consoleLoggerFormat = winston.format.printf(info => {
40 const obj = omit(info, 'label', 'timestamp', 'level', 'message')
42 let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2)
44 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
45 else additionalInfos = ' ' + additionalInfos
47 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
50 const jsonLoggerFormat = winston.format.printf(info => {
51 return JSON.stringify(info, getLoggerReplacer())
54 const timestampFormatter = winston.format.timestamp({
55 format: 'YYYY-MM-DD HH:mm:ss.SSS'
57 const labelFormatter = (suffix?: string) => {
58 return winston.format.label({
59 label: suffix ? `${label} ${suffix}` : label
63 const fileLoggerOptions: FileTransportOptions = {
64 filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
65 handleExceptions: true,
66 format: winston.format.combine(
67 winston.format.timestamp(),
72 if (CONFIG.LOG.ROTATION.ENABLED) {
73 fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE
74 fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
77 const logger = buildLogger()
79 function buildLogger (labelSuffix?: string) {
80 return winston.createLogger({
81 level: CONFIG.LOG.LEVEL,
82 format: winston.format.combine(
83 labelFormatter(labelSuffix),
84 winston.format.splat()
87 new winston.transports.File(fileLoggerOptions),
88 new winston.transports.Console({
89 handleExceptions: true,
90 format: winston.format.combine(
92 winston.format.colorize(),
101 function bunyanLogFactory (level: string) {
105 args.concat(arguments)
107 if (arguments[0] instanceof Error) {
108 meta = arguments[0].toString()
109 args = Array.prototype.slice.call(arguments, 1)
111 } else if (typeof (args[0]) !== 'string') {
113 args = Array.prototype.slice.call(arguments, 1)
117 logger[level].apply(logger, args)
121 const bunyanLogger = {
122 trace: bunyanLogFactory('debug'),
123 debug: bunyanLogFactory('debug'),
124 info: bunyanLogFactory('info'),
125 warn: bunyanLogFactory('warn'),
126 error: bunyanLogFactory('error'),
127 fatal: bunyanLogFactory('error')
129 // ---------------------------------------------------------------------------