Skip to content

API Documentation

Abir Bhushan edited this page Jul 9, 2020 · 11 revisions

WDYM

What do you mean?

Overview: Converts Common Log Format (CLF) into more useful JSON and/or CSV.

Internal implementation: A custom (Duplex) Transform stream.

Getting Started:

When you want to convert CLF to CSV:

const wdymCSV = require('wdym').csv

When you want to convert CLF to JSON:

const wdymJSON = require('wdym').json

API methods

Class: WDYM

Kind: global class, no constructor β€” instance method(s) can only be used through its child classes.

Parent: This class inherits from stream.Transform, allowing the objects of its child classes to be used in pipes and similar stream operations.

wdym.isCLF(line, [options]) β‡’ Array

Matches the provided line to standardised (ASCII) CLF Log format. If in strict mode, returns the full match only after validation of each log's components (such as remote host and HTTP status code).

Kind: instance method of WDYM

Param Type Description
line String the string to match
[options] Object
[options.strict] Boolean enables strict mode

Return value: returns an Array whose contents match the schema below (if the line is in CLF) or null.

Example:

const wdymCSV = require('wdym').csv
const clf = '127.0.0.1 - g [27/Apr/2012:11:27:36 +0700] "GET /ss.html HTTP/1.1" 200 2326'

const match = wdymCSV.isCLF(clf, { strict: true })
//=> [ 
//  '127.0.0.1 - g [27/Apr/2012:11:27:36 +0700] "GET /ss.html HTTP/1.1" 200 2326',
//  '127.0.0.1',
//  '-',
//  'g',
//  '27/Apr/2012:11:27:36 +0700',
//  'GET /ss.html HTTP/1.1',
//  '200',
//  '2326',
//  ...
// ]

Class: WDYM_CSV

Kind: global class

Parent: WDYM

wdym.toCSV(clf) β‡’ Array

Parses the CLF logs and converts them to CSV.

Kind: instance method of WDYM_CSV

Param Type Description
clf String the CLF logs β€” separated by the newline character

Throws:

  • IncorrectFormatError - when one (or more) of the logs is not in CLF
  • ValidationError - when one (or more) of the logs' components fail to validate

Return value: the logs as a single CSV string.

Example:

const wdymCSV = require('wdym').csv
const clf = '127.0.0.1 - g [27/Apr/2012:11:27:36 +0700] "GET /ss.html HTTP/1.1" 200 2326'

const csv = wdymCSV.toCSV(clf)
//=> 
//  REMOTE HOST,REMOTE LOG NAME,USER ID,DATE,REQUEST,HTTP STATUS CODE,SIZE
//  127.0.0.1,-,g,Fri Apr 27 2012 09:57:36 GMT+0530 (India Standard Time),GET /ss.html HTTP/1.1,200,2326

Class: WDYM_JSON

Kind: global class

Parent: WDYM

wdym.toJSON(clf) β‡’ Array

Parses the CLF logs and converts them to JSON.

Kind: instance method of WDYM_JSON

Param Type Description
clf String the CLF logs β€” separated by the newline character

Throws:

  • IncorrectFormatError - when one (or more) of the logs is not in CLF
  • ValidationError - when one (or more) of the logs' components fail to validate

Return value: the logs as a single JSON object that corresponds to the schema in the example below

Example:

const wdymJSON = require('wdym').json
const clf = '127.0.0.1 - g [27/Apr/2012:11:27:36 +0700] "GET /ss.html HTTP/1.1" 200 2326'

const json = wdymJSON.toJSON(clf)
//=> {
//  "log": [
//    {
//      "remoteHost": "127.0.0.1",
//      "remoteLogName": "-",
//      "authUser": "g",
//      "date": "2012-04-27T04:27:36.000Z",
//      "request": "GET /ss.html HTTP/1.1",
//      "status": 200,
//      "size": 2326
//    }
//  ]
// }

Notes

more on .isCLF(line) return value:

[ 0: input,

1: remote host/IP Address of the client,

2: RFC 1413 identity of the client,

3: user ID,

4: date,

5: request,

6: HTTP status code,

7: size of the object returned to the client (in bytes),

...: miscellaneous ]