1.0.3 • Published 8 months ago

koa-pretty-logger v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

koa-pretty-logger

Koa simple middleware for logging the entire HTTP call flow.

Installation

yarn add koa-pretty-logger

or

npm install koa-pretty-logger

Usage

Simple Usage

const koa = require('koa');
const logger = require('koa-pretty-logger');

const app = new koa()

app.use(logger());

Or, if you use ES6 Modules:

import koa from 'koa';
import logger from 'koa-pretty-logger';

const app = new koa()

app.use(logger());

Output:

curl localhost

request
{
  time: '2021-10-31T09:41:27.981Z',
  timestamp: 1635586887981,
  name: 'my-app-package-name',
  version: '1.0.0',
  ip: '::1',
  method: 'GET',
  url: '/v1/users'
}

/* request processing... */

response
{
  time: '2021-10-31T09:41:28.556Z',
  timestamp: 1635586888556,
  name: 'my-app-package-name',
  version: '1.0.0',
  ip: '::1',
  method: 'GET',
  url: '/v1/users',
  status: 200,
  response_size: '105b',
  response_time: '577ms'
}

Log with custom fields

import koa from 'koa';
import logger from 'koa-pretty-logger';

const app = new koa()

// log with the additional fields "customer" and "auth_key"
app.use(
  logger({
    addFields: (ctx) => ({
      customer: 'my-customer',
      auth_key: ctx.request.headers['x-auth_key'] 
    })
  })
);

Exclude log

import koa from 'koa';
import logger from 'koa-pretty-logger';

const app = new koa()

// exlude healtcheck log
app.use(
    logger({
      exclude: (ctx) => ctx.path.includes('healthcheck'),
    })
  );

Options

  • logger: function to log info with, default is console.log
  • addFields: function receiving ctx as argument and returning additional properties to log
  • exclude: function receiving ctx as argument and returning bool indicating whether to not log request
1.0.3

8 months ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.0

3 years ago