Log Analyzer
A lightweight and powerful log analyzer.
Installation
Node
$ npm install log-analyzer
Browser
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;
analyzer.use([method], path)
Adds an request rule.
Example:
// Match all request methods.analyzer;// Path with param token.analyzer;// Using the regular expression.analyzer;
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;
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` fieldanalyzer; // Group by the houranalyzer; // Calculate the band width per houranalyzer;
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;