]>
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 | ||
13 | program | |
14 | .option('-l, --level [level]', 'Level log (debug/info/warn/error)') | |
15 | .parse(process.argv) | |
16 | ||
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 | ||
33 | return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}` | |
34 | }) | |
35 | ||
36 | const logger = winston.createLogger({ | |
37 | transports: [ | |
38 | new winston.transports.Console({ | |
39 | level: program['level'] || 'debug', | |
40 | stderrLevels: [], | |
41 | format: winston.format.combine( | |
42 | winston.format.splat(), | |
43 | labelFormatter(), | |
44 | winston.format.colorize(), | |
45 | loggerFormat | |
46 | ) | |
47 | }) | |
48 | ], | |
49 | exitOnError: true | |
50 | }) | |
51 | ||
52 | const logLevels = { | |
53 | error: logger.error.bind(logger), | |
54 | warn: logger.warn.bind(logger), | |
55 | info: logger.info.bind(logger), | |
56 | debug: logger.debug.bind(logger) | |
57 | } | |
58 | ||
59 | run() | |
60 | .then(() => process.exit(0)) | |
61 | .catch(err => console.error(err)) | |
62 | ||
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) | |
67 | ||
68 | const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile) | |
69 | console.log('Opening %s.', path) | |
70 | ||
71 | const stream = createReadStream(path) | |
72 | ||
73 | const rl = createInterface({ | |
74 | input: stream | |
75 | }) | |
76 | ||
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 }) | |
81 | ||
82 | logLevels[ log.level ](log) | |
83 | }) | |
84 | ||
85 | stream.once('close', () => res()) | |
86 | }) | |
87 | } | |
88 | ||
89 | // Thanks: https://stackoverflow.com/a/37014317 | |
90 | async function getNewestFile (files: string[], basePath: string) { | |
91 | const sorted = await mtimeSortFilesDesc(files, basePath) | |
92 | ||
93 | return (sorted.length > 0) ? sorted[ 0 ].file : '' | |
94 | } | |
95 | ||
96 | function toTimeFormat (time: string) { | |
97 | const timestamp = Date.parse(time) | |
98 | ||
99 | if (isNaN(timestamp) === true) return 'Unknown date' | |
100 | ||
101 | return new Date(timestamp).toISOString() | |
102 | } |