parse-log.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { createReadStream } from 'fs'
  2. import { join } from 'path'
  3. import { createInterface } from 'readline'
  4. import * as winston from 'winston'
  5. import { CONFIG } from '../server/initializers/constants'
  6. const label = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
  7. const logger = new winston.Logger({
  8. transports: [
  9. new winston.transports.Console({
  10. level: 'debug',
  11. label: label,
  12. handleExceptions: true,
  13. humanReadableUnhandledException: true,
  14. json: false,
  15. colorize: true,
  16. prettyPrint: true,
  17. stderrLevels: []
  18. })
  19. ],
  20. exitOnError: true
  21. })
  22. const logLevels = {
  23. error: logger.error,
  24. warn: logger.warn,
  25. info: logger.info,
  26. debug: logger.debug
  27. }
  28. const path = join(CONFIG.STORAGE.LOG_DIR, 'all-logs.log')
  29. console.log('Opening %s.', path)
  30. const rl = createInterface({
  31. input: createReadStream(path)
  32. })
  33. rl.on('line', line => {
  34. const log = JSON.parse(line)
  35. const additionalInfo: any = {}
  36. Object.keys(log).forEach(logKey => {
  37. if (logKey !== 'message' && logKey !== 'level') additionalInfo[logKey] = log[logKey]
  38. })
  39. logLevels[log.level](log.message, additionalInfo)
  40. })