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

nginx-access-log

v1.1.1

Published

nginx access log profiler/parser

Downloads

36

Readme

nginx-access-log

NPM

これは、nginxのアクセスログをパース/プロファイルするnpm パッケージです。

インストール

npmまたはyarnを使用

$ npm i nginx-access-log

使用例


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ログ形式

nginxのアクセスログは、以下のような形式で出力してください。

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