0.10.1 • Published 9 years ago

express-log2response v0.10.1

Weekly downloads
1
License
MIT License
Repository
github
Last release
9 years ago

Log2response

Log2response is a tool (express middleware) that provides simple API to send your request-based logs directly into browser console. Its not a complex log utility, so you probably should use it with your main tool like bunyan as a transport

Features

  • All request-based logs in your developer's console
  • Colored output
  • Callbacks that will help you to customize output, e.g. if color depends of content
  • Easy to use with another log utility

Installation

npm install --save-dev express-log2response

Usage

First, execute as a middleware:

app.use(require('express-log2response')(options));

You can specify next options. All of these is not required:

Next, you can use res.log(input) in your application and then logs will be sent to browser console with calling res.send or res.render. Note that log will be sent to browser ONLY if content type is html. So don't worry about breaking your JSON or smth.

Using with main log utility

All you need is your request-based log utility must have access to res.log. So you can specify res.log function as a one of destination functions (or streams) of main utility.

Example for bunyan:

req.log = bunyan.createLogger({
    name: 'REQ',
    streams: [
        ...
        {
            level: 'debug',
            stream: {write: res.log},
            type: 'raw'
        }
        ...
    ]
});
...
app.get('/', function(req, res, next) {
    req.log.debug('This log will be sent in browser console!');
}

So all you need to do is send all your logs to res.log.

If your main log utility is not much flexible, you can write custom fucntion that will send logs to each destination. Example:

res.l = function() {
    this.log(arguments);
    req.anotherLogTool(arguments);
}

Minimal example application

index.js:

var express = require('express');
var app = express();

app.use(require('express-log2response')());
app.get('/', function(req, res, next){
    res.log('Requset path is ' + req.path);
    res.send('alloha');
});
app.listen(3001);

Run nodejs index.js and go to http://localhost:3001