Add simple access logs to any http or https server

Downloads in past


0.4.17 years ago12 years agoMinified + gzip package size for access-log in KB


Add simple access logs to any http or https server


var http = require('http');
var accesslog = require('access-log');

http.createServer(function(req, res) {
  accesslog(req, res);
}).listen(80, '');

This will automatically log requests as they come in to the web server that look like... - - [13/Sep/2013:01:38:09 -0400] "GET / HTTP/1.1" 200 - "-" "-" - - [13/Sep/2013:01:38:09 -0400] "GET /testing HTTP/1.1" 200 - "-" "-" - - [13/Sep/2013:01:38:10 -0400] "GET /index.html HTTP/1.1" 200 - "-" "-"


accesslog(req, res, opts, function)


Opts is an object that can contain a format identifier and userID func (both optional).
For example,
    userID: function (req) { return req.user; },
    format : 'url=":url" method=":method" statusCode=":statusCode" delta=":delta" ip=":ip"'

You can pass in a format string, the default is Apache Common Log Format
If opts is a string, it is assumed to be the opts.format property.
:ip - :userID [:clfDate] ":method :url :protocol/:httpVersion" :statusCode :contentLength ":referer" ":userAgent"

  • clfDate: The date of the end of the response in Apache Common Log format
  • contentLength: The response Content-Length header, or - if unset
  • delta: The time in ms from request to response
  • endDate: The ISO formatted string when the response was ended
  • endTime: The epoch time when the response was ended
  • host: The host header from the request if set
  • httpVersion: The HTTP version used (ie. 1.0, 1.1)
  • ip: The remote IP
  • Xip: The remote IP, using X-Forwarded-For if set
  • method: The HTTP method
  • protocol: HTTP or HTTPS
  • referer: The request Referer header, or - if unset
  • startDate: The ISO formatted string when the request was received
  • startTime: The epoch time when the request was received
  • statusCode: The response status code sent from the server
  • url: The requested URL
  • urlDecoded: The decoded request URL (ie. %20 => )
  • userID: The username if applicable
  • userAgent: The request User-Agent header, or - if unset

NOTE: Wrap variables in {} to protect against unwanted interpolation.
request to :url took :{delta}ms


You can also pass in your own custom callback, the default is console.log. The only argument passed is the access log string


var format = 'url=":url" method=":method" statusCode=":statusCode" delta=":delta" ip=":ip"';

accesslog(req, res, format, function(s) {

url="/projects" method="GET" statusCode="200" delta="0" ip=""
url="/testing" method="GET" statusCode="200" delta="1" ip=""
url="/index.html" method="GET" statusCode="200" delta="0" ip=""


npm install access-log


Consider further customizing the access logs by using the log-timestamp ( module to prepend a timestamp automatically.


MIT Licensed