Contain a throw from the application's logger instead of letting it escape
This commit is contained in:
+23
@@ -20,3 +20,26 @@ export const silentLog: SmppLog = {
|
||||
verbose: noop,
|
||||
warn: noop,
|
||||
};
|
||||
|
||||
function contained(log: SmppLog, method: keyof SmppLog): LogMethod {
|
||||
return (msg, metadata) => {
|
||||
try {
|
||||
log[method](msg, metadata);
|
||||
} catch {
|
||||
// Reporting a broken logger would need a logger, and hard rule 1 outranks the report.
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** The application's logger with a throw from it contained, or silence when it supplied none. */
|
||||
export function guardedLog(log?: SmppLog): SmppLog {
|
||||
if (log === undefined) return silentLog;
|
||||
|
||||
return {
|
||||
debug: contained(log, 'debug'),
|
||||
error: contained(log, 'error'),
|
||||
info: contained(log, 'info'),
|
||||
verbose: contained(log, 'verbose'),
|
||||
warn: contained(log, 'warn'),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user