]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/logger.ts
allow public video privacy to be deleted in the web client
[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
1896bca0
C
30 if (value instanceof Set) {
31 return Array.from(value)
32 }
33
34 if (value instanceof Map) {
35 return Array.from(value.entries())
36 }
37
959dbbd7
C
38 if (value instanceof Error) {
39 const error = {}
40
a1587156 41 Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
d5b7d911 42
959dbbd7
C
43 return error
44 }
45
46 return value
47 }
23e27dd5
C
48}
49
276d03ed 50const consoleLoggerFormat = winston.format.printf(info => {
452b3bea 51 const toOmit = [ 'label', 'timestamp', 'level', 'message', 'sql', 'tags' ]
1e743faa
C
52
53 const obj = omit(info, ...toOmit)
328e607d 54
959dbbd7 55 let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2)
fd8710b8 56
e20015d7 57 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
2af4fa4d 58 else additionalInfos = ' ' + additionalInfos
23e27dd5 59
d223dca0
C
60 if (info.sql) {
61 if (CONFIG.LOG.PRETTIFY_SQL) {
62 additionalInfos += '\n' + sqlFormat(info.sql, {
63 language: 'sql',
ba5a8d89 64 indent: ' '
d223dca0
C
65 })
66 } else {
67 additionalInfos += ' - ' + info.sql
68 }
2a6cf69c
RK
69 }
70
2af4fa4d 71 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
23e27dd5
C
72})
73
e20015d7 74const jsonLoggerFormat = winston.format.printf(info => {
959dbbd7 75 return JSON.stringify(info, getLoggerReplacer())
276d03ed
C
76})
77
23e27dd5 78const timestampFormatter = winston.format.timestamp({
0647f472 79 format: 'YYYY-MM-DD HH:mm:ss.SSS'
23e27dd5 80})
bc0d801b
C
81const labelFormatter = (suffix?: string) => {
82 return winston.format.label({
83 label: suffix ? `${label} ${suffix}` : label
84 })
85}
23e27dd5 86
fcf4569f 87const fileLoggerOptions: FileTransportOptions = {
566c125d 88 filename: path.join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
fcf4569f
NB
89 handleExceptions: true,
90 format: winston.format.combine(
91 winston.format.timestamp(),
92 jsonLoggerFormat
93 )
94}
95
2f6b5e2d
C
96if (CONFIG.LOG.ROTATION.ENABLED) {
97 fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE
98 fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
fcf4569f
NB
99}
100
bc0d801b
C
101const logger = buildLogger()
102
103function buildLogger (labelSuffix?: string) {
104 return winston.createLogger({
105 level: CONFIG.LOG.LEVEL,
106 format: winston.format.combine(
107 labelFormatter(labelSuffix),
108 winston.format.splat()
109 ),
110 transports: [
111 new winston.transports.File(fileLoggerOptions),
112 new winston.transports.Console({
113 handleExceptions: true,
114 format: winston.format.combine(
115 timestampFormatter,
116 winston.format.colorize(),
117 consoleLoggerFormat
118 )
119 })
120 ],
121 exitOnError: true
122 })
123}
8c308c2b 124
05e67d62
C
125function bunyanLogFactory (level: string) {
126 return function () {
127 let meta = null
c1e791ba
RK
128 let args: any[] = []
129 args.concat(arguments)
05e67d62 130
a1587156
C
131 if (arguments[0] instanceof Error) {
132 meta = arguments[0].toString()
05e67d62
C
133 args = Array.prototype.slice.call(arguments, 1)
134 args.push(meta)
a1587156
C
135 } else if (typeof (args[0]) !== 'string') {
136 meta = arguments[0]
05e67d62
C
137 args = Array.prototype.slice.call(arguments, 1)
138 args.push(meta)
139 }
140
a1587156 141 logger[level].apply(logger, args)
05e67d62
C
142 }
143}
a1587156 144
05e67d62
C
145const bunyanLogger = {
146 trace: bunyanLogFactory('debug'),
147 debug: bunyanLogFactory('debug'),
148 info: bunyanLogFactory('info'),
149 warn: bunyanLogFactory('warn'),
150 error: bunyanLogFactory('error'),
151 fatal: bunyanLogFactory('error')
152}
452b3bea 153
46320694
C
154type LoggerTagsFn = (...tags: string[]) => { tags: string[] }
155function loggerTagsFactory (...defaultTags: string[]): LoggerTagsFn {
452b3bea
C
156 return (...tags: string[]) => {
157 return { tags: defaultTags.concat(tags) }
158 }
159}
160
9f10b292 161// ---------------------------------------------------------------------------
c45f7f84 162
23e27dd5 163export {
46320694
C
164 LoggerTagsFn,
165
bc0d801b 166 buildLogger,
23e27dd5
C
167 timestampFormatter,
168 labelFormatter,
276d03ed 169 consoleLoggerFormat,
59390818 170 jsonLoggerFormat,
05e67d62 171 logger,
452b3bea 172 loggerTagsFactory,
05e67d62 173 bunyanLogger
23e27dd5 174}