Giter Club home page Giter Club logo

log-analyzer's Introduction

Log Analyzer

A lightweight and powerful log analyzer.

NPM

Installation

Node

$ npm install log-analyzer

Browser

<script src="js/log-analyzer.js"></script>

Documentation

new Analyzer([format], [options])

Instance an log analyzer.

options

  • separator - The separator of each logs, defaults to \n.
  • placeholder - The placeholder of empty fields, defaults to -.
  • encoding - The encoding of the analyzing file, defaults to `utf-8``.
  • ignoreMismatches - If true, ignore mismatched logs, defaults to false.

analyzer.token(name, [pattern], [type])

Define a token to match the log field.

Predefined tokens:

name pattern type
url /[^ ]+/ String
method /\w+/ String
response-time /[\d.]+/ Number
datetime /[\w-./,:+ ]+/ Date
status /\d+/ Number
referrer /[^ ]+/ String
remote-addr /[\w.,: ]+/ String
remote-user /\w+/ String
http-version /\d.\d/ Number
user-agent /.*/ String
content-length /\d+/ Number
#default# /[^ ]+/ String

analyzer.format(format)

Set up the log format, if the token in format is not matched, then use the default pattern and type.

Example:

analyzer.format(':remote-addr - :remote-user [:datetime] ":method :url HTTP/:http-version" :status :content-length');

analyzer.use([method], path)

Adds an request rule.

Example:

// Match all request methods.
analyzer.use('/index.html');
// Path with param token.
analyzer.use('GET', '/user/:name');
// Using the regular expression.
analyzer.use('POST', /\/search?q=.*/);

analyzer.overview(callback)

Adds a callback to analyze overview data, callback gets one argument (logs) where logs is an array of log objects.

Example:

analyzer.overview(function (logs) {
	var notFoundRequests = 0;
	for (var i = 0, l = logs; i < l; ++i) {
		var log = logs[i];
		if (log['status'] === 404) {
			++notFoundRequests;
		}
	}
	return {
		notFoundRequests: notFoundRequests
	};
});

analyzer.group(name, [groupBy], [calculator])

Adds a analysis group.

If groupBy is a string, analyzer will group by the field value, if it is a function, analyzer will group by the return value of it.

The calculator callback gets one argument (logs) where logs is an array of grouping logs.

Example:

// Group by the `method` field
analyzer.group('method');

// Group by the hour
analyzer.group('requestsPerHour', function (log) {
	var date = log['datetime'];
	if (date) {
		return date.getHour();
	} else {
		return '-';
	}
});

// Calculate the band width per hour
analyzer.group('bandWidthPerHour', function (log) {
	return log['datetime'].getHours();
}, function (logs) {
	var result = 0;
	for (var i = 0, l = logs.length; i < l; ++i) {
		var log = logs[i],
			contentLength = log['content-length'];
		if (contentLength) {
			result += contentLength;
		}
	}
	return result;
});

analyzer.parse(logs)

Returns a object array of parsed logs.

analyzer.analyze(logs)

Returns the analytics result of the logs.

analyzer.analyzeFile(filename, callback)

Analyze the provided log file, the callback gets two arguments (err, result).

analyzer.analyzeFile('access.log', function (err, result) {
	if (err) {
		console.error(err);
	} else {
		console.log(result);
	}
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.