Open
Description
We use a custom winston transport to log errors to Trace (see below).
reportError
only supports passing instances of Error
, so we pass on the error if we have one, or wrap our custom message in an error if we only have a message.
Winston supports the concept of meta data, which it then includes in the log entry as a string. We use this to include extra context with the log entry.
It would be awesome if Trace could do the same.
Custom winston transport:
const winston = require('winston');
class TraceLogger extends winston.Transport {
constructor(options) {
super(options);
this.name = 'traceLogger';
this.level = 'error'; // only log errors to trace
this.trace = (options && options.trace) || require('@risingstack/trace');
}
log(level, msg, meta, callback) {
let traceError;
if (!meta && msg) {
traceError = new Error(msg);
} else if (meta instanceof Error) {
traceError = meta;
}
if (traceError) {
this.trace.reportError('winston_error', traceError);
}
callback(null, true);
}
}
winston.transports.TraceLogger = TraceLogger;
module.exports = TraceLogger;
As you can see from the code snippet, we only use winston meta when it is of type Error
.