Spaces:
Build error
Build error
import winston from 'winston'; | |
import { Settings } from './settings'; | |
// Map log levels to short labels and emojis | |
const levelMap: { [key: string]: string } = { | |
error: 'ERR', | |
warn: 'WRN', | |
info: 'INF', | |
debug: 'DBG', | |
}; | |
const moduleMap: { [key: string]: string } = { | |
server: 'π SERVER', | |
wrappers: 'π¦ WRAPPERS', | |
crypto: 'π CRYPTO', | |
addon: 'π§© ADDON', | |
parser: 'π PARSER', | |
mediaflow: 'π MEDIAFLOW', | |
stremthru: 'β¨ STREMTHRU', | |
cache: 'ποΈ CACHE', | |
regex: 'π °οΈ REGEX', | |
}; | |
// Define colors for each log level | |
const levelColors: { [key: string]: string } = { | |
ERR: 'red', | |
WRN: 'yellow', | |
INF: 'cyan', | |
DBG: 'magenta', | |
}; | |
const emojiLevelMap: { [key: string]: string } = { | |
error: 'β', | |
warn: 'β οΈ ', | |
info: 'π΅', | |
debug: 'π', | |
}; | |
// Apply colors to Winston | |
winston.addColors(levelColors); | |
export const createLogger = (module: string) => { | |
// cloudflare workers do not play nice with winston | |
// so we disable winston logs in cloudflare workers | |
try { | |
__dirname; | |
} catch (e) { | |
return console; | |
} | |
const isJsonFormat = Settings.LOG_FORMAT === 'json'; | |
return winston.createLogger({ | |
level: Settings.LOG_LEVEL || 'info', // Default to 'info' if not set | |
format: isJsonFormat | |
? winston.format.combine( | |
winston.format.timestamp(), | |
winston.format.json() // Use JSON format when LOG_FORMAT=json | |
) | |
: winston.format.combine( | |
winston.format.timestamp({ | |
format: 'YYYY-MM-DD HH:mm:ss.SSS', | |
}), | |
winston.format.printf(({ timestamp, level, message, func }) => { | |
const emoji = emojiLevelMap[level] || ''; | |
const formattedModule = moduleMap[module] || module; | |
level = levelMap[level] || level.toUpperCase(); | |
// Apply color to the level | |
const coloredLevel = winston.format | |
.colorize() | |
.colorize(level, `${level}`); | |
const formatLine = (line: unknown) => { | |
return `${emoji} | ${coloredLevel} | ${timestamp} | ${formattedModule} ${ | |
func ? ' (' + func + ')' : '' | |
} > ${line}`; | |
}; | |
if (typeof message === 'string') { | |
return message.split('\n').map(formatLine).join('\n'); | |
} | |
return formatLine(message); | |
}) | |
), | |
transports: [new winston.transports.Console()], | |
}); | |
}; | |
export function maskSensitiveInfo(message: string) { | |
if (Settings.LOG_SENSITIVE_INFO) { | |
return message; | |
} | |
return '<redacted>'; | |
} | |