Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Initial Commit of an apache-like access log for nodejs
  • Loading branch information
petershaw committed Jun 12, 2013
0 parents commit f9d273c
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
accesslog
==========
---

A simple apache-like access log middleware for nodejs.

Description
----------
Accesslog will generate a apache like access.log file that can produce the two main formats: CLF and EXTENDED

Usage
---------

```javascript
var accesslog = require('accesslog');
[…express...]
app.use(apachelog.logger);
```

Configuration
---------
Accesslog will produce a log file called "access.log" inside the ./logs directory of your application root.
You can overwrite this defaults by passing a configuration object into the accesslog.configure function.
```javascript
accesslog.configure({
format: 'CLF',
directory: 'logs',
filename: 'access.log'});
```

110 changes: 110 additions & 0 deletions accesslog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
var microtime = require('microtime')
, sprintf = require('sprintf').sprintf
, dateformat = require('date-format-lite')
, fs = require('fs')
, path = require('path')
, fd = undefined
;

// public exports
var accesslog = exports;
var conf = {
format: 'CLF',
directory: 'logs',
filename: 'access.log'
};

accesslog.version = '0.0.1';

accesslog.configure = function accesslogConfigure(opt) {
// writes logs into this directory
directory = (typeof opt.directory === 'string') ? opt.directory : __dirname + pathsep + 'logs';

// write log to file with name
filename = (typeof opt.directory === 'string') ? opt.filename : 'access.log';

// log format
/**
NCSA Common Log Format (CLF)
"%h %l %u %t \"%r\" %>s %b"
NCSA Common Log Format with Virtual Host
"%v %h %l %u %t \"%r\" %>s %b"
NCSA extended/combined log format
"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
NCSA extended/combined log format with Virtual Host
"%v %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
**/
conf.format = (typeof opt.directory === 'string') ? opt.format : 'CLF';
};

accesslog.logger = function log(request, response, next) {
var starttime = microtime.now();
// from behind a proxy
var clientAddr = request.headers['X-Forwarded-For'];
if( clientAddr == undefined ){
// direct connection
clientAddr = request.connection.remoteAddress;
}
// get username (if available)
var username = "-";
if(request.session.user){
username = "-";
} else {
if(request.session.id){
username = request.session.id;
}
}


if (typeof next === 'function') {
next();
}

var endtime = microtime.now(); // microseconds
var rendertime = endtime - starttime;

var now = new Date();
var p0 = sprintf("%s - %s [%s/%s/%s:%s:%s] %s",
clientAddr,
username,
now.format("DD"),
now.format("MMM"),
now.format("YYYY"),
now.format("hh"),
now.format("mm"),
now.format("ss"),
(rendertime /60/60)
);

var p1 = sprintf('"%s %s %s/%s"',
request.method,
request.url,
request.protocol.toUpperCase(),
request.httpVersion
)

var p2 = sprintf("%d %s",
200,
response._headers['content-length']
);
if(conf.format == 'CLF'){
writeToLog(p0 +" "+ p1 +" "+ p2);
} else if(conf.format == 'EXTENDED'){
writeToLog(p0 +" "+ p1 +" "+ '"'+response.req.headers['user-agent']+'"' +" "+ p2);
} else {
// default fallback
writeToLog(p0 +" "+ p1 +" "+ p2);
}
};


writeToLog = function( str ){
console.log(conf);
if(fd == undefined){
fd = fs.openSync(path.join(conf.directory, conf.filename), 'a');
}
fs.write( fd, str+"\n" );
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./accesslog');
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "accesslog",
"description": "A apache-like access log for nodejs.",
"version": "0.0.1",
"private": false,
"homepage": "https://github.com/petershaw/NodeJS-Apache-Like-AccessLog",
"repository": {
"type": "git",
"url": "https://github.com/petershaw/NodeJS-Apache-Like-AccessLog.git"
},
"author": "Peter Shaw <[email protected]> (https://github.com/petershaw/)",
"main": "index.js",
"keywords": [
"log",
"access",
"apache",
"NCSA Common Log Format",
"CLF"
],
"dependencies": {
"microtime": "~0.4.0",
"sprintf": "~0.1.1",
"date-format-lite": "0.1.1"
}
}

0 comments on commit f9d273c

Please sign in to comment.