]>
Commit | Line | Data |
---|---|---|
2aaa1a3f C |
1 | import { registerTSPaths } from '../server/helpers/register-ts-paths' |
2 | registerTSPaths() | |
3 | ||
3aa5cea8 | 4 | import * as program from 'commander' |
fd8710b8 | 5 | import { createReadStream, readdir } from 'fs-extra' |
41dbdb8a C |
6 | import { join } from 'path' |
7 | import { createInterface } from 'readline' | |
8 | import * as winston from 'winston' | |
94a5ff8a | 9 | import { labelFormatter } from '../server/helpers/logger' |
74dc3bca | 10 | import { CONFIG } from '../server/initializers/config' |
cda03765 | 11 | import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs' |
1e743faa | 12 | import { inspect } from 'util' |
cb5c2abc | 13 | import { format as sqlFormat } from 'sql-formatter' |
41dbdb8a | 14 | |
3aa5cea8 C |
15 | program |
16 | .option('-l, --level [level]', 'Level log (debug/info/warn/error)') | |
e0783718 | 17 | .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)') |
3aa5cea8 C |
18 | .parse(process.argv) |
19 | ||
ba5a8d89 C |
20 | const options = program.opts() |
21 | ||
94a5ff8a C |
22 | const excludedKeys = { |
23 | level: true, | |
24 | message: true, | |
25 | splat: true, | |
26 | timestamp: true, | |
cb5c2abc C |
27 | label: true, |
28 | sql: true | |
94a5ff8a C |
29 | } |
30 | function keysExcluder (key, value) { | |
31 | return excludedKeys[key] === true ? undefined : value | |
32 | } | |
33 | ||
34 | const loggerFormat = winston.format.printf((info) => { | |
35 | let additionalInfos = JSON.stringify(info, keysExcluder, 2) | |
36 | if (additionalInfos === '{}') additionalInfos = '' | |
37 | else additionalInfos = ' ' + additionalInfos | |
38 | ||
cb5c2abc C |
39 | if (info.sql) { |
40 | if (CONFIG.LOG.PRETTIFY_SQL) { | |
41 | additionalInfos += '\n' + sqlFormat(info.sql, { | |
42 | language: 'sql', | |
ba5a8d89 | 43 | indent: ' ' |
cb5c2abc C |
44 | }) |
45 | } else { | |
46 | additionalInfos += ' - ' + info.sql | |
47 | } | |
48 | } | |
49 | ||
0647f472 | 50 | return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}` |
94a5ff8a C |
51 | }) |
52 | ||
85b4d9c5 | 53 | const logger = winston.createLogger({ |
41dbdb8a C |
54 | transports: [ |
55 | new winston.transports.Console({ | |
ba5a8d89 | 56 | level: options.level || 'debug', |
23e27dd5 C |
57 | stderrLevels: [], |
58 | format: winston.format.combine( | |
23e27dd5 | 59 | winston.format.splat(), |
1b05d82d | 60 | labelFormatter(), |
23e27dd5 C |
61 | winston.format.colorize(), |
62 | loggerFormat | |
63 | ) | |
41dbdb8a C |
64 | }) |
65 | ], | |
66 | exitOnError: true | |
67 | }) | |
68 | ||
69 | const logLevels = { | |
23e27dd5 C |
70 | error: logger.error.bind(logger), |
71 | warn: logger.warn.bind(logger), | |
72 | info: logger.info.bind(logger), | |
73 | debug: logger.debug.bind(logger) | |
41dbdb8a C |
74 | } |
75 | ||
fd8710b8 C |
76 | run() |
77 | .then(() => process.exit(0)) | |
78 | .catch(err => console.error(err)) | |
85b4d9c5 | 79 | |
fd8710b8 | 80 | function run () { |
ba5a8d89 | 81 | return new Promise<void>(async res => { |
e0783718 | 82 | const files = await getFiles() |
41dbdb8a | 83 | |
e0783718 C |
84 | for (const file of files) { |
85 | console.log('Opening %s.', file) | |
41dbdb8a | 86 | |
e0783718 | 87 | const stream = createReadStream(file) |
afffe988 | 88 | |
e0783718 C |
89 | const rl = createInterface({ |
90 | input: stream | |
91 | }) | |
0647f472 | 92 | |
e0783718 | 93 | rl.on('line', line => { |
2b6c5552 C |
94 | try { |
95 | const log = JSON.parse(line) | |
96 | // Don't know why but loggerFormat does not remove splat key | |
97 | Object.assign(log, { splat: undefined }) | |
98 | ||
99 | logLevels[log.level](log) | |
100 | } catch (err) { | |
1e743faa | 101 | console.error('Cannot parse line.', inspect(line)) |
2b6c5552 C |
102 | throw err |
103 | } | |
e0783718 | 104 | }) |
0647f472 | 105 | |
e0783718 C |
106 | stream.once('close', () => res()) |
107 | } | |
fd8710b8 | 108 | }) |
0647f472 | 109 | } |
337ba64e C |
110 | |
111 | // Thanks: https://stackoverflow.com/a/37014317 | |
fd8710b8 C |
112 | async function getNewestFile (files: string[], basePath: string) { |
113 | const sorted = await mtimeSortFilesDesc(files, basePath) | |
337ba64e | 114 | |
f0af38e6 | 115 | return (sorted.length > 0) ? sorted[0].file : '' |
fd8710b8 C |
116 | } |
117 | ||
e0783718 | 118 | async function getFiles () { |
ba5a8d89 | 119 | if (options.files) return options.files |
e0783718 C |
120 | |
121 | const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR) | |
122 | ||
123 | const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR) | |
124 | return [ join(CONFIG.STORAGE.LOG_DIR, filename) ] | |
125 | } | |
126 | ||
fd8710b8 C |
127 | function toTimeFormat (time: string) { |
128 | const timestamp = Date.parse(time) | |
337ba64e | 129 | |
fd8710b8 | 130 | if (isNaN(timestamp) === true) return 'Unknown date' |
337ba64e | 131 | |
fd8710b8 | 132 | return new Date(timestamp).toISOString() |
e20015d7 | 133 | } |