Koa middleware for Bunyan stream to Stackdriver Logging
Last updated 2 months ago by tbinna .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install stackdriver-logging-bunyan-koa 
SYNC missed versions from official npm registry.


Build Status npm version codecov

This module provides an Koa middleware for working with Stackdriver Logging, compatible with Bunyan.

The implementation is adopted from the exsting express middleware implementation in nodejs-logging-bunyan module.

For general documentation on bunyan logging to Stackdriver please refer to the nodejs-logging-bunyan module.


Installing the client library

npm install stackdriver-logging-bunyan-koa

Using the koa middleware

We provide a middleware that can be used in an koa application. Apart from being easy to use, this enables some more powerful features of Stackdriver Logging: request bundling. Any application logs emitted on behalf of a specific request will be shown nested inside the request log.

The middleware adds a bunyan-style log function to the ctx object. You can use this wherever you have access to the ctx object. All log entries that are made on behalf of a specific request are shown bundled together in the Stackdriver Logging UI.

const lb = require('stackdriver-logging-bunyan-koa');

// Import koa module and create an http server.
const koa = require('koa');

async function startServer() {
  const {logger, mw} = await lb.koa.middleware();
  const app = koa();

  // Install the logging middleware. This ensures that a Bunyan-style `log`
  // function is available on the `context` object. Attach this as one of the
  // earliest middleware to make sure that log function is available in all the
  // subsequent middleware and routes.

  // Setup an http route and a route handler.
  app.use(async (ctx) => {
    // `ctx.log` can be used as a bunyan style log method. All logs generated
    // using `ctx.log` use the current request context. That is, all logs
    // corresponding to a specific request will be bundled in the Stackdriver
    // UI.'this is an info log message');
    ctx.body = 'hello world';

  // `logger` can be used as a global logger, one not correlated to any specific
  // request.{port: 8080}, 'bonjour');

  // Start listening on the http server.
  app.listen(8080, () => {
    console.log('http server listening on port 8080');



Contributions welcome!


Apache Version 2.0

Current Tags

  • 1.0.0-alpha.0                                ...           alpha (9 months ago)
  • 1.0.1                                ...           latest (2 months ago)

3 Versions

  • 1.0.1                                ...           2 months ago
  • 1.0.0                                ...           9 months ago
  • 1.0.0-alpha.0                                ...           9 months ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 15
Dependencies (15)
Dev Dependencies (10)
Dependents (0)

Copyright 2014 - 2017 © |