]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/logger.ts
Disable sql prettifier by default
[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'
2a6cf69c 3import { omit } from 'lodash'
4d4e5cd4 4import * as path from 'path'
2a6cf69c 5import { format as sqlFormat } from 'sql-formatter'
4d4e5cd4 6import * as winston from 'winston'
fcf4569f 7import { FileTransportOptions } from 'winston/lib/winston/transports'
6dd9de95 8import { CONFIG } from '../initializers/config'
837666fe 9import { LOG_FILENAME } from '../initializers/constants'
8c308c2b 10
65fcc311 11const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
320d6275
C
12
13// Create the directory if it does not exist
fd8710b8 14// FIXME: use async
c9d5c64f 15mkdirpSync(CONFIG.STORAGE.LOG_DIR)
320d6275 16
959dbbd7
C
17function getLoggerReplacer () {
18 const seen = new WeakSet()
e20015d7 19
959dbbd7
C
20 // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples
21 return (key: string, value: any) => {
a9d4c3c8
C
22 if (key === 'cert') return 'Replaced by the logger to avoid large log message'
23
959dbbd7
C
24 if (typeof value === 'object' && value !== null) {
25 if (seen.has(value)) return
d5b7d911 26
959dbbd7
C
27 seen.add(value)
28 }
29
30 if (value instanceof Error) {
31 const error = {}
32
a1587156 33 Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
d5b7d911 34
959dbbd7
C
35 return error
36 }
37
38 return value
39 }
23e27dd5
C
40}
41
276d03ed 42const consoleLoggerFormat = winston.format.printf(info => {
1e743faa
C
43 const toOmit = [ 'label', 'timestamp', 'level', 'message' ]
44 if (CONFIG.LOG.PRETTIFY_SQL) toOmit.push('sql')
45
46 const obj = omit(info, ...toOmit)
328e607d 47
959dbbd7 48 let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2)
fd8710b8 49
e20015d7 50 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
2af4fa4d 51 else additionalInfos = ' ' + additionalInfos
23e27dd5 52
1e743faa 53 if (CONFIG.LOG.PRETTIFY_SQL && info.sql) {
2a6cf69c
RK
54 additionalInfos += '\n' + sqlFormat(info.sql, {
55 language: 'sql',
56 ident: ' '
57 })
58 }
59
2af4fa4d 60 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
23e27dd5
C
61})
62
e20015d7 63const jsonLoggerFormat = winston.format.printf(info => {
959dbbd7 64 return JSON.stringify(info, getLoggerReplacer())
276d03ed
C
65})
66
23e27dd5 67const timestampFormatter = winston.format.timestamp({
0647f472 68 format: 'YYYY-MM-DD HH:mm:ss.SSS'
23e27dd5 69})
bc0d801b
C
70const labelFormatter = (suffix?: string) => {
71 return winston.format.label({
72 label: suffix ? `${label} ${suffix}` : label
73 })
74}
23e27dd5 75
fcf4569f 76const fileLoggerOptions: FileTransportOptions = {
566c125d 77 filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
fcf4569f
NB
78 handleExceptions: true,
79 format: winston.format.combine(
80 winston.format.timestamp(),
81 jsonLoggerFormat
82 )
83}
84
2f6b5e2d
C
85if (CONFIG.LOG.ROTATION.ENABLED) {
86 fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE
87 fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
fcf4569f
NB
88}
89
bc0d801b
C
90const logger = buildLogger()
91
92function buildLogger (labelSuffix?: string) {
93 return winston.createLogger({
94 level: CONFIG.LOG.LEVEL,
95 format: winston.format.combine(
96 labelFormatter(labelSuffix),
97 winston.format.splat()
98 ),
99 transports: [
100 new winston.transports.File(fileLoggerOptions),
101 new winston.transports.Console({
102 handleExceptions: true,
103 format: winston.format.combine(
104 timestampFormatter,
105 winston.format.colorize(),
106 consoleLoggerFormat
107 )
108 })
109 ],
110 exitOnError: true
111 })
112}
8c308c2b 113
05e67d62
C
114function bunyanLogFactory (level: string) {
115 return function () {
116 let meta = null
c1e791ba
RK
117 let args: any[] = []
118 args.concat(arguments)
05e67d62 119
a1587156
C
120 if (arguments[0] instanceof Error) {
121 meta = arguments[0].toString()
05e67d62
C
122 args = Array.prototype.slice.call(arguments, 1)
123 args.push(meta)
a1587156
C
124 } else if (typeof (args[0]) !== 'string') {
125 meta = arguments[0]
05e67d62
C
126 args = Array.prototype.slice.call(arguments, 1)
127 args.push(meta)
128 }
129
a1587156 130 logger[level].apply(logger, args)
05e67d62
C
131 }
132}
a1587156 133
05e67d62
C
134const bunyanLogger = {
135 trace: bunyanLogFactory('debug'),
136 debug: bunyanLogFactory('debug'),
137 info: bunyanLogFactory('info'),
138 warn: bunyanLogFactory('warn'),
139 error: bunyanLogFactory('error'),
140 fatal: bunyanLogFactory('error')
141}
9f10b292 142// ---------------------------------------------------------------------------
c45f7f84 143
23e27dd5 144export {
bc0d801b 145 buildLogger,
23e27dd5
C
146 timestampFormatter,
147 labelFormatter,
276d03ed 148 consoleLoggerFormat,
59390818 149 jsonLoggerFormat,
05e67d62
C
150 logger,
151 bunyanLogger
23e27dd5 152}