]>
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' |
41dbdb8a | 12 | |
3aa5cea8 C |
13 | program |
14 | .option('-l, --level [level]', 'Level log (debug/info/warn/error)') | |
15 | .parse(process.argv) | |
16 | ||
94a5ff8a C |
17 | const excludedKeys = { |
18 | level: true, | |
19 | message: true, | |
20 | splat: true, | |
21 | timestamp: true, | |
22 | label: true | |
23 | } | |
24 | function keysExcluder (key, value) { | |
25 | return excludedKeys[key] === true ? undefined : value | |
26 | } | |
27 | ||
28 | const loggerFormat = winston.format.printf((info) => { | |
29 | let additionalInfos = JSON.stringify(info, keysExcluder, 2) | |
30 | if (additionalInfos === '{}') additionalInfos = '' | |
31 | else additionalInfos = ' ' + additionalInfos | |
32 | ||
0647f472 | 33 | return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}` |
94a5ff8a C |
34 | }) |
35 | ||
85b4d9c5 | 36 | const logger = winston.createLogger({ |
41dbdb8a C |
37 | transports: [ |
38 | new winston.transports.Console({ | |
3aa5cea8 | 39 | level: program['level'] || 'debug', |
23e27dd5 C |
40 | stderrLevels: [], |
41 | format: winston.format.combine( | |
23e27dd5 C |
42 | winston.format.splat(), |
43 | labelFormatter, | |
44 | winston.format.colorize(), | |
45 | loggerFormat | |
46 | ) | |
41dbdb8a C |
47 | }) |
48 | ], | |
49 | exitOnError: true | |
50 | }) | |
51 | ||
52 | const logLevels = { | |
23e27dd5 C |
53 | error: logger.error.bind(logger), |
54 | warn: logger.warn.bind(logger), | |
55 | info: logger.info.bind(logger), | |
56 | debug: logger.debug.bind(logger) | |
41dbdb8a C |
57 | } |
58 | ||
fd8710b8 C |
59 | run() |
60 | .then(() => process.exit(0)) | |
61 | .catch(err => console.error(err)) | |
85b4d9c5 | 62 | |
fd8710b8 C |
63 | function run () { |
64 | return new Promise(async res => { | |
65 | const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR) | |
66 | const lastLogFile = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR) | |
41dbdb8a | 67 | |
fd8710b8 C |
68 | const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile) |
69 | console.log('Opening %s.', path) | |
41dbdb8a | 70 | |
fd8710b8 | 71 | const stream = createReadStream(path) |
afffe988 | 72 | |
fd8710b8 C |
73 | const rl = createInterface({ |
74 | input: stream | |
75 | }) | |
0647f472 | 76 | |
fd8710b8 C |
77 | rl.on('line', line => { |
78 | const log = JSON.parse(line) | |
79 | // Don't know why but loggerFormat does not remove splat key | |
80 | Object.assign(log, { splat: undefined }) | |
0647f472 | 81 | |
fd8710b8 C |
82 | logLevels[ log.level ](log) |
83 | }) | |
0647f472 | 84 | |
fd8710b8 C |
85 | stream.once('close', () => res()) |
86 | }) | |
0647f472 | 87 | } |
337ba64e C |
88 | |
89 | // Thanks: https://stackoverflow.com/a/37014317 | |
fd8710b8 C |
90 | async function getNewestFile (files: string[], basePath: string) { |
91 | const sorted = await mtimeSortFilesDesc(files, basePath) | |
337ba64e | 92 | |
fd8710b8 C |
93 | return (sorted.length > 0) ? sorted[ 0 ].file : '' |
94 | } | |
95 | ||
96 | function toTimeFormat (time: string) { | |
97 | const timestamp = Date.parse(time) | |
337ba64e | 98 | |
fd8710b8 | 99 | if (isNaN(timestamp) === true) return 'Unknown date' |
337ba64e | 100 | |
fd8710b8 | 101 | return new Date(timestamp).toISOString() |
e20015d7 | 102 | } |