"use strict"; var levels = require("./levels"); var _ = require('underscore'); var DEFAULT_FORMAT = ':remote-addr - -' + ' ":method :url HTTP/:http-version"' + ' :status :content-length ":referrer"' + ' ":user-agent"'; /** * Log requests with the given `options` or a `format` string. * * Options: * * - `format` Format string, see below for tokens * - `level` A log4js levels instance. Supports also 'auto' * * Tokens: * * - `:req[header]` ex: `:req[Accept]` * - `:res[header]` ex: `:res[Content-Length]` * - `:http-version` * - `:response-time` * - `:remote-addr` * - `:date` * - `:method` * - `:url` * - `:referrer` * - `:user-agent` * - `:status` * * @param {String|Function|Object} format or options * @return {Function} * @api public */ function getLogger(logger4js, options) { if ('object' == typeof options) { options = options || {}; } else if (options) { options = { format: options }; } else { options = {}; } var thislogger = logger4js , level = levels.toLevel(options.level, levels.INFO) , fmt = options.format || DEFAULT_FORMAT , nolog = options.nolog ? createNoLogCondition(options.nolog) : null; return function (req, res, next) { // mount safety if (req._logging) return next(); // nologs if (nolog && nolog.test(req.originalUrl)) return next(); if (thislogger.isLevelEnabled(level) || options.level === 'auto') { var start = process.hrtime() , statusCode , writeHead = res.writeHead , url = req.originalUrl; // flag as logging req._logging = true; // proxy for statusCode. res.writeHead = function(code, headers){ res.writeHead = writeHead; res.writeHead(code, headers); res.__statusCode = statusCode = code; res.__headers = headers || {}; //status code response level handling if(options.level === 'auto'){ level = levels.INFO; if(code >= 300) level = levels.WARN; if(code >= 400) level = levels.ERROR; } else { level = levels.toLevel(options.level, levels.INFO); } }; //hook on end request to emit the log entry of the HTTP request. res.on('finish', function() { var end = process.hrtime(); res.responseTime = (end[0] - start[0]) * 1000 + (end[1] - start[1]) / 1000000; //status code response level handling if(res.statusCode && options.level === 'auto'){ level = levels.INFO; if(res.statusCode >= 300) level = levels.WARN; if(res.statusCode >= 400) level = levels.ERROR; } if (thislogger.isLevelEnabled(level)) { var combined_tokens = assemble_tokens(req, res, options.tokens || []); if (typeof fmt === 'function') { var line = fmt(req, res, function(str){ return format(str, combined_tokens); }); if (line) thislogger.log(level, line); } else { thislogger.log(level, format(fmt, combined_tokens)); } } }); } //ensure next gets always called next(); }; } /** * Adds custom {token, replacement} objects to defaults, overwriting the defaults if any tokens clash * * @param {IncomingMessage} req * @param {ServerResponse} res * @param {Array} custom_tokens [{ token: string-or-regexp, replacement: string-or-replace-function }] * @return {Array} */ function assemble_tokens(req, res, custom_tokens) { var array_unique_tokens = function(array) { var a = array.concat(); for(var i=0; i