parse-log.ts 2.7 KB

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