question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't modify arguments passed to logging methods

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v11.4.0
  • Operating System? (Linux)
  • Language? (ES6/7)

What is the problem?

Winston modifies objects passed to it!

I have a webservice with error handler:

   handleError(err, req, res, next) {
     winston.warn(err)
     res.status(err.status || err.statusCode || 500).json(err)
   }

I expect, say, a 404 response with body {"message":"Not Found"}, but with the winston.warn call included, it becomes {"message":"Not Found","level":"warn"}. Of course, it’s worse after going through formatters—I get tabs, ANSI color codes, etc.—but out of the box I still get the level property.

What do you expect to happen instead?

No mutation

Other information

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
dpurringtoncommented, Feb 13, 2019

The log utility we use can’t have side effects. This is a deal-breaker for us, unfortunately.

7reactions
indexzerocommented, Dec 23, 2018

This is by design. The README for info clearly states that the objects are mutable. In this particular case, you could avoid the mutability if you wished, but opting not to use the convenience method .warn. Instead:

winston.log({ level: 'warn', ...err });

@sparkida unfortunately there is no easy way to handle all permutations of possible inputs that folks which to provide. In particular the desire of most developers includes:

  1. Variable-arity (e.g. logger.log('info', 'Interpolate %s [%j]', 'please', { myData: 'stuff' }))
  2. Using errors as “data” objects (as in this case) – despite Error instances having non-enumerable properties.

… and many others. In general from a historical perspective, winston@1, and winston@2 did clone each object that was passed to it. This led to an significantly lower performance – which as a project we sought to fix in winston@3.

There are trade-offs for performance vs. features. We opt for performance whenever possible.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How to disable evaluation of parameters in a method
When I disable logger in settings (so the _isActive field in Logger class is false), then nothing is saved to database. But all...
Read more >
java - Is it still a good practice to log parameters and returns?
I am currently setting up the logging of the application and I stumbled upong this url and found it helpful. Although it is...
Read more >
Python logging: do's and don'ts - palkeo
It will work fine. But it's bad. Don't do it. Instead, all the logging methods accept a string and arbitrary number of parameters....
Read more >
A better way to logging in Python | F5 - Squashing Bugs
We can do this by just iterating over args and kwargs and joining them to form string message to log.
Read more >
logging — Logging facility for Python — Python 3.11.1 ...
If this attribute evaluates to true, events logged to this logger will be passed to the handlers of higher level (ancestor) loggers, in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found