sanitize relay log
This commit is contained in:
parent
efa3976657
commit
ca5f90be01
1 changed files with 14 additions and 1 deletions
|
|
@ -14,10 +14,23 @@ if (logLevel !== "DEBUG" && logLevel !== "WARN" && logLevel !== "ERROR") {
|
|||
throw new Error("Invalid log level " + logLevel + " must be one of (DEBUG, WARN, ERROR)")
|
||||
}
|
||||
const z = (n: number) => n < 10 ? `0${n}` : `${n}`
|
||||
// Sanitize filename to remove invalid characters for filesystem
|
||||
const sanitizeFileName = (fileName: string): string => {
|
||||
// Replace invalid filename characters with underscores
|
||||
// Invalid on most filesystems: / \ : * ? " < > |
|
||||
return fileName.replace(/[/\\:*?"<>|]/g, '_')
|
||||
}
|
||||
const openWriter = (fileName: string): Writer => {
|
||||
const now = new Date()
|
||||
const date = `${now.getFullYear()}-${z(now.getMonth() + 1)}-${z(now.getDate())}`
|
||||
const logStream = fs.createWriteStream(`${logsDir}/${fileName}_${date}.log`, { flags: 'a' });
|
||||
const sanitizedFileName = sanitizeFileName(fileName)
|
||||
const logPath = `${logsDir}/${sanitizedFileName}_${date}.log`
|
||||
// Ensure parent directory exists
|
||||
const dirPath = logPath.substring(0, logPath.lastIndexOf('/'))
|
||||
if (!fs.existsSync(dirPath)) {
|
||||
fs.mkdirSync(dirPath, { recursive: true })
|
||||
}
|
||||
const logStream = fs.createWriteStream(logPath, { flags: 'a' });
|
||||
return (message) => {
|
||||
logStream.write(message + "\n")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue