Federated video streaming platform using ActivityPub and P2P in the web browser with Angular. https://joinpeertube.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

156 lines
4.0 KiB

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