diff options
Diffstat (limited to 'scripts/parse-log.ts')
-rwxr-xr-x | scripts/parse-log.ts | 58 |
1 files changed, 32 insertions, 26 deletions
diff --git a/scripts/parse-log.ts b/scripts/parse-log.ts index 86aaa7994..66a5b8719 100755 --- a/scripts/parse-log.ts +++ b/scripts/parse-log.ts | |||
@@ -1,10 +1,11 @@ | |||
1 | import * as program from 'commander' | 1 | import * as program from 'commander' |
2 | import { createReadStream, readdirSync, statSync } from 'fs-extra' | 2 | import { createReadStream, readdir } from 'fs-extra' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { createInterface } from 'readline' | 4 | import { createInterface } from 'readline' |
5 | import * as winston from 'winston' | 5 | import * as winston from 'winston' |
6 | import { labelFormatter } from '../server/helpers/logger' | 6 | import { labelFormatter } from '../server/helpers/logger' |
7 | import { CONFIG } from '../server/initializers/constants' | 7 | import { CONFIG } from '../server/initializers/constants' |
8 | import { mtimeSortFilesDesc } from '../shared/utils/logs/logs' | ||
8 | 9 | ||
9 | program | 10 | program |
10 | .option('-l, --level [level]', 'Level log (debug/info/warn/error)') | 11 | .option('-l, --level [level]', 'Level log (debug/info/warn/error)') |
@@ -52,42 +53,47 @@ const logLevels = { | |||
52 | debug: logger.debug.bind(logger) | 53 | debug: logger.debug.bind(logger) |
53 | } | 54 | } |
54 | 55 | ||
55 | const logFiles = readdirSync(CONFIG.STORAGE.LOG_DIR) | 56 | run() |
56 | const lastLogFile = getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR) | 57 | .then(() => process.exit(0)) |
58 | .catch(err => console.error(err)) | ||
57 | 59 | ||
58 | const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile) | 60 | function run () { |
59 | console.log('Opening %s.', path) | 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) | ||
60 | 64 | ||
61 | const rl = createInterface({ | 65 | const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile) |
62 | input: createReadStream(path) | 66 | console.log('Opening %s.', path) |
63 | }) | ||
64 | 67 | ||
65 | rl.on('line', line => { | 68 | const stream = createReadStream(path) |
66 | const log = JSON.parse(line) | ||
67 | // Don't know why but loggerFormat does not remove splat key | ||
68 | Object.assign(log, { splat: undefined }) | ||
69 | 69 | ||
70 | logLevels[log.level](log) | 70 | const rl = createInterface({ |
71 | }) | 71 | input: stream |
72 | }) | ||
72 | 73 | ||
73 | function toTimeFormat (time: string) { | 74 | rl.on('line', line => { |
74 | const timestamp = Date.parse(time) | 75 | const log = JSON.parse(line) |
76 | // Don't know why but loggerFormat does not remove splat key | ||
77 | Object.assign(log, { splat: undefined }) | ||
75 | 78 | ||
76 | if (isNaN(timestamp) === true) return 'Unknown date' | 79 | logLevels[ log.level ](log) |
80 | }) | ||
77 | 81 | ||
78 | return new Date(timestamp).toISOString() | 82 | stream.once('close', () => res()) |
83 | }) | ||
79 | } | 84 | } |
80 | 85 | ||
81 | // Thanks: https://stackoverflow.com/a/37014317 | 86 | // Thanks: https://stackoverflow.com/a/37014317 |
82 | function getNewestFile (files: string[], basePath: string) { | 87 | async function getNewestFile (files: string[], basePath: string) { |
83 | const out = [] | 88 | const sorted = await mtimeSortFilesDesc(files, basePath) |
84 | 89 | ||
85 | files.forEach(file => { | 90 | return (sorted.length > 0) ? sorted[ 0 ].file : '' |
86 | const stats = statSync(basePath + '/' + file) | 91 | } |
87 | if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() }) | 92 | |
88 | }) | 93 | function toTimeFormat (time: string) { |
94 | const timestamp = Date.parse(time) | ||
89 | 95 | ||
90 | out.sort((a, b) => b.mtime - a.mtime) | 96 | if (isNaN(timestamp) === true) return 'Unknown date' |
91 | 97 | ||
92 | return (out.length > 0) ? out[ 0 ].file : '' | 98 | return new Date(timestamp).toISOString() |
93 | } | 99 | } |