aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-04-10 15:26:33 +0200
committerChocobozzz <me@florianbigard.com>2019-04-10 16:38:32 +0200
commitfd8710b897a67518d3a61c319e54b6a65ba443ef (patch)
treed9953b7e0bb4e5a119c872ab21021f4c1ab33bea /scripts
parent31b6ddf86652502e0c96d77fa10861ce4af11aa4 (diff)
downloadPeerTube-fd8710b897a67518d3a61c319e54b6a65ba443ef.tar.gz
PeerTube-fd8710b897a67518d3a61c319e54b6a65ba443ef.tar.zst
PeerTube-fd8710b897a67518d3a61c319e54b6a65ba443ef.zip
Add logs endpoint
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/parse-log.ts58
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 @@
1import * as program from 'commander' 1import * as program from 'commander'
2import { createReadStream, readdirSync, statSync } from 'fs-extra' 2import { createReadStream, readdir } from 'fs-extra'
3import { join } from 'path' 3import { join } from 'path'
4import { createInterface } from 'readline' 4import { createInterface } from 'readline'
5import * as winston from 'winston' 5import * as winston from 'winston'
6import { labelFormatter } from '../server/helpers/logger' 6import { labelFormatter } from '../server/helpers/logger'
7import { CONFIG } from '../server/initializers/constants' 7import { CONFIG } from '../server/initializers/constants'
8import { mtimeSortFilesDesc } from '../shared/utils/logs/logs'
8 9
9program 10program
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
55const logFiles = readdirSync(CONFIG.STORAGE.LOG_DIR) 56run()
56const lastLogFile = getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR) 57 .then(() => process.exit(0))
58 .catch(err => console.error(err))
57 59
58const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile) 60function run () {
59console.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
61const rl = createInterface({ 65 const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile)
62 input: createReadStream(path) 66 console.log('Opening %s.', path)
63})
64 67
65rl.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
73function 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
82function getNewestFile (files: string[], basePath: string) { 87async 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 }) 93function 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}