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