fix(logger): output error cause info (#3760)

* fix(logger): output error cause info

* fix: typo
This commit is contained in:
YANG QIA 2024-03-20 09:50:04 +08:00 committed by GitHub
parent 5153ce9ab2
commit 751bd73175
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -49,13 +49,13 @@ class SystemLoggerTransport extends Transport {
} }
log(info: any, callback: any) { log(info: any, callback: any) {
const { level, message, reqId, app, stack, [SPLAT]: args } = info; const { level, message, reqId, app, stack, cause, [SPLAT]: args } = info;
const logger = level === 'error' && this.errorLogger ? this.errorLogger : this.logger; const logger = level === 'error' && this.errorLogger ? this.errorLogger : this.logger;
const { module, submodule, method, ...meta } = args?.[0] || {}; const { module, submodule, method, ...meta } = args?.[0] || {};
logger.log({ logger.log({
level, level,
stack,
message, message,
stack,
meta, meta,
module: module || info['module'] || '', module: module || info['module'] || '',
submodule: submodule || info['submodule'] || '', submodule: submodule || info['submodule'] || '',
@ -63,11 +63,51 @@ class SystemLoggerTransport extends Transport {
app, app,
reqId, reqId,
}); });
if (cause) {
logger.log({
level,
message: cause.message,
stack: cause.stack,
app,
reqId,
});
}
callback(null, true); callback(null, true);
} }
} }
export const createSystemLogger = (options: SystemLoggerOptions): SystemLogger => function child(defaultRequestMetadata: any) {
winston.createLogger({ const logger = this;
return Object.create(logger, {
write: {
value: function (info: any) {
const infoClone = Object.assign({}, defaultRequestMetadata, info);
if (info instanceof Error) {
infoClone.stack = info.stack;
infoClone.message = info.message;
infoClone.cause = info.cause;
}
logger.write(infoClone);
},
},
});
}
export const createSystemLogger = (options: SystemLoggerOptions): SystemLogger => {
const logger = winston.createLogger({
transports: [new SystemLoggerTransport(options)], transports: [new SystemLoggerTransport(options)],
}); });
// Since error.cause is not supported by child logger of winston
// we have to use a proxy to rewrite child method
return new Proxy(logger, {
get(target, prop) {
if (prop === 'child') {
return child.bind(target);
}
return Reflect.get(target, prop);
},
});
};