-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Middleware logging... how to? #367
Comments
I think I don't understand your argument. What is sub optimal about it? The context doesn't really know anything about which middleware is processing it at this moment anyway. Our current approach lets you log any way you want, which is super optimal :) |
debug isn't really designed for long-lived logging, it's meant to be a solution for libs that would otherwise not have any logging, just transient development debugging etc. However I do agree some standardization would be nice, logging is an epic mess if everything is different, but people have a hard tim even agreeing on levels and methods of logging (json vs text etc) so it would be tricky to get into core I think |
My argument is this: in the ideal world, logging should be the end user's impetus entirely. This is important when you start to write log parsers which dumps your logs into aggregator engines. The more uniform your logs are, the better. And to ensure that your logs are pretty, you need to have some form of control over what module gets to output stuff. To be honest I haven't have a clue yet. Haven't done any deep thinking into how to accomplish this, but I'll come up with a code sample later tonight to illustrate what I feel would be an improvement over the middleware system in koa. |
Agreed, I've switch back and forth myself, that's why I'm not sure I would commit to either structured or unstructured. I have mixed feelings about both, especially with Go, structured logging is nottttt fun |
In this case, we've provided the method for middleware to bubble up their logs to the end user without polluting STDOUT or STDERR. It is then the user's impetus to determine how best to parse the payload... either through simple
or something similar. I figure that would at least provide a way for middleware to submit information back to koa without having them to resort back to using and choosing their own logging methods. Let me know if this proposal sounds silly in the grand scheme of things. |
That might not be a bad idea. I like that we would have a standard Also I got confused because you said bubble, I assume all you're talking about here would be emitting a log event? |
@hallas well... I'm not sure what the right term is. But yeah, most likely an emitted event. And I'm on the same page as you... I have no love for |
This is actually a pretty easy piece of code in
|
You might want to make sure that the |
Just a sidenote... I notice this issue is kind of similar to #219 |
I personally would not consider a logger to be a middleware, but a context property. Also, it is a standard to have 'debug', app.use(function * () {
this.logger.log(level, message, data);
this.logger.info(message, data);
this.logger.warn(message, data);
}); However, since not so many people agree with the latter, the helper functions could simply be created dynamically (through a setup process). var context = require('koa/lib/context');
context.logger.warn = function(message, data) {
context.logger.log('warn', message, data);
}; I like the event-based concept, though on the app.logger.on('log', function (evt) {
// evt.level = string, log level
// evt.message = string
// evt.data = any
// evt.context = object, application context
// evt.timestamp = unix time (int)
}); |
@soggie thank you for bringing this up. I'm actually going over this issue at the moment and while I'm an advocate of the I like the event-based concept, but won't it introduce too many performance hurdles with high load? Correct me if I'm wrong, but I think this could be kept really simple, e.g.:
var debug = require('debug')('koa:application');
app.log = debug;
this.log = this.app.log; Then we'd just need to pass our own function: var log = require('bunyan');
var app = koa();
app.log = log.debug;
mid(app);
app.listen(); What do you think? |
My main worry is the structured-vs-unstructured log approaches since they're completely different. The pros probably out-weigh the cons there though, that would help mitigate the need for tracing middleware too. Any other maintainers have thoughts on this? @koajs/owners |
i think all public middleware should cohere to some strict standard (i.e. only strings). each logger can support additional features like objects, arrays, etc, but those types of logs should only be used by the app, not public modules. but i'm not sure what that strict standard is... |
@ruimarinho the problem with |
Yeah that's where I'm torn too, choosing between structured and unstructured is tricky. Let's keep it on the back burner for now for post-1.0 |
I'm leaning towards providing a suite of log methods like warn, info, log, debug and having them emit a log event on the application which comes with the log record and the context. That way people can listen on the event and log it the way they need to. |
+1 with a default log configuration to write logs to stderr. Koa should be the central dispach for middleware logs and even console output if we want to foster an ecosystem. |
Summary above, there're two ways for logging:
app.on('log', function (log) {
// log.level = string, log level
// log.message = string
// log.ctx = ctx
});
|
I prefer event,not the instance.Make a chance to write logger on my own,and feel much more fun.Actually I still think to make a middle ware for package the request ,response and ctx. = = Why not |
I'd vote for structured if this is something that people want to see in the core. printf-style is too hard to work with in practice, basically useless for querying so I wouldn't want to promote that out of the box. Just regular old |
I agree with @tj. The middleware would allow, just like What I don't like about events is the unnecessary steps :
However, with a middleware, we get rid of steps 2 and 3 altogether :
Thus, completely separate logger from core and even allow users to implement their own middlewares. |
The more I think about this, the less I believe we should build anything into core. It requires heavy-handed standardization of things many have differing opinions of - while also requiring keeping it flexible enough to be completely overridden to support things people are already doing (e.g. this). I'm +1 for documenting and promoting what a lot of the community seems to already be doing vs prescribing something for everyone. I see two common things in koa apps and modules regarding logs:
If ^ doesn't fly here, then I'm +0 for creating a standard |
How about, instead of worrying about standardizing issues when creating app.log = new Proxy({}, {
get: (target, name) => function() {
app.emit('log', [name, ctx, ...arguments])
}
}); Then you could to: ctx.log.error('your params...');
ctx.log.whatever('param1', 2, 3, 4);
ctx.log.crazy('works'); And it would pass the name of the function as the first parameter in the event. No problem with standardization of logging levels, since people can use what they want and it looks better than passing the level as a parameter. Better yet, with this the community could create pluggable interfaces to any other loggers and they'd just listen for the right events and invoke their own methods. The downside is that for now Proxy still requires |
@nicoder I like this approach. |
it doesn't seem we've come to a conclusion, so i'm going to delegate this to "userland". if you're interested in profiling middleware, follow this discussion: #219
|
Hi guys, I'm looking at
koa-i18n
and it usesdebug
as a means to log information. I think this is sub-optimal and makes it hard for koa users to control what kind of information gets printed.My suggestion is to provide an
info
anddebug
method alongsidethrow
inctx
so that there is a standard way for middlewares to bubble up messages and as an end-user of koa.js you can choose your own logging library to handle the logs.What do you guys think?
The text was updated successfully, but these errors were encountered: