Implement first-pass load reduction controls
This commit is contained in:
parent
5d488fd7f5
commit
e7f4805ccc
17 changed files with 1191 additions and 608 deletions
|
|
@ -22,20 +22,46 @@ export type LoggerOptions = {
|
|||
service: string;
|
||||
now?: () => string;
|
||||
sink?: (record: LogRecord) => void;
|
||||
level?: LogLevel;
|
||||
};
|
||||
|
||||
const defaultSink = (record: LogRecord) => {
|
||||
console.log(JSON.stringify(record));
|
||||
};
|
||||
|
||||
const LOG_LEVEL_ORDER: Record<LogLevel, number> = {
|
||||
debug: 10,
|
||||
info: 20,
|
||||
warn: 30,
|
||||
error: 40
|
||||
};
|
||||
|
||||
const resolveLogLevel = (value: string | undefined): LogLevel => {
|
||||
switch ((value ?? "").trim().toLowerCase()) {
|
||||
case "debug":
|
||||
case "info":
|
||||
case "warn":
|
||||
case "error":
|
||||
return value!.trim().toLowerCase() as LogLevel;
|
||||
default:
|
||||
return "info";
|
||||
}
|
||||
};
|
||||
|
||||
export const createLogger = ({
|
||||
service,
|
||||
now = () => new Date().toISOString(),
|
||||
sink = defaultSink
|
||||
sink = defaultSink,
|
||||
level = resolveLogLevel(process.env.LOG_LEVEL)
|
||||
}: LoggerOptions): Logger => {
|
||||
const write = (level: LogLevel, msg: string, context?: LogContext) => {
|
||||
const levelThreshold = resolveLogLevel(level);
|
||||
|
||||
const write = (recordLevel: LogLevel, msg: string, context?: LogContext) => {
|
||||
if (LOG_LEVEL_ORDER[recordLevel] < LOG_LEVEL_ORDER[levelThreshold]) {
|
||||
return;
|
||||
}
|
||||
const record: LogRecord = {
|
||||
level,
|
||||
level: recordLevel,
|
||||
service,
|
||||
msg,
|
||||
ts: now(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue