]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/logger.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / logger.ts
CommitLineData
9f10b292 1// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
c9d5c64f 2import { mkdirpSync } from 'fs-extra'
4d4e5cd4
C
3import * as path from 'path'
4import * as winston from 'winston'
fcf4569f 5import { FileTransportOptions } from 'winston/lib/winston/transports'
6dd9de95 6import { CONFIG } from '../initializers/config'
fd8710b8 7import { omit } from 'lodash'
837666fe 8import { LOG_FILENAME } from '../initializers/constants'
8c308c2b 9
65fcc311 10const 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 14mkdirpSync(CONFIG.STORAGE.LOG_DIR)
320d6275 15
959dbbd7
C
16function 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) => {
a9d4c3c8
C
21 if (key === 'cert') return 'Replaced by the logger to avoid large log message'
22
959dbbd7
C
23 if (typeof value === 'object' && value !== null) {
24 if (seen.has(value)) return
d5b7d911 25
959dbbd7
C
26 seen.add(value)
27 }
28
29 if (value instanceof Error) {
30 const error = {}
31
a1587156 32 Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
d5b7d911 33
959dbbd7
C
34 return error
35 }
36
37 return value
38 }
23e27dd5
C
39}
40
276d03ed 41const consoleLoggerFormat = winston.format.printf(info => {
fd8710b8 42 const obj = omit(info, 'label', 'timestamp', 'level', 'message')
328e607d 43
959dbbd7 44 let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2)
fd8710b8 45
e20015d7 46 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
2af4fa4d 47 else additionalInfos = ' ' + additionalInfos
23e27dd5 48
2af4fa4d 49 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
23e27dd5
C
50})
51
e20015d7 52const jsonLoggerFormat = winston.format.printf(info => {
959dbbd7 53 return JSON.stringify(info, getLoggerReplacer())
276d03ed
C
54})
55
23e27dd5 56const timestampFormatter = winston.format.timestamp({
0647f472 57 format: 'YYYY-MM-DD HH:mm:ss.SSS'
23e27dd5 58})
bc0d801b
C
59const labelFormatter = (suffix?: string) => {
60 return winston.format.label({
61 label: suffix ? `${label} ${suffix}` : label
62 })
63}
23e27dd5 64
fcf4569f 65const fileLoggerOptions: FileTransportOptions = {
566c125d 66 filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
fcf4569f
NB
67 handleExceptions: true,
68 format: winston.format.combine(
69 winston.format.timestamp(),
70 jsonLoggerFormat
71 )
72}
73
2f6b5e2d
C
74if (CONFIG.LOG.ROTATION.ENABLED) {
75 fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE
76 fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
fcf4569f
NB
77}
78
bc0d801b
C
79const logger = buildLogger()
80
81function buildLogger (labelSuffix?: string) {
82 return winston.createLogger({
83 level: CONFIG.LOG.LEVEL,
84 format: winston.format.combine(
85 labelFormatter(labelSuffix),
86 winston.format.splat()
87 ),
88 transports: [
89 new winston.transports.File(fileLoggerOptions),
90 new winston.transports.Console({
91 handleExceptions: true,
92 format: winston.format.combine(
93 timestampFormatter,
94 winston.format.colorize(),
95 consoleLoggerFormat
96 )
97 })
98 ],
99 exitOnError: true
100 })
101}
8c308c2b 102
05e67d62
C
103function bunyanLogFactory (level: string) {
104 return function () {
105 let meta = null
c1e791ba
RK
106 let args: any[] = []
107 args.concat(arguments)
05e67d62 108
a1587156
C
109 if (arguments[0] instanceof Error) {
110 meta = arguments[0].toString()
05e67d62
C
111 args = Array.prototype.slice.call(arguments, 1)
112 args.push(meta)
a1587156
C
113 } else if (typeof (args[0]) !== 'string') {
114 meta = arguments[0]
05e67d62
C
115 args = Array.prototype.slice.call(arguments, 1)
116 args.push(meta)
117 }
118
a1587156 119 logger[level].apply(logger, args)
05e67d62
C
120 }
121}
a1587156 122
05e67d62
C
123const 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')
130}
9f10b292 131// ---------------------------------------------------------------------------
c45f7f84 132
23e27dd5 133export {
bc0d801b 134 buildLogger,
23e27dd5
C
135 timestampFormatter,
136 labelFormatter,
276d03ed 137 consoleLoggerFormat,
59390818 138 jsonLoggerFormat,
05e67d62
C
139 logger,
140 bunyanLogger
23e27dd5 141}