Thursday, March 2, 2017

Koa.js - Logging

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

For enabling logging in Koa, we need the middleware, koa-logger. Install it using:
$ npm install --save-dev koa-logger
Now in your applicaiton, 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:
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.

No comments:

Post a Comment