1 import { createReadStream } from 'fs'
2 import { join } from 'path'
3 import { createInterface } from 'readline'
4 import * as winston from 'winston'
5 import { labelFormatter } from '../server/helpers/logger'
6 import { CONFIG } from '../server/initializers/constants'
15 function keysExcluder (key, value) {
16 return excludedKeys[key] === true ? undefined : value
19 const loggerFormat = winston.format.printf((info) => {
20 let additionalInfos = JSON.stringify(info, keysExcluder, 2)
21 if (additionalInfos === '{}') additionalInfos = ''
22 else additionalInfos = ' ' + additionalInfos
24 return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
27 const logger = new winston.createLogger({
29 new winston.transports.Console({
32 format: winston.format.combine(
33 winston.format.splat(),
35 winston.format.colorize(),
44 error: logger.error.bind(logger),
45 warn: logger.warn.bind(logger),
46 info: logger.info.bind(logger),
47 debug: logger.debug.bind(logger)
50 const path = join(CONFIG.STORAGE.LOG_DIR, 'peertube.log')
51 console.log('Opening %s.', path)
53 const rl = createInterface({
54 input: createReadStream(path)
57 rl.on('line', line => {
58 const log = JSON.parse(line)
59 // Don't know why but loggerFormat does not remove splat key
60 Object.assign(log, { splat: undefined })
62 logLevels[log.level](log)
65 function toTimeFormat (time: string) {
66 const timestamp = Date.parse(time)
68 if (isNaN(timestamp) === true) return 'Unknown date'
70 return new Date(timestamp).toISOString()