Koa.js - Logging


Advertisements


Logging is quite useful when creating web applications as they tell us where exactly things went wrong. We also get the context for the things that went wrong and can come up with possible solutions for the same.

To enable logging in Koa, we need the middleware, koa-logger. Install it using the following command.

$ npm install --save-dev koa-logger

Now in your application, add the following code to enable logging.

var logger = require('koa-logger')
var koa = require('koa')

var app = koa()
app.use(logger())

app.use(function*(){
   this.body = "Hello Logger";
})

app.listen(3000)

Run this server and visit any route on the server. You should see the logs like −

Logging

Now if you get an error on a specific route or request, these logs should help you figure out what went wrong in each of them.



Advertisements
E-Books Store