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

pino-express-req-logger

v1.0.7

Published

A express logging middleware, logs requests and errors.

Downloads

52

Readme

Introduction

[![NPM Version][npm-badge]][npm-url] [![NPM Downloads][npmd-badge]][npm-url] [![Build Status][travis-badge]][travis-url] [![Test Coverage][codecov-badge]][codecov-url] [![Dependencies][dependencies-badge]][dependencies-url] [![devDependencies][devDependencies-badge]][devDependencies-url] [![Known Vulnerabilities][snyk-badge]][snyk-url]

A simple logging middleware for the [express] http framework for nodejs. This module uses the [pino] logger and was inspired by the [koa-req-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 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.

Contents

Install

yarn add express-req-logger
npm install express-req-logger

Usage

TypeScript

import express = require('express');
import {NextFunction, Request, Response} from 'express-serve-static-core';
import {ExpressReqLogger, resBodySerializer} from './';

const app: express.Application = express();
const logger = new ExpressReqLogger({
    pinoOptions: {
        enabled: true,
        serializers: {
            res: resBodySerializer,
        }
    }
});
app.use(logger.getMiddleware());

app.get('/', async (req: Request, res: Response, next: NextFunction) => {
    res.status(200);
    res.json({
        data: 'Hello World!'
    });
});

app.use(logger.getErrorHandlingMiddleware());

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.