-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[RFC] Pluggable log handler #556
Comments
It might be much more practical to just have a top level function on the
const debug = require('debug');
debug.on('log', msg => writeToSomeStream(msg)); |
The problem with both of these solutions, is I need to know/iterate all of my dependencies that support The goal is to re-direct all |
Right now it's going to I still think a top-level function that receives all of the raw output (that defaults to I also think this is a bit of an anti-pattern, personally. |
Yes, If I have 3 modules, all using I can appreciate that you think it's an anti-pattern; however, all I'm trying to do is control how my application handles debug output. If anything here is an anti-pattern, it's the fact that a dependency is outputting to If this is out-of-scope for |
You wouldn't, it'd be a top level function that belongs to the module itself, therefore not requiring that you edit the code that uses the module. But as I said before, this is usually an anti-pattern. |
Ok; happy to implement this if this is something that you’d consider, but I’m getting the feeling you consider this to be an anti-pattern. Let me know if you decide otherwise. |
Definitely something I'm considering. I have an idea on how this will happen - stay tuned. |
Within the discussion of #644 the idea of using Similar to SLF4J, adding log handlers at the end user level is becoming a common theme among use cases (see the closed issues above this comment). I think we should solve it as such. I think adding a global log handler would be ideal, allowing extensions that use Take the following example implementation: // debug.js
let handler = (ns, msg) => console.error(`\x1b[2m${ns}\x1b[m`, msg);
const debug = ns => {
return msg => handler(ns, msg);
};
debug.setHandler = _handler => {
handler = _handler;
};
module.exports = debug; // handler.js
const debug = require('./debug');
debug.setHandler((ns, msg) => console.error(JSON.stringify({
namespace: ns,
message: msg
}))); // index.js
const debug = require('./debug')('foo:bar');
debug('hello, world!');
debug({foo: 'bar'}); $ node index.js
foo:bar hello, world!
foo:bar { foo: 'bar' } $ node -r ./handler.js index.js
{"namespace":"foo:bar","message":"hello, world!"}
{"namespace":"foo:bar","message":{"foo":"bar"}} This solves a lot of the problems, features and pains users have felt for a while. Coupled with #644, this could be a large step forward whilst being backward compatible (with a default compatibility handler for the time being). Thoughts? cc @TooTallNate |
The |
They would go hand in hand @jasonpincin. :) |
Sure. Just saying it’s not a requirement right? My app can internally bind the log handler. |
@Qix- I believe this is relevant here so I'll post it as a comment instead of a new issue (let me know if you'd rather have it as a separate issue though) I'm trying to stream all debug messages generated in a server via a websocket, is there an elegant way to listen to all messages sent to debug so that I can stream them, but still having them reported in the usual way (stdout)? |
No, not elegant or "correct". That's exactly what this issue would solve. |
@Qix- Thanks for the quick reply! So I guess the only non-hacky way will be to wait for the release of v5, I'll keep an eye on it! |
Yes, sadly :| I have some vacation coming up and hope to knock out a ton of OSS that has built up. |
I too am looking for a similar feature (and for node). Specifically I would like to be able to prepend all debug statements with some process-specific string that can later be used by AWS Cloudwatch Insights to parse log statements based on process-specific information. For example, I would like to add a unique ID and process name to every debug statement and set this once in my application (not on every file). This makes it easy to filter multiple application's log statements (in AWS Log Insights) so I can see logging for just a a particular run of my process. Typically, an error would be reported by some mechanism (email) and filtering would allow me to see just the log statements that came in the same process just before the error line. I would like to take this one step further and turn all log statements into a json string. This would give me some well-known structure for all debug statements in my specific application and that json makes it easy to filter in AWS Log Insights. One thing to note is that I have a |
@bodaro Yep, or just set it directly via |
Doing a bit of poking around on this today, this appears to be soft-blocked by nodejs/node#40110. Not a deal-breaker but it'll be less than advantageous without allowing users to do this with ESM modules. The other issue is code-based linkage. If you add a handler via code instead of pre-loading modules, then we'll run into the issue of libraries pre-loading their own handlers and causing unexpected output or mis-routed output (potentially) - something that, given the ubiquity of this package, simply won't do. There needs to be a way to either prevent or disable added log handlers in application code. |
This comment has been minimized.
This comment has been minimized.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as off-topic.
This comment was marked as off-topic.
https://www.npmjs.com/search?q=logging Not sure what else you'd like from this thread; coming here to complain certainly isn't making this get implemented any faster. You're not "stuck" with this package, you're free to use what you'd like. I haven't had time to work on this. If my schedule is not fast enough for you, you're welcome to switch to something else. Locking for now as I don't think there's going to be any more constructive conversation on this topic moving forward until it's implemented. |
Would you consider a PR that implemented support for globally defaulting
log
to something other thanprocess.stderr
? The way I’d expect this to work is debug would default to stderr unlessprocess[Symbol.for(‘DEBUG_LOG’)]
was set. Of course the localself.log
would always take precedence.The motivation for this feature is to allow an application to redirect all debug output to a logger of its choice, without knowing which all dependencies utilize debug.
I’ve chosen a global Symbol to circumvent the issue of multiple versions of debug in codebase.
Happy to submit this. Thoughts?
The text was updated successfully, but these errors were encountered: