node-analytics.js
function logRequest(req, res, session, request, cb){

    const now = new Date();

    if(opts.log){
        onHeaders(res, log_start.bind(res));
        onFinished(res, req_log.bind({
            req: request,
            ses: session
        }));
    }

    cb(null, session, request);

    if(opts.log_all)
        log.timer('logRequest', now);

    /// ==============

    function log_start(){
        this._log_start = process.hrtime();
    }
    function req_log(){

        const request = this.req;
        const session = this.ses;

        // Status colour
        const sc = res.statusCode < 400 ? 'green' : 'red';

        // Res time
        const ms = nano_time(res._log_start);

        // Referrer
        let ref = request.referrer;
        if(ref){
            ref = ref.replace('http://', '');
            ref = ref.replace('https://', '');
        }


        // Args
        const args = [session, '|', chalk.magenta(request.url), '|', request.method, chalk[sc](res.statusCode), `: ${ms} ms`];

        if(session && session.system){

            args.push('|');

            if(session.system.browser){
                args.push(chalk.grey(session.system.browser.name));
                args.push(chalk.grey(session.system.browser.version));
            }
            if(session.system.os){
                args.push(chalk.grey(session.system.os.name));
                args.push(chalk.grey(session.system.os.version));
            }
        }

        if(ref)
            args.push('|', chalk.grey(`from ${ref}`));

        // Apply
        log.session.apply(log, args);

        // ===

        function nano_time(start){
            let t = conv(process.hrtime()) - conv(start);   // ns
            t = Math.round(t / 1000);                       // µs
            return t / 1000;                                // ms [3 dec]

            // ====

            function conv(t){
                if(!t || typeof t[0] === 'undefined')
                    return 0;

                return t[0] * 1e9 + t[1];
            }
        }
    }
}
function logRequest(req, res, session, request, cb){

    const now = new Date();

    if(opts.log){
        onHeaders(res, log_start.bind(res));
        onFinished(res, req_log.bind({
            req: request,
            ses: session
        }));
    }

    cb(null, session, request);

    if(opts.log_all)
        log.timer('logRequest', now);

    /// ==============

    function log_start(){
        this._log_start = process.hrtime();
    }
    function req_log(){

        const request = this.req;
        const session = this.ses;

        // Status colour
        const sc = res.statusCode < 400 ? 'green' : 'red';

        // Res time
        const ms = nano_time(res._log_start);

        // Referrer
        let ref = request.referrer;
        if(ref){
            ref = ref.replace('http://', '');
            ref = ref.replace('https://', '');
        }


        // Args
        const args = [session, '|', chalk.magenta(request.url), '|', request.method, chalk[sc](res.statusCode), `: ${ms} ms`];

        if(session && session.system){

            args.push('|');

            if(session.system.browser){
                args.push(chalk.grey(session.system.browser.name));
                args.push(chalk.grey(session.system.browser.version));
            }
            if(session.system.os){
                args.push(chalk.grey(session.system.os.name));
                args.push(chalk.grey(session.system.os.version));
            }
        }

        if(ref)
            args.push('|', chalk.grey(`from ${ref}`));

        // Apply
        log.session.apply(log, args);

        // ===

        function nano_time(start){
            let t = conv(process.hrtime()) - conv(start);   // ns
            t = Math.round(t / 1000);                       // µs
            return t / 1000;                                // ms [3 dec]

            // ====

            function conv(t){
                if(!t || typeof t[0] === 'undefined')
                    return 0;

                return t[0] * 1e9 + t[1];
            }
        }
    }
}
log request