parse-log.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import * as program from 'commander'
  2. import { createReadStream, readdir } from 'fs-extra'
  3. import { join } from 'path'
  4. import { createInterface } from 'readline'
  5. import * as winston from 'winston'
  6. import { labelFormatter } from '../server/helpers/logger'
  7. import { CONFIG } from '../server/initializers/config'
  8. import { mtimeSortFilesDesc } from '../shared/core-utils/logs/logs'
  9. program
  10. .option('-l, --level [level]', 'Level log (debug/info/warn/error)')
  11. .parse(process.argv)
  12. const excludedKeys = {
  13. level: true,
  14. message: true,
  15. splat: true,
  16. timestamp: true,
  17. label: true
  18. }
  19. function keysExcluder (key, value) {
  20. return excludedKeys[key] === true ? undefined : value
  21. }
  22. const loggerFormat = winston.format.printf((info) => {
  23. let additionalInfos = JSON.stringify(info, keysExcluder, 2)
  24. if (additionalInfos === '{}') additionalInfos = ''
  25. else additionalInfos = ' ' + additionalInfos
  26. return `[${info.label}] ${toTimeFormat(info.timestamp)} ${info.level}: ${info.message}${additionalInfos}`
  27. })
  28. const logger = winston.createLogger({
  29. transports: [
  30. new winston.transports.Console({
  31. level: program['level'] || 'debug',
  32. stderrLevels: [],
  33. format: winston.format.combine(
  34. winston.format.splat(),
  35. labelFormatter,
  36. winston.format.colorize(),
  37. loggerFormat
  38. )
  39. })
  40. ],
  41. exitOnError: true
  42. })
  43. const logLevels = {
  44. error: logger.error.bind(logger),
  45. warn: logger.warn.bind(logger),
  46. info: logger.info.bind(logger),
  47. debug: logger.debug.bind(logger)
  48. }
  49. run()
  50. .then(() => process.exit(0))
  51. .catch(err => console.error(err))
  52. function run () {
  53. return new Promise(async res => {
  54. const logFiles = await readdir(CONFIG.STORAGE.LOG_DIR)
  55. const lastLogFile = await getNewestFile(logFiles, CONFIG.STORAGE.LOG_DIR)
  56. const path = join(CONFIG.STORAGE.LOG_DIR, lastLogFile)
  57. console.log('Opening %s.', path)
  58. const stream = createReadStream(path)
  59. const rl = createInterface({
  60. input: stream
  61. })
  62. rl.on('line', line => {
  63. const log = JSON.parse(line)
  64. // Don't know why but loggerFormat does not remove splat key
  65. Object.assign(log, { splat: undefined })
  66. logLevels[ log.level ](log)
  67. })
  68. stream.once('close', () => res())
  69. })
  70. }
  71. // Thanks: https://stackoverflow.com/a/37014317
  72. async function getNewestFile (files: string[], basePath: string) {
  73. const sorted = await mtimeSortFilesDesc(files, basePath)
  74. return (sorted.length > 0) ? sorted[ 0 ].file : ''
  75. }
  76. function toTimeFormat (time: string) {
  77. const timestamp = Date.parse(time)
  78. if (isNaN(timestamp) === true) return 'Unknown date'
  79. return new Date(timestamp).toISOString()
  80. }