]>
Commit | Line | Data |
---|---|---|
8cc61201 | 1 | import { program } from 'commander' |
fd8710b8 | 2 | import { createReadStream, readdir } from 'fs-extra' |
41dbdb8a | 3 | import { join } from 'path' |
045224d5 | 4 | import { stdin } from 'process' |
41dbdb8a | 5 | import { createInterface } from 'readline' |
f8360396 C |
6 | import { format as sqlFormat } from 'sql-formatter' |
7 | import { inspect } from 'util' | |
41dbdb8a | 8 | import * as winston from 'winston' |
15a7eafb | 9 | import { labelFormatter, mtimeSortFilesDesc } from '../server/helpers/logger' |
74dc3bca | 10 | import { CONFIG } from '../server/initializers/config' |
41dbdb8a | 11 | |
3aa5cea8 C |
12 | program |
13 | .option('-l, --level [level]', 'Level log (debug/info/warn/error)') | |
e0783718 | 14 | .option('-f, --files [file...]', 'Files to parse. If not provided, the script will parse the latest log file from config)') |
452b3bea C |
15 | .option('-t, --tags [tags...]', 'Display only lines with these tags') |
16 | .option('-nt, --not-tags [tags...]', 'Donrt display lines containing these tags') | |
3aa5cea8 C |
17 | .parse(process.argv) |
18 | ||
ba5a8d89 C |
19 | const options = program.opts() |
20 | ||
94a5ff8a C |
21 | const excludedKeys = { |
22 | level: true, | |
23 | message: true, | |
24 | splat: true, | |
25 | timestamp: true, | |
452b3bea | 26 | tags: true, |
cb5c2abc C |
27 | label: true, |
28 | sql: true | |
94a5ff8a C |
29 | } |
30 | function keysExcluder (key, value) { | |
31 | return excludedKeys[key] === true ? undefined : value | |
32 | } | |
33 | ||
34 | const loggerFormat = winston.format.printf((info) => { | |
35 | let additionalInfos = JSON.stringify(info, keysExcluder, 2) | |
36 | if (additionalInfos === '{}') additionalInfos = '' | |
37 | else additionalInfos = ' ' + additionalInfos | |
38 | ||
cb5c2abc C |
39 | if (info.sql) { |
40 | if (CONFIG.LOG.PRETTIFY_SQL) { | |
41 | additionalInfos += '\n' + sqlFormat(info.sql, { | |
42 | language: 'sql', | |
a91e9beb | 43 | tabWidth: 2 |
cb5c2abc C |
44 | }) |
45 | } else { | |
46 | additionalInfos += ' - ' + info.sql | |
47 | } | |
48 | } | |
49 | ||
0647f472 | 50 | return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}` |
94a5ff8a C |
51 | }) |
52 | ||
85b4d9c5 | 53 | const logger = winston.createLogger({ |
41dbdb8a C |
54 | transports: [ |
55 | new winston.transports.Console({ | |
ba5a8d89 | 56 | level: options.level || 'debug', |
23e27dd5 C |
57 | stderrLevels: [], |
58 | format: winston.format.combine( | |
23e27dd5 | 59 | winston.format.splat(), |
1b05d82d | 60 | labelFormatter(), |
23e27dd5 C |
61 | winston.format.colorize(), |
62 | loggerFormat | |
63 | ) | |
41dbdb8a C |
64 | }) |
65 | ], | |
66 | exitOnError: true | |
67 | }) | |
68 | ||
69 | const logLevels = { | |
23e27dd5 C |
70 | error: logger.error.bind(logger), |
71 | warn: logger.warn.bind(logger), | |
72 | info: logger.info.bind(logger), | |
73 | debug: logger.debug.bind(logger) | |
41dbdb8a C |
74 | } |
75 | ||
fd8710b8 C |
76 | run() |
77 | .then(() => process.exit(0)) | |
78 | .catch(err => console.error(err)) | |
85b4d9c5 | 79 | |
52208599 C |
80 | async function run () { |
81 | const files = await getFiles() | |
41dbdb8a | 82 | |
52208599 C |
83 | for (const file of files) { |
84 | if (file === 'peertube-audit.log') continue | |
8ebf2a5d | 85 | |
52208599 C |
86 | await readFile(file) |
87 | } | |
88 | } | |
afffe988 | 89 | |
52208599 C |
90 | function readFile (file: string) { |
91 | console.log('Opening %s.', file) | |
0647f472 | 92 | |
045224d5 | 93 | const stream = file === '-' ? stdin : createReadStream(file) |
452b3bea | 94 | |
52208599 C |
95 | const rl = createInterface({ |
96 | input: stream | |
97 | }) | |
452b3bea | 98 | |
52208599 C |
99 | return new Promise<void>(res => { |
100 | rl.on('line', line => { | |
101 | try { | |
102 | const log = JSON.parse(line) | |
103 | if (options.tags && !containsTags(log.tags, options.tags)) { | |
104 | return | |
105 | } | |
2b6c5552 | 106 | |
52208599 C |
107 | if (options.notTags && containsTags(log.tags, options.notTags)) { |
108 | return | |
2b6c5552 | 109 | } |
0647f472 | 110 | |
52208599 C |
111 | // Don't know why but loggerFormat does not remove splat key |
112 | Object.assign(log, { splat: undefined }) | |
113 | ||
114 | logLevels[log.level](log) | |
115 | } catch (err) { | |
116 | console.error('Cannot parse line.', inspect(line)) | |
117 | throw err | |
118 | } | |
119 | }) | |
120 | ||
045224d5 | 121 | stream.once('end', () => res()) |
fd8710b8 | 122 | }) |
0647f472 | 123 | } |
337ba64e C |
124 | |
125 | // Thanks: https://stackoverflow.com/a/37014317 | |
fd8710b8 C |
126 | async function getNewestFile (files: string[], basePath: string) { |
127 | const sorted = await mtimeSortFilesDesc(files, basePath) | |
337ba64e | 128 | |
f0af38e6 | 129 | return (sorted.length > 0) ? sorted[0].file : '' |
fd8710b8 C |
130 | } |
131 | ||
e0783718 | 132 | async function getFiles () { |
ba5a8d89 | 133 | if (options.files) return options.files |
e0783718 C |
134 | |
135 | const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR) | |
136 | ||
137 | const filename = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR) | |
138 | return [ join(CONFIG.STORAGE.LOG_DIR, filename) ] | |
139 | } | |
140 | ||
fd8710b8 C |
141 | function toTimeFormat (time: string) { |
142 | const timestamp = Date.parse(time) | |
337ba64e | 143 | |
fd8710b8 | 144 | if (isNaN(timestamp) === true) return 'Unknown date' |
337ba64e | 145 | |
ed76401e C |
146 | const d = new Date(timestamp) |
147 | return d.toLocaleString() + `.${d.getMilliseconds()}` | |
e20015d7 | 148 | } |
452b3bea C |
149 | |
150 | function containsTags (loggerTags: string[], optionsTags: string[]) { | |
151 | if (!loggerTags) return false | |
152 | ||
153 | for (const lt of loggerTags) { | |
154 | for (const ot of optionsTags) { | |
155 | if (lt === ot) return true | |
156 | } | |
157 | } | |
158 | ||
159 | return false | |
160 | } |