Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/winston/create-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ class DerivedLogger extends Logger {
return;
}

//Handle Error Objects
const errorMapper = o => o instanceof Error ? { message : o.message || 'Error', stack : o.stack} : o;

// Define prototype methods for each log level
// e.g. logger.log('info', msg) <––> logger.info(msg) & logger.isInfoEnabled()
// this is not an arrow function so it'll always be called on the instance instead of a fixed place in the prototype chain.
this[level] = function (...args) {
// Optimize the hot-path which is the single object.
if (args.length === 1) {
const [msg] = args;
const [msg] = args.map(errorMapper);
const info = msg && msg.message && msg || { message: msg };
info.level = info[LEVEL] = level;
this.write(info);
Expand Down
6 changes: 5 additions & 1 deletion lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ class Logger extends Transform {
return this;
}

//Handle Error Objects
const errorMapper = o => o instanceof Error ? { message : o.message || 'Error', stack : o.stack} : o;
msg = errorMapper(msg);

// Slightly less hotpath, but worth optimizing for.
if (arguments.length === 2) {
if (msg && typeof msg === 'object') {
Expand All @@ -171,7 +175,7 @@ class Logger extends Transform {

const [meta] = splat;
if (typeof meta === 'object' && meta !== null) {
this.write(Object.assign({}, meta, {
this.write(Object.assign({}, errorMapper(meta), {
[LEVEL]: level,
[SPLAT]: splat.slice(1),
level,
Expand Down