]>
Commit | Line | Data |
---|---|---|
1 | import { registerTSPaths } from '../server/helpers/register-ts-paths' | |
2 | registerTSPaths() | |
3 | ||
4 | import * as program from 'commander' | |
5 | import { createReadStream, readdir } from 'fs-extra' | |
6 | import { join } from 'path' | |
7 | import { createInterface } from 'readline' | |
8 | import * as winston from 'winston' | |
9 | import { labelFormatter } from '../server/helpers/logger' | |
10 | import { CONFIG } from '../server/initializers/config' | |
11 | import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs' | |
12 | import { inspect } from 'util' | |
13 | import { format as sqlFormat } from 'sql-formatter' | |
14 | ||
15 | program | |
16 | .option('-l, --level [level]', 'Level log (debug/info/warn/error)') | |
17 | .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)') | |
18 | .parse(process.argv) | |
19 | ||
20 | const options = program.opts() | |
21 | ||
22 | const excludedKeys = { | |
23 | level: true, | |
24 | message: true, | |
25 | splat: true, | |
26 | timestamp: true, | |
27 | label: true, | |
28 | sql: true | |
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 | ||
39 | if (info.sql) { | |
40 | if (CONFIG.LOG.PRETTIFY_SQL) { | |
41 | additionalInfos += '\n' + sqlFormat(info.sql, { | |
42 | language: 'sql', | |
43 | indent: ' ' | |
44 | }) | |
45 | } else { | |
46 | additionalInfos += ' - ' + info.sql | |
47 | } | |
48 | } | |
49 | ||
50 | return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}` | |
51 | }) | |
52 | ||
53 | const logger = winston.createLogger({ | |
54 | transports: [ | |
55 | new winston.transports.Console({ | |
56 | level: options.level || 'debug', | |
57 | stderrLevels: [], | |
58 | format: winston.format.combine( | |
59 | winston.format.splat(), | |
60 | labelFormatter(), | |
61 | winston.format.colorize(), | |
62 | loggerFormat | |
63 | ) | |
64 | }) | |
65 | ], | |
66 | exitOnError: true | |
67 | }) | |
68 | ||
69 | const logLevels = { | |
70 | error: logger.error.bind(logger), | |
71 | warn: logger.warn.bind(logger), | |
72 | info: logger.info.bind(logger), | |
73 | debug: logger.debug.bind(logger) | |
74 | } | |
75 | ||
76 | run() | |
77 | .then(() => process.exit(0)) | |
78 | .catch(err => console.error(err)) | |
79 | ||
80 | function run () { | |
81 | return new Promise<void>(async res => { | |
82 | const files = await getFiles() | |
83 | ||
84 | for (const file of files) { | |
85 | console.log('Opening %s.', file) | |
86 | ||
87 | const stream = createReadStream(file) | |
88 | ||
89 | const rl = createInterface({ | |
90 | input: stream | |
91 | }) | |
92 | ||
93 | rl.on('line', line => { | |
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) { | |
101 | console.error('Cannot parse line.', inspect(line)) | |
102 | throw err | |
103 | } | |
104 | }) | |
105 | ||
106 | stream.once('close', () => res()) | |
107 | } | |
108 | }) | |
109 | } | |
110 | ||
111 | // Thanks: https://stackoverflow.com/a/37014317 | |
112 | async function getNewestFile (files: string[], basePath: string) { | |
113 | const sorted = await mtimeSortFilesDesc(files, basePath) | |
114 | ||
115 | return (sorted.length > 0) ? sorted[0].file : '' | |
116 | } | |
117 | ||
118 | async function getFiles () { | |
119 | if (options.files) return options.files | |
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 | ||
127 | function toTimeFormat (time: string) { | |
128 | const timestamp = Date.parse(time) | |
129 | ||
130 | if (isNaN(timestamp) === true) return 'Unknown date' | |
131 | ||
132 | return new Date(timestamp).toISOString() | |
133 | } |