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

log-analyzer

v1.1.0

Published

A lightweight and powerful log analyzer.

Downloads

8

Readme

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);
	}
});