Git Product home page Git Product logo

nginx-access-log's Introduction

nginx-access-log

NPM

This is npm package for parse/profile nginx access log.

Japanese README

Installation

Using npm or yarn

$ npm i nginx-access-log

Usage

const { parse, digest } = require('nginx-access-log')

const logData = "time:10/Jul/2021:13:37:14 +0000	host:192.168.144.1	forwardedfor:-	req:GET /fetch HTTP/1.1	status:403	method:GET	uri:/fetch	size:5	referer:http://127.0.0.1/channel/1	ua:IPhone	reqtime:0.003	cache:-	runtime:-	apptime:0.003	vhost:127.0.0.1"
const logs = parse(logData)
const result = digest(logs)
console.log(result)

// [
//   {
//     count: 1,
//     count2xx: 0,
//     count3xx: 0,
//     count4xx: 1,
//     count5xx: 0,
//     min: 0.003,
//     max: 0.003,
//     sum: 0.003,
//     average: 0.003,
//     maxBody: 5,
//     minBody: 5,
//     averageBody: 5,
//     sumBody: 5,
//     method: "GET",
//     uri: '/fetch'
//   }
// ]

TypeScript

import type { Log, DigestItem } from 'nginx-access-log'
import { parse, digest } from 'nginx-access-log'

const logData: string = "time:10/Jul/2021:13:37:14 +0000	host:192.168.144.1	forwardedfor:-	req:GET /fetch HTTP/1.1	status:403	method:GET	uri:/fetch	size:5	referer:http://127.0.0.1/channel/1	ua:IPhone	reqtime:0.003	cache:-	runtime:-	apptime:0.003	vhost:127.0.0.1"
const logs: Log[] = parse(logData)
const result: DigestItem[] = digest(logs)
console.log(result)

// [
//   {
//     count: 1,
//     count2xx: 0,
//     count3xx: 0,
//     count4xx: 1,
//     count5xx: 0,
//     min: 0.003,
//     max: 0.003,
//     sum: 0.003,
//     average: 0.003,
//     maxBody: 5,
//     minBody: 5,
//     averageBody: 5,
//     sumBody: 5,
//     method: "GET",
//     uri: '/fetch'
//   }
// ]

parse

import type { Log, DigestItem } from 'nginx-access-log'
import { parse } from 'nginx-access-log'

const logData: string = "time:10/Jul/2021:13:37:14 +0000	host:192.168.144.1	forwardedfor:-	req:GET /fetch HTTP/1.1	status:403	method:GET	uri:/fetch	size:5	referer:http://127.0.0.1/channel/1	ua:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36	reqtime:0.003	cache:-	runtime:-	apptime:0.003	vhost:127.0.0.1"
const logs: Log[] = parse(logData)
console.log(logs)

// [
//   {
//     time: 2021-07-10T13:37:14.000Z,
//     host: '192.168.144.1',
//     forwardedfor: '-',
//     req: 'GET /fetch HTTP/1.1',
//     status: 403,
//     method: 'GET',
//     uri: '/fetch',
//     size: 5,
//     referer: 'http://127.0.0.1/channel/1',
//     ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
//     reqtime: 0.003,
//     cache: '-',
//     runtime: '-',
//     apptime: 0.003,
//     vhost: '127.0.0.1'
//   }
// ]

filter

import type { Log, FilterQuery } from 'nginx-access-log'
import { parse, filter } from 'nginx-access-log'

const logData: string = "time:10/Jul/2021:13:37:14 +0000	host:192.168.144.1	forwardedfor:-	req:GET /fetch HTTP/1.1	status:403	method:GET	uri:/fetch	size:5	referer:http://127.0.0.1/channel/1	ua:IPhone	reqtime:0.003	cache:-	runtime:-	apptime:0.003	vhost:127.0.0.1" + "\n" +
  "time:10/Jul/2021:13:37:14 +0000	host:192.168.144.1	forwardedfor:-	req:POST /post HTTP/1.1	status:403	method:POST	uri:/post	size:5	referer:http://127.0.0.1/channel/1	ua:IPhone	reqtime:0.003	cache:-	runtime:-	apptime:0.003	vhost:127.0.0.1"

const logs: Log[] = parse(logData)
const query: FilterQuery = { methods: ["GET"] }
const logsFilterd: Log[] = filter(logs, query)
console.log(logsFilterd)

// [
//   {
//     time: 2021-07-10T13:37:14.000Z,
//     host: '192.168.144.1',
//     forwardedfor: '-',
//     req: 'GET /fetch HTTP/1.1',
//     status: 403,
//     method: 'GET',
//     uri: '/fetch',
//     size: 5,
//     referer: 'http://127.0.0.1/channel/1',
//     ua: 'IPhone',
//     reqtime: 0.003,
//     cache: '-',
//     runtime: '-',
//     apptime: 0.003,
//     vhost: '127.0.0.1'
//   }
// ]

digest

import type { Log, DigestQuery, DigestItem } from 'nginx-access-log'
import { parse, digest } from 'nginx-access-log'

const logData: string = "time:10/Jul/2021:13:37:14 +0000	host:192.168.144.1	forwardedfor:-	req:GET /users/1 HTTP/1.1	status:403	method:GET	uri:/users/1	size:5	referer:http://127.0.0.1/channel/1	ua:IPhone	reqtime:0.003	cache:-	runtime:-	apptime:0.003	vhost:127.0.0.1" + "\n" +
  "time:10/Jul/2021:13:37:14 +0000	host:192.168.144.1	forwardedfor:-	req:GET /users/2 HTTP/1.1	status:403	method:GET	uri:/users/2	size:5	referer:http://127.0.0.1/channel/1	ua:IPhone	reqtime:0.003	cache:-	runtime:-	apptime:0.003	vhost:127.0.0.1"

const logs: Log[] = parse(logData)
const query: DigestQuery = { uriPatterns: ["/users/"] }
const result: DigestItem[] = digest(logs, query)
console.log(result)

// [
//   {
//     count: 2,
//     count2xx: 0,
//     count3xx: 0,
//     count4xx: 2,
//     count5xx: 0,
//     min: 0.003,
//     max: 0.003,
//     sum: 0.006,
//     average: 0.003,
//     minBody: 5,
//     maxBody: 5,
//     averageBody: 5,
//     sumBody: 10,
//     method: 'GET',
//     uri: '/users/'
//   }
// ]

nginx log format

output nginx logs according to the following example

http {
  log_format ltsv "time:$time_local"
    "\thost:$remote_addr"
    "\tforwardedfor:$http_x_forwarded_for"
    "\treq:$request"
    "\tmethod:$request_method"
    "\turi:$request_uri"
    "\tstatus:$status"
    "\tsize:$body_bytes_sent"
    "\treferer:$http_referer"
    "\tua:$http_user_agent"
    "\treqtime:$request_time"
    "\truntime:$upstream_http_x_runtime"
    "\tapptime:$upstream_response_time"
    "\tcache:$upstream_http_x_cache"
    "\tvhost:$host";

  access_log  /var/log/nginx/access.log ltsv;
}

nginx-access-log's People

Contributors

mrymam avatar

Stargazers

Vel avatar

Watchers

 avatar

nginx-access-log's Issues

demonstration code is not working

Run it with error as following:

import { parse, digest } from 'nginx-access-log'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

I googled a lot, tried to use some like (as in mochajs/mocha#4531)

import pkg from 'nginx-access-log';
const { parse, digest } = pkg;

Or add "type": "module", into package.json...

But the functions are missing, still.

Dont know how to make this module work.

Is there a solution?

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.