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