Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
45 changes: 45 additions & 0 deletions helpers/cloneErrorProxy.js
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 = [
Copy link
Member Author

@rodrigo4244 rodrigo4244 Jan 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied LOGGING_LEVELS from src/helpers/assertLevel.js because this file is going to be deprecated when the bug is solved on winston.

Copy link
Contributor

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

'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
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: does -> do
but ok

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed :)

// after winston solves its bug.
const logger = cloneErrorProxy(unwrappedLogger)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@invisible/logger",
"version": "0.0.12",
"version": "0.0.13",
"description": "Invisible Logging Wrapper",
"main": "index.js",
"scripts": {
Expand Down