all files / middleware/ log.js

100% Statements 38/38
100% Branches 10/10
100% Functions 1/1
100% Lines 36/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                                                           
/**
 * Log middleware with Lark-Log
 **/
'use strict';
 
const $       = require('lodash');
const debug   = require('debug')('lark.middlewares.log');
const assert  = require('assert');
const bytes   = require('bytes');
const utils   = require('lark-utils');
 
const LarkLogger  = require('lark-log');
 
debug('loading ...');
 
function middleware (config = {}, app = null) {
    debug('middleware() called ...');
    assert(config instanceof Object, 'log middleware config should be an object');
    assert(null !== app, 'Internal Error!');
    const logger = new LarkLogger(config);
    debug('lark logger initialized');
    app.logger = logger;
 
    let accessMethod = config.accessMethod;
    if (!accessMethod) {
        return (ctx, next) => next();
    }
    assert('string' === typeof accessMethod && logger[accessMethod] instanceof Function, 'Access method [' + accessMethod + '] should be a function!');
    debug('access method confirmed');
 
    debug('middleware() done!');
 
    return (ctx, next) => {
        debug('middleware of log called ...');
        ctx.logger = logger;
        let startTime = new Date();
        assert(logger[accessMethod] instanceof Function, 'Access method [' + accessMethod + '] should be a function!');
        const requestInfo = {
            REQUEST_TIME: utils.time.format('YYYY-MM-DD HH:II:SS.UUU', startTime),
            METHOD: ctx.method.toUpperCase(),
            URL:    ctx.url,
            HOST:   ctx.host,
            IP:     ctx.ip,
            IPS:    ctx.ips,
            HEADER: ctx.header,
        };
 
        const printLog = error => {
            debug('printing access log ...');
            const responseTime = new Date();
            const bodyLength = (ctx.body || '').length;
            const responseInfo = {
                RESPONSE_TIME: utils.time.format('YYYY-MM-DD HH:II:SS.UUU', responseTime),
                STATUS: ctx.status,
                TYPE:   ctx.type,
                LENGTH: bodyLength,
                SIZE:   bytes(bodyLength),
                COST:   (responseTime.getTime() - startTime.getTime()) + 'ms',
                LAST_MODIFIED:  ctx.lastModified,
                ETAG:   ctx.etag,
                ERROR_MESSAGE: error instanceof Error ? error.message : '',
            }; 
            logger[accessMethod](JSON.stringify($.assign(requestInfo, responseInfo)));
 
            if (error instanceof Error) throw error;
        }
 
        return next().then(printLog).catch(printLog);
    };
}
 
debug('loaded!');
module.exports = middleware;