-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: log Errors on winston ^2.3.1 #13
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
// I copied LOGGING_LEVELS from src/helpers/assertLevel.js | ||
// because this file is going to be deprecated when the bug is solved on winston. | ||
const LOGGING_LEVELS = [ | ||
'error', | ||
'warn', | ||
'info', | ||
'verbose', | ||
'debug', | ||
'silly', | ||
] | ||
|
||
// FIX: because winston from versions 2.3.1 until 2.4.0 does not log Error objects. | ||
const cloneErrorProxy = logger => { | ||
const handler = { | ||
get: (target, name) => { | ||
// If the target[name] is a logging level we should verify if any | ||
// of the arguments is an Error. | ||
if (LOGGING_LEVELS.indexOf(name) > -1) { | ||
return (...loggerArgs) => { | ||
const validArgs = loggerArgs | ||
.map(loggerArg => { | ||
if (loggerArg instanceof Error) { | ||
// Loosely "Clone" Error Objects | ||
return { | ||
message: loggerArg.message, | ||
stack: loggerArg.stack, | ||
} | ||
} | ||
return loggerArg | ||
}) | ||
|
||
return target[name](...validArgs) | ||
} | ||
} | ||
|
||
return target[name] | ||
}, | ||
} | ||
|
||
return new Proxy(logger, handler) | ||
} | ||
|
||
module.exports = cloneErrorProxy |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,20 @@ | |
const glob = require('glob') | ||
const winston = require('winston') | ||
|
||
const cloneErrorProxy = require('./helpers/cloneErrorProxy') | ||
|
||
const isTransport = t => t && t.log | ||
const transports = glob.sync('./transports/*.js', { cwd: __dirname }) | ||
.map(require) | ||
.filter(isTransport) | ||
|
||
const logger = new (winston.Logger)({ | ||
const unwrappedLogger = new (winston.Logger)({ | ||
transports, | ||
}) | ||
|
||
// We are proxying winston logger because versions from 2.3.0 until 2.4.0 | ||
// does not log Error objects. The proxy is going to be DEPRECATED | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: does -> do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed :) |
||
// after winston solves its bug. | ||
const logger = cloneErrorProxy(unwrappedLogger) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment in this file as well as to why we're doing that |
||
|
||
module.exports = logger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied
LOGGING_LEVELS
fromsrc/helpers/assertLevel.js
because this file is going to be deprecated when the bug is solved onwinston
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make the above a code comment