aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/logger.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/logger.ts')
-rw-r--r--server/helpers/logger.ts66
1 files changed, 37 insertions, 29 deletions
diff --git a/server/helpers/logger.ts b/server/helpers/logger.ts
index 395417612..9553f70e8 100644
--- a/server/helpers/logger.ts
+++ b/server/helpers/logger.ts
@@ -5,7 +5,7 @@ import * as winston from 'winston'
5import { FileTransportOptions } from 'winston/lib/winston/transports' 5import { FileTransportOptions } from 'winston/lib/winston/transports'
6import { CONFIG } from '../initializers/config' 6import { CONFIG } from '../initializers/config'
7import { omit } from 'lodash' 7import { omit } from 'lodash'
8import { LOG_FILENAME } from '@server/initializers/constants' 8import { LOG_FILENAME } from '../initializers/constants'
9 9
10const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT 10const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
11 11
@@ -27,7 +27,7 @@ function getLoggerReplacer () {
27 if (value instanceof Error) { 27 if (value instanceof Error) {
28 const error = {} 28 const error = {}
29 29
30 Object.getOwnPropertyNames(value).forEach(key => error[ key ] = value[ key ]) 30 Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
31 31
32 return error 32 return error
33 } 33 }
@@ -54,9 +54,11 @@ const jsonLoggerFormat = winston.format.printf(info => {
54const timestampFormatter = winston.format.timestamp({ 54const timestampFormatter = winston.format.timestamp({
55 format: 'YYYY-MM-DD HH:mm:ss.SSS' 55 format: 'YYYY-MM-DD HH:mm:ss.SSS'
56}) 56})
57const labelFormatter = winston.format.label({ 57const labelFormatter = (suffix?: string) => {
58 label 58 return winston.format.label({
59}) 59 label: suffix ? `${label} ${suffix}` : label
60 })
61}
60 62
61const fileLoggerOptions: FileTransportOptions = { 63const fileLoggerOptions: FileTransportOptions = {
62 filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME), 64 filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
@@ -72,25 +74,29 @@ if (CONFIG.LOG.ROTATION.ENABLED) {
72 fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES 74 fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
73} 75}
74 76
75const logger = winston.createLogger({ 77const logger = buildLogger()
76 level: CONFIG.LOG.LEVEL, 78
77 format: winston.format.combine( 79function buildLogger (labelSuffix?: string) {
78 labelFormatter, 80 return winston.createLogger({
79 winston.format.splat() 81 level: CONFIG.LOG.LEVEL,
80 ), 82 format: winston.format.combine(
81 transports: [ 83 labelFormatter(labelSuffix),
82 new winston.transports.File(fileLoggerOptions), 84 winston.format.splat()
83 new winston.transports.Console({ 85 ),
84 handleExceptions: true, 86 transports: [
85 format: winston.format.combine( 87 new winston.transports.File(fileLoggerOptions),
86 timestampFormatter, 88 new winston.transports.Console({
87 winston.format.colorize(), 89 handleExceptions: true,
88 consoleLoggerFormat 90 format: winston.format.combine(
89 ) 91 timestampFormatter,
90 }) 92 winston.format.colorize(),
91 ], 93 consoleLoggerFormat
92 exitOnError: true 94 )
93}) 95 })
96 ],
97 exitOnError: true
98 })
99}
94 100
95function bunyanLogFactory (level: string) { 101function bunyanLogFactory (level: string) {
96 return function () { 102 return function () {
@@ -98,19 +104,20 @@ function bunyanLogFactory (level: string) {
98 let args: any[] = [] 104 let args: any[] = []
99 args.concat(arguments) 105 args.concat(arguments)
100 106
101 if (arguments[ 0 ] instanceof Error) { 107 if (arguments[0] instanceof Error) {
102 meta = arguments[ 0 ].toString() 108 meta = arguments[0].toString()
103 args = Array.prototype.slice.call(arguments, 1) 109 args = Array.prototype.slice.call(arguments, 1)
104 args.push(meta) 110 args.push(meta)
105 } else if (typeof (args[ 0 ]) !== 'string') { 111 } else if (typeof (args[0]) !== 'string') {
106 meta = arguments[ 0 ] 112 meta = arguments[0]
107 args = Array.prototype.slice.call(arguments, 1) 113 args = Array.prototype.slice.call(arguments, 1)
108 args.push(meta) 114 args.push(meta)
109 } 115 }
110 116
111 logger[ level ].apply(logger, args) 117 logger[level].apply(logger, args)
112 } 118 }
113} 119}
120
114const bunyanLogger = { 121const bunyanLogger = {
115 trace: bunyanLogFactory('debug'), 122 trace: bunyanLogFactory('debug'),
116 debug: bunyanLogFactory('debug'), 123 debug: bunyanLogFactory('debug'),
@@ -122,6 +129,7 @@ const bunyanLogger = {
122// --------------------------------------------------------------------------- 129// ---------------------------------------------------------------------------
123 130
124export { 131export {
132 buildLogger,
125 timestampFormatter, 133 timestampFormatter,
126 labelFormatter, 134 labelFormatter,
127 consoleLoggerFormat, 135 consoleLoggerFormat,