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.

Console transport does not write to debug console

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • _node -v outputs: v10.1.0
  • Operating System? Windows
  • Language? TypeScript

What is the problem?

When I use the built in Console transport, it will write to console when running normally (eg node ./dist/index.js ) but not to the debug console when I debug from Visual Studio Code. Standard calls to console.log()/warn()/error() do write to the debug console.

What do you expect to happen instead?

I expect all messages to write to the debug console as they would to the standard console when running normally.

Other information

The problem seems to be the use of _stderr.write() and _stdout.write() in the log() method of the transport implementation. If I replace the condition in the if statements at 49, 62 and 77 with false so that the standard console.log()/warn()/error() functions get called, the output does reach the console output. Obviously an undesirable side-effect is that the custom eol gets ignored.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:17
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

85reactions
victorandreecommented, Mar 12, 2019

I just ran into this issue and wanted to add my very simple idea: Just use console.log, console.warn and console.error 😄

Right now, Winston is checking for console._stderr and console._stdout, preferring to write to those streams directly, but falling back to using console.warn, console.error and console.log if not available. These properties (_stderr and _stdout) are available on the global console instance in node.js, however, they’re not documented as being part of the console API nor are they enumerable.

What’s the upside to using the streams directly in Winston over calling the methods? To me, console in a Javascript context means specifically the console object, not “console” in general or as a proxy for streams. Wouldn’t the methods always work? Especially since there is a built-in stream transport in Winston, I would interpret “console” as appropriate for basically local development.

VS Code does have the corresponding properties (probably because it’s running on node), but its default “capture” for the debug console is “console” – which probably means they’ve installed hooks specifically on the console.log calls, not on the streams themselves. I.e. VS Code is generating output, but not capturing it. It’s probably related to this file

As others have mentioned, you can change which output VS Code captures, or change what the “console” transport does in Winston.

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceFolder}/index.js",
      // Capture "std" instead of "console"
      "outputCapture": "std"
    }
  ]
}

Note, if you follow the linked advice from @adi7ya, VS Code will output the line where console.log got called – which is not where you called Winston, but the line in your overridden log. So, it might be only marginally useful…

8reactions
melbourne2991commented, Jun 6, 2019

This also bit me, could not agree more with @victorandree.

I’m using this very barebones transport for my own purposes at the moment if anyone else finds it useful:

import winston from "winston";
import Transport from "winston-transport";

const { MESSAGE } = require("triple-beam");
const level = process.env.LOG_LEVEL || "info";

class SimpleConsoleTransport extends Transport {
  log = (info: any, callback: any) => {
    setImmediate(() => this.emit("logged", info));

    console.log(info[MESSAGE]);

    if (callback) {
      callback();
    }
  };
}

export const logger = winston.createLogger({
  level,
  defaultMeta: {},
  transports: [new SimpleConsoleTransport()]
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

Winston not Logging to console in typescript - Stack Overflow
This is the Typescript way to import Winston. First, be sure you have installed the typing : npm i -D @types/winston. Then, in...
Read more >
How to use the winston.transports.Console function in ... - Snyk
To help you get started, we've selected a few winston examples, based on popular ways it is used in public projects. Secure your...
Read more >
How to Get Started with Logging in Node.js - Better Stack
The most common way to log in Node.js is by using methods on the console module (such as log() ). It's adequate for...
Read more >
Node.js Logging – How to Get Started - Papertrail
Though console.log offers a hassle-free way to write log messages, it leaves a lot to be desired. For one, there's no way to...
Read more >
Logs stopped showing on console [37137285] - Visible to Public
Console no longer shows Log debug messages right after I upgraded to Studio 23 ... This issue was about the logcat output 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