Skip to content

Instantly share code, notes, and snippets.

@astashov
Created October 3, 2015 02:16
Show Gist options
  • Save astashov/604bd65081f1bbce2ac2 to your computer and use it in GitHub Desktop.
Save astashov/604bd65081f1bbce2ac2 to your computer and use it in GitHub Desktop.
Redstone logger, which has unique id per request, to avoid interleaved request logs
Logger get logger => Zone.current[#logger];
Logger _initializeRequestLogger() {
var uid = new Random().nextInt(1000);
var logger = new Logger.detached("app:${uid}");
logger.onRecord.listen(print);
return logger;
}
@app.Route("/foo")
fooHandler() {
return runZoned(() {
logger.info("Started request");
// we can do some handling even in a future
return new Future(() => someFunction());
}, zoneValues: {#logger: _initializeRequestLogger()});
}
void someFunction() {
// And even here we can access logger we created in _iniitalizeRequestLogger();
logger.info("Doing some work here");
}
// And the logger's output when our service is under heavy load will be something like:
//
// app:123: Started request
// app:345: Started request
// app:123: Doing some work here
// app:345: Doing some work here
//
// i.e. we will be able to distinguish logs from different requests, even despite the fact they happen simultaneously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment