npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

access-log

v0.4.1

Published

Add simple access logs to any http or https server

Downloads

15,994

Readme

access-log

Add simple access logs to any http or https server

Usage

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

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

This will automatically log requests as they come in to the web server that look like...

127.0.0.1 - - [13/Sep/2013:01:38:09 -0400] "GET / HTTP/1.1" 200 - "-" "-"
127.0.0.1 - - [13/Sep/2013:01:38:09 -0400] "GET /testing HTTP/1.1" 200 - "-" "-"
127.0.0.1 - - [13/Sep/2013:01:38:10 -0400] "GET /index.html HTTP/1.1" 200 - "-" "-"

Customization

accesslog(req, res, [opts], [function])

opts

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 http://en.wikipedia.org/wiki/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.

ex:

request to :url took :{delta}ms

function

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

Example

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

accesslog(req, res, format, function(s) {
  console.log(s);
});

yields

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

Installation

npm install access-log

Extend

Consider further customizing the access logs by using the [log-timestamp] (https://github.com/bahamas10/node-log-timestamp) module to prepend a timestamp automatically.

License

MIT Licensed