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

koa-json-log

v2.0.2

Published

Simple Koa JSON Logging Middleware with development mode

Downloads

266

Readme

koa-json-log

This is a simple Koa middleware that outputs HTTP requests and response details as JSON data. During development, logs are shown in a human-readable format.

Features

  • Zero-dependency. Logs are output to stdout.
  • Tools like PM2 can then be used to write logs to files if needed.
  • Development Mode (when NODE_ENV == development), shows human-readable logs.
  • TypeScript definitions

Sample Output when NODE_ENV == development

200 GET / - 4ms
404 GET /favicon.ico - 1ms
500 GET /test - 24ms
Error: Ack nooo!!
    at router.get (/project/src/server/server.ts:18:11)
    at dispatch (/project/node_modules/koa-router/node_modules/koa-compose/index.js:44:32)
    at next (/project/node_modules/koa-router/node_modules/koa-compose/index.js:45:18)
    at /project/node_modules/koa-router/lib/router.js:346:16
    ...
200 GET /static/logo.png - 4ms

Sample JSON Output (when NODE_ENV != development)

(JSON data shown across multiple lines for readability)

{
  "timestamp": "2018-05-04T12:22:14.412Z",
  "method": "GET",
  "url": "/",
  "query": {},
  "remoteAddress": "127.0.0.1",
  "host": "localhost:3000",
  "userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
  "statusCode": 200,
  "responseTime": 7
}
{
  "timestamp": "2018-05-04T12:25:23.125Z",
  "method": "GET",
  "url": "/test",
  "query": {},
  "remoteAddress": "127.0.0.1",
  "host": "localhost:3000",
  "userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36",
  "errorMessage": "Ack nooo!!",
  "errorStack": "Error: Ack nooo!!\n    at router.get (/src/server/server.ts:18:11)\n  ...",
  "statusCode": 500,
  "responseTime": 18
}

Installation

npm install koa-json-log

Usage

We recommend registering jsonLog() as one of the first app.use() calls, to make sure all requests and errors are logged as expected.

import * as Koa from 'koa';
import { jsonLog } from 'koa-json-log';

const app = new Koa();
app.use(jsonLog());
...

Options

The following options can be passed to jsonLog() (as an object):

  • json - boolean - enable / disable JSON format (by default this is based on NODE_ENV)
  • onLog - function - you can override this function to intercept and modify the log data object before it is written to the log.
  • logFn - function - override this function to redirect your log output. It is called with every log line. By default this is set to process.stdout.write.

Error Details

When throwing errors from middleware, jsonLog() will pick up and log the following properties from Error objects:

  • message - the error message
  • expose - a boolean indicating whether the error message should be returned to the client in the HTTP response
  • status - the http status to return
  • stack - the error stack trace (never returned to the client)
  • data - additional JSON data to log (never returned to the client)

TODO List

  • More options
  • Probably some other things!

Pull requests welcome!

License

MIT