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.

Add a way to get the log line number when console.log has been wrapped by a custom function

See original GitHub issue

Is your feature request related to a problem? Please describe. I am working on a TS express app and the logger will use a custom ConsoleTransport class that wraps console.log. Now whenever it outputs to the debug console, the line number shows the code within my ConsoleTransport class. I added the file to skipFiles of the launch config with the hope that it would ignore that file and perhaps show me the top call in the stack that was not ignored, but it did not.

// my launch.json configuration
{
        "name": "Run Backend",
        "type": "node",
        "request": "launch",
        "runtimeArgs": ["--nolazy", "-r", "ts-node/register", "-r", "tsconfig-paths/register"],
        "args": ["./src/index.ts", "--transpile-only"],
        "cwd": "${workspaceFolder}/backend",
        "internalConsoleOptions": "openOnSessionStart",
        "skipFiles": [
            "<node_internals>/**",
            "node_modules/**",
            "${workspaceFolder}/backend/src/modules/logger/ConsoleTransport.ts"
        ],
        "env": {
            // ommitted
        }
}

All the suggestions online show hacky or confusing ways of binding to console.log and/or overriding it which just feels wrong and unintuitive to me.

Describe the feature you’d like I would love for it to use the first part of the stack that is not included in the skipFiles array or some way to tell the debugger to not use the strict line it called console.log but instead some custom function.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bzuillsmithcommented, Dec 8, 2020

I can do that in about an hour as I’m away from my desk, but it’s literally their transport but with the console._stdout lines removed (and the of statements around them so it calls console.log)

0reactions
connor4312commented, Dec 16, 2020

sure thing, thanks for filing the issue and verifying 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

A proper wrapper for console.log with correct line number?
You can create console.log wrapper that can have side effects, call other functions, etc, and still retain the line number that called the...
Read more >
Wrapping `console.log` (et al.) in your own function to modify ...
I've been trying to write a custom function to process the arguments and bind console log but I'm losing the line numbers. If...
Read more >
How to keep console.log line number in an wrapper function ...
Update: I have created an npm library with a better version of this idea. Take a look at it here: SaninnLogger Package!
Read more >
The problem with wrapping console.log is that you lose the ...
The problem with wrapping console.log is that you lose the ability to know what line number initiated the log entry. The line number...
Read more >
console.log with line numbers | Kai Hao
So lots of things is happening here, in short, the basic idea is that we can get the line number through stack trace...
Read more >

github_iconTop Related Medium Post

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