Middleware logging... how to?
Hi guys, I'm looking at koa-i18n and it uses debug 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 and debug method alongside throw in ctx 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?
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
// on the middleware side... (called mid)
module.exports = function mid(app, opts) {
return function *(next) {
this.log({ foo : 'bar' }); // we will call this log_1
this.log('info', 'this is an info message'); // we will call this log_2
yield *next;
}
}
// on the app side...
var app = koa();
mid(app);
app.on('log', function (payload) {
// here I get to do my own logging
//
// the output for log_1 payload is:
//
// [ { foo : 'bar' } ]
//
// the output for log_2 payload is:
//
// [ 'info', 'this is an info message' ]
});
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 console.log, or have the community adopt a simple format like
// for info:
{ message : 'this is the log message', level : 'info' }
// for error:
{ message : 'this is an error message', level : 'error', err : <err object> }
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 log function that would handle all cases. I do not like multiple fns such as this.log, this.warn etc.
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.log, this.warn, this.trace, this.debug etc. Just one "channel" for us to receive all kinds of feedback or info from middlewares and let the user decide what to do with it.
This is actually a pretty easy piece of code in application.js:
app.log = function() {
this.emit('log', Array.prototype.slice.call(arguments));
};
You might want to make sure that the ctx is passed into the log event, so you can log stuff about the context as well :D
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', info, warn, error, etc. Something like
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 logger property instead for consistency, and to separate responsibility.
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 debug module for its simplicity, I concur it would be awesome to have a standard way - sanctioned by the koa community - in order for custom loggers to be passed down through all middlewares and also including koa's own context, request and response objects.
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.:
application.js
var debug = require('debug')('koa:application');
app.log = debug;
context.js (shorthand used by middleware)
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 app.log = whatever is that, when using middlewares such as koa-mount, and/or under certain cases, you might lose that reference. This is why I suggested attaching to koa/lib/context instead.
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:
logevent
app.on('log', function (log) {
// log.level = string, log level
// log.message = string
// log.ctx = ctx
});
- logger instance
- methods:
debug(),info(),warn(),error() app.loggerctx.logger, alias toapp.logger
- methods:
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