HumanReadableLogs.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import LatexLogParser from '../log-parser/latex-log-parser'
  2. import ruleset from './HumanReadableLogsRules'
  3. export default {
  4. parse(rawLog, options) {
  5. let parsedLogEntries
  6. if (typeof rawLog === 'string') {
  7. const latexLogParser = new LatexLogParser(rawLog, options)
  8. parsedLogEntries = latexLogParser.parse()
  9. } else {
  10. parsedLogEntries = rawLog
  11. }
  12. const _getRule = function (logMessage) {
  13. for (const rule of ruleset) {
  14. if (rule.regexToMatch.test(logMessage)) {
  15. return rule
  16. }
  17. }
  18. }
  19. const seenErrorTypes = {} // keep track of types of errors seen
  20. for (const entry of parsedLogEntries.all) {
  21. const ruleDetails = _getRule(entry.message)
  22. if (ruleDetails != null) {
  23. let type
  24. if (ruleDetails.ruleId != null) {
  25. entry.ruleId = ruleDetails.ruleId
  26. }
  27. if (ruleDetails.newMessage != null) {
  28. entry.message = entry.message.replace(
  29. ruleDetails.regexToMatch,
  30. ruleDetails.newMessage
  31. )
  32. }
  33. if (ruleDetails.contentRegex) {
  34. const match = entry.content.match(ruleDetails.contentRegex)
  35. if (match) {
  36. entry.contentDetails = match.slice(1)
  37. }
  38. }
  39. if (entry.contentDetails && ruleDetails.improvedTitle) {
  40. entry.message = ruleDetails.improvedTitle(
  41. entry.message,
  42. entry.contentDetails
  43. )
  44. }
  45. // suppress any entries that are known to cascade from previous error types
  46. if (ruleDetails.cascadesFrom != null) {
  47. for (type of ruleDetails.cascadesFrom) {
  48. if (seenErrorTypes[type]) {
  49. entry.suppressed = true
  50. }
  51. }
  52. }
  53. // record the types of errors seen
  54. if (ruleDetails.types != null) {
  55. for (type of ruleDetails.types) {
  56. seenErrorTypes[type] = true
  57. }
  58. }
  59. }
  60. }
  61. // filter out the suppressed errors (from the array entries in parsedLogEntries)
  62. for (const key in parsedLogEntries) {
  63. const errors = parsedLogEntries[key]
  64. if (typeof errors === 'object' && errors.length > 0) {
  65. parsedLogEntries[key] = Array.from(errors).filter(
  66. err => !err.suppressed
  67. )
  68. }
  69. }
  70. return parsedLogEntries
  71. },
  72. }