]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/logger.ts
Fix stats time metric
[github/Chocobozzz/PeerTube.git] / server / helpers / logger.ts
... / ...
CommitLineData
1// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2import { stat } from 'fs-extra'
3import { omit } from 'lodash'
4import { join } from 'path'
5import { format as sqlFormat } from 'sql-formatter'
6import { createLogger, format, transports } from 'winston'
7import { FileTransportOptions } from 'winston/lib/winston/transports'
8import { CONFIG } from '../initializers/config'
9import { LOG_FILENAME } from '../initializers/constants'
10
11const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
12
13function getLoggerReplacer () {
14 const seen = new WeakSet()
15
16 // Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#Examples
17 return (key: string, value: any) => {
18 if (key === 'cert') return 'Replaced by the logger to avoid large log message'
19
20 if (typeof value === 'object' && value !== null) {
21 if (seen.has(value)) return
22
23 seen.add(value)
24 }
25
26 if (value instanceof Set) {
27 return Array.from(value)
28 }
29
30 if (value instanceof Map) {
31 return Array.from(value.entries())
32 }
33
34 if (value instanceof Error) {
35 const error = {}
36
37 Object.getOwnPropertyNames(value).forEach(key => { error[key] = value[key] })
38
39 return error
40 }
41
42 return value
43 }
44}
45
46const consoleLoggerFormat = format.printf(info => {
47 const toOmit = [ 'label', 'timestamp', 'level', 'message', 'sql', 'tags' ]
48
49 const obj = omit(info, ...toOmit)
50
51 let additionalInfos = JSON.stringify(obj, getLoggerReplacer(), 2)
52
53 if (additionalInfos === undefined || additionalInfos === '{}') additionalInfos = ''
54 else additionalInfos = ' ' + additionalInfos
55
56 if (info.sql) {
57 if (CONFIG.LOG.PRETTIFY_SQL) {
58 additionalInfos += '\n' + sqlFormat(info.sql, {
59 language: 'sql',
60 indent: ' '
61 })
62 } else {
63 additionalInfos += ' - ' + info.sql
64 }
65 }
66
67 return `[${info.label}] ${info.timestamp} ${info.level}: ${info.message}${additionalInfos}`
68})
69
70const jsonLoggerFormat = format.printf(info => {
71 return JSON.stringify(info, getLoggerReplacer())
72})
73
74const timestampFormatter = format.timestamp({
75 format: 'YYYY-MM-DD HH:mm:ss.SSS'
76})
77const labelFormatter = (suffix?: string) => {
78 return format.label({
79 label: suffix ? `${label} ${suffix}` : label
80 })
81}
82
83const fileLoggerOptions: FileTransportOptions = {
84 filename: join(CONFIG.STORAGE.LOG_DIR, LOG_FILENAME),
85 handleExceptions: true,
86 format: format.combine(
87 format.timestamp(),
88 jsonLoggerFormat
89 )
90}
91
92if (CONFIG.LOG.ROTATION.ENABLED) {
93 fileLoggerOptions.maxsize = CONFIG.LOG.ROTATION.MAX_FILE_SIZE
94 fileLoggerOptions.maxFiles = CONFIG.LOG.ROTATION.MAX_FILES
95}
96
97const logger = buildLogger()
98
99function buildLogger (labelSuffix?: string) {
100 return createLogger({
101 level: CONFIG.LOG.LEVEL,
102 format: format.combine(
103 labelFormatter(labelSuffix),
104 format.splat()
105 ),
106 transports: [
107 new transports.File(fileLoggerOptions),
108 new transports.Console({
109 handleExceptions: true,
110 format: format.combine(
111 timestampFormatter,
112 format.colorize(),
113 consoleLoggerFormat
114 )
115 })
116 ],
117 exitOnError: true
118 })
119}
120
121function bunyanLogFactory (level: string) {
122 return function (...params: any[]) {
123 let meta = null
124 let args = [].concat(params)
125
126 if (arguments[0] instanceof Error) {
127 meta = arguments[0].toString()
128 args = Array.prototype.slice.call(arguments, 1)
129 args.push(meta)
130 } else if (typeof (args[0]) !== 'string') {
131 meta = arguments[0]
132 args = Array.prototype.slice.call(arguments, 1)
133 args.push(meta)
134 }
135
136 logger[level].apply(logger, args)
137 }
138}
139
140const bunyanLogger = {
141 level: () => { },
142 trace: bunyanLogFactory('debug'),
143 debug: bunyanLogFactory('debug'),
144 info: bunyanLogFactory('info'),
145 warn: bunyanLogFactory('warn'),
146 error: bunyanLogFactory('error'),
147 fatal: bunyanLogFactory('error')
148}
149
150type LoggerTagsFn = (...tags: string[]) => { tags: string[] }
151function loggerTagsFactory (...defaultTags: string[]): LoggerTagsFn {
152 return (...tags: string[]) => {
153 return { tags: defaultTags.concat(tags) }
154 }
155}
156
157async function mtimeSortFilesDesc (files: string[], basePath: string) {
158 const promises = []
159 const out: { file: string, mtime: number }[] = []
160
161 for (const file of files) {
162 const p = stat(basePath + '/' + file)
163 .then(stats => {
164 if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() })
165 })
166
167 promises.push(p)
168 }
169
170 await Promise.all(promises)
171
172 out.sort((a, b) => b.mtime - a.mtime)
173
174 return out
175}
176
177// ---------------------------------------------------------------------------
178
179export {
180 LoggerTagsFn,
181
182 buildLogger,
183 timestampFormatter,
184 labelFormatter,
185 consoleLoggerFormat,
186 jsonLoggerFormat,
187 mtimeSortFilesDesc,
188 logger,
189 loggerTagsFactory,
190 bunyanLogger
191}