66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
// Import XRegExp
|
|
import XRegExp from 'xregexp'
|
|
|
|
// Example matching function
|
|
const parseLog = (data: string) => {
|
|
// Define the regex components
|
|
const logPattern = XRegExp(
|
|
`
|
|
^\\*(?<month>\\w{3})\\s+
|
|
(?<day>\\d{1,2})\\s+
|
|
(?<time>\\d{2}:\\d{2}:\\d{2}\\.\\d{3}):\\s+
|
|
%(?<facility>\\w+)-(?<severity>\\d)-(?<mnemonic>\\w+):\\s+
|
|
(?<message>.+)
|
|
`,
|
|
'x'
|
|
)
|
|
|
|
const logPattern2 = XRegExp(
|
|
`
|
|
^(?<day>\\d{2})-(?<month>\\w{3})-(?<year>\\d{4})\\s+
|
|
(?<time>\\d{2}:\\d{2}:\\d{2})\\s+
|
|
:%(?<facility>\\w+)-(?<severity>[A-Z0-9])-(?<mnemonic>\\w+):\\s+
|
|
(?<message>.+)
|
|
`,
|
|
'x'
|
|
)
|
|
|
|
// Split log content into individual lines
|
|
// const logLines = data.split("\n").filter((line) => line.startsWith("*"));
|
|
const logLines = data.split('\n')
|
|
|
|
// Parse each log line
|
|
const logs = logLines
|
|
.map((line) => {
|
|
const match = XRegExp.exec(line, logPattern)
|
|
const match2 = XRegExp.exec(line, logPattern2)
|
|
if (match && match.groups) {
|
|
return {
|
|
month: match.groups.month,
|
|
day: match.groups.day,
|
|
time: match.groups.time,
|
|
facility: match.groups.facility,
|
|
severity: match.groups.severity,
|
|
mnemonic: match.groups.mnemonic,
|
|
message: match.groups.message?.trim() ?? '',
|
|
}
|
|
} else if (match2 && match2.groups) {
|
|
return {
|
|
month: match2.groups.month,
|
|
day: match2.groups.day,
|
|
time: match2.groups.time,
|
|
facility: match2.groups.facility,
|
|
severity: match2.groups.severity,
|
|
mnemonic: match2.groups.mnemonic,
|
|
message: match2.groups.message?.trim() ?? '',
|
|
}
|
|
}
|
|
return null // Ignore lines that do not match the pattern
|
|
})
|
|
.filter(Boolean) // Remove null entries
|
|
|
|
return logs
|
|
}
|
|
|
|
export default parseLog
|