Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-req-logger

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-req-logger

A koa logging middleware, logs requests and errors. Also adds date and x-response-time to response headers.

  • 1.5.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Introduction

NPM Version NPM Downloads Build Status Test Coverage Dependencies devDependencies Known Vulnerabilities

A simple logging middleware for the koa http framework for nodejs. This module uses the pino logger and was inspired by the koa-pino-logger module. This module was created with typescript, and is compatible with both javascript and typescript projects.

As well as logging requests and providing a log object in requests, this module also sets the HTTP Headers Date, X-Response-Time and X-Request-ID.

  • X-Request-ID Header is set to a new uuid or the value of the X-Request-ID header sent with the request. This allows requests to be tracked through microservices.
  • Date Header is set to the date and time that the request was received.
  • X-Response-Time Header is set to the response time of the request in milliseconds.

Contents

Install

yarn add koa-req-logger
npm install koa-req-logger

Usage

For a full API Reference see the documentation here⇗.

TypeScript

import Koa from 'koa';
import { KoaReqLogger, KoaReqLoggerOptions } from 'koa-req-logger';

const opts: KoaReqLoggerOptions = {
  disableIdHeader: false;
};

const app = new Koa();
const logger = new KoaReqLogger(opts);
app.use(logger.getMiddleware());

app.use((ctx, next) => {
  ctx.log.info('Some Log Message');
  ctx.log.warn({ obj: 'object' }, 'Log a message with an object');

  ctx.throw(400, 'Bad Request');
});

app.listen();

JavaScript

const Koa = require('koa');
const { KoaReqLogger } = require('koa-req-logger');

const app = new Koa();

const logger = new KoaReqLogger();
app.use(logger.getMiddleware());

app.use((ctx, next) => {
  ctx.log.info('Some Log Message');
  ctx.log.warn({ obj: 'object' }, 'Log a message with an object');

  ctx.throw(400, 'Bad Request');
});

app.listen(3000);

Output

Produces a similar output to the following json, which can then be parsed with pino's shell utility to pretty-print the output.

{"level":30,"time":1532251116578,"msg":"::1 - GET /","pid":4992,"hostname":"server.local","id":"ff0bae4b-b067-4cd6-8b99-5d221e74c515","req":{"method":"GET","url":"/","headers":{"host":"localhost:3000","connection":"keep-alive","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","accept-encoding":"gzip, deflate, br","accept-language":"en-GB,en-US;q=0.9,en;q=0.8"}},"startDate":"Sun, 22 Jul 2018 09:18:36 GMT","v":1}
{"level":30,"time":1532251116579,"msg":"Some Log Message","pid":4992,"hostname":"server.local","id":"ff0bae4b-b067-4cd6-8b99-5d221e74c515","v":1}
{"level":40,"time":1532251116579,"msg":"Log a message with an object","pid":4992,"hostname":"server.local","id":"ff0bae4b-b067-4cd6-8b99-5d221e74c515","obj":"object","v":1}
{"level":50,"time":1532251116583,"msg":"::1 - GET / - 400 4ms","pid":4992,"hostname":"server.local","id":"ff0bae4b-b067-4cd6-8b99-5d221e74c515","res":{"status":400,"headers":{"x-request-id":"ff0bae4b-b067-4cd6-8b99-5d221e74c515","date":"Sun, 22 Jul 2018 09:18:36 GMT","x-response-time":"4ms","content-type":"application/json; charset=utf-8"}},"err":{"type":"ClientError","message":"Bad Request","stack":"BadRequestError: Bad Request\n    at Object.throw (/home/drbarnabus/Development/test-service/node_modules/koa/lib/context.js:96:11)...","status":400,"statusCode":400,"expose":true},"responseTime":4,"startDate":"Sun, 22 Jul 2018 09:18:36 GMT","v":1}

Test

yarn test
npm test

License

Licensed under MIT.

Keywords

FAQs

Package last updated on 02 Aug 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc