Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial Commit

  • Loading branch information...
commit 39ebbd1e83016fc842f0e4357dcf1288ed61f6a6 0 parents
@bahamas10 authored
85 README.md
@@ -0,0 +1,85 @@
+access-log
+==========
+
+Add simple access logs to any http or https server
+
+Usage
+-----
+
+``` js
+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...
+
+```
+GET 200 /testing (0ms)
+GET 200 /index.html (0ms)
+GET 200 /projects (0ms)
+```
+
+Customization
+-------------
+
+### accesslog(req, res, [format], [function])
+
+#### format
+
+You can pass in a format string, the default is
+
+```
+:method :statusCode :url (:timems)
+```
+
+- :method - The request method (POST|HEAD|GET|DELETE|PUT, etc.)
+- :statusCode - The response status code sent from the server
+- :url - The requested URL
+- :time - The latency from request to response in 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
+-------
+
+``` js
+var format = 'url=":url" method=":method" statusCode=":statusCode" time=":time"';
+
+accesslog(req, res, format, function(s) {
+ console.log(s);
+});
+```
+
+yields
+
+```
+url="/projects" method="GET" statusCode="200" time="0"
+url="/testing" method="GET" statusCode="200" time="1"
+url="/index.html" method="GET" statusCode="200" time="0"
+```
+
+Installation
+------------
+
+ npm install access-log
+
+Extend
+------
+
+Consider further customizing the access logs by using the [log-timestamp]
+(https://github.com/bahamas10/node-log-timestamp) to prepend the timestamp
+automatically.
+
+License
+-------
+
+MIT Licensed
13 examples/fancy.js
@@ -0,0 +1,13 @@
+var http = require('http');
+var accesslog = require('../');
+
+var format = 'url=":url" method=":method" statusCode=":statusCode" time=":time"';
+
+http.createServer(function(req, res) {
+ accesslog(req, res, format, function(s) {
+ console.log(s);
+ });
+ res.end();
+}).listen(8000, 'localhost', function() {
+ console.log('Listening on localhost:8000');
+});
9 examples/simple.js
@@ -0,0 +1,9 @@
+var http = require('http');
+var accesslog = require('../');
+
+http.createServer(function(req, res) {
+ accesslog(req, res);
+ res.end();
+}).listen(8000, 'localhost', function() {
+ console.log('Listening on localhost:8000');
+});
23 index.js
@@ -0,0 +1,23 @@
+module.exports = access_log;
+
+function access_log(req, res, format, func) {
+ format = format || ':method :statusCode :url (:timems)';
+ func = func || console.log;
+
+ req._received_date = new Date();
+
+ // override res.end to capture all responses
+ var res_end = res.end;
+ res.end = function() {
+ var s = format
+ .replace(':method', req.method)
+ .replace(':statusCode', res.statusCode)
+ .replace(':url', req.url)
+ .replace(':time', new Date() - req._received_date);
+
+ func(s);
+
+ // now call the original
+ res_end.apply(res, arguments);
+ };
+}
24 package.json
@@ -0,0 +1,24 @@
+{
+ "name": "access-log",
+ "description": "Add simple access logs to any http or https server",
+ "version": "0.0.0",
+ "author": "Dave Eddy <dave@daveeddy.com> (http://www.daveeddy.com)",
+ "contributors": [],
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/bahamas10/node-access-log.git"
+ },
+ "scripts": {},
+ "main": "./index.js",
+ "dependencies": {},
+ "bin": {},
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ },
+ "keywords": [
+ "access",
+ "logs"
+ ]
+}
Please sign in to comment.
Something went wrong with that request. Please try again.