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