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

@somosphi/logger

v1.6.0

Published

A package to include request/response logging inside services

Downloads

1,358

Readme

@somosphi Logger

This is a package to add logging to request/response packages inside backend services and web applications.

Installing

Using NPM:

npm install @somosphi/logger

Usage

There are 4 ways to use this package, for now. This package can be:

  1. A simple logger that uses bunyan as logging package.
  2. A middleware for Express services, used to log all requests received by the service
  3. An interceptor for axios request package, logging all requests that axios executes
  4. An interceptor for request package, logging all requests that request executes
  5. A redact package to remove secret information from the logs

Configuration

The logger configuration (type LoggerConfig) has these properties:

|Name|Description|Type|Required| |----|-----------|----|--------| |PROJECT_NAME|The name of the project using the logger|String|true| |LOG_LEVEL|The level to start logging the messages|Log Level|false| |OMIT_ROUTES|Routes that the express middleware will not log|String[]|false| |STREAMS|Configuration for the location of the output of the log level|Stream|false|

Using require

Simple Logger

const { Logger } = require('@somosphi/logger').init({
  PROJECT_NAME: 'project-name',
});

Logger.info(JSON.stringify({
  message: 'This is a cool message',
  origin: 'Some Place',
  status: 200,
})); 
/*
  This will create the following log: 
 {"name":"project-name","hostname":"hostname.local","pid":245,"level":30,"msg":"{\"message\":\"This is a cool message\",\"origin\":\"Some Place\",\"status\":200}","time":"2019-09-10T00:42:46.361Z","v":0}
*/

Express Middleware

const { ExpressLogger } = require('@somosphi/logger').init({
  PROJECT_NAME: 'project-name',
});

/** Request/Response Logger */
app.use(ExpressLogger.onSuccess.bind(ExpressLogger));
app.use(ExpressLogger.onError.bind(ExpressLogger));

/*
  This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":298,"level":30,"msg":"{\"origin\":\"Express\",\"requrestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"}}","time":"2019-09-10T00:49:04.394Z","v":0}
*/

Axios Interceptor

const { AxiosLogger } = require('@somosphi/logger').init({
  PROJECT_NAME: 'project-name',
});

const axiosInstance = require('axios').default.create({ ...config });

AxiosLogger.attachInterceptor.bind(AxiosLogger)(axiosInstance);

/*
  This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":24654,"level":30,"msg":"{\"origin\":\"Axios\",\"requrestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"},\"method\":\"HTTP Method\",\"url\":\"https://somosphi.com\",\"data\":{\"data\":\"all data from axios\"},\"params\":{\"data\":\"params used\"},\"status\":200,\"statusText\":\"OK\"}","time":"2019-09-10T00:53:40.767Z","v":0}
*/

Request Debug

const { RequestLogger } = require('@somosphi/logger').init({
  PROJECT_NAME: 'project-name',
});
const request = require('request');

RequestLogger.attachDebug.bind(RequestLogger)(request);

/*
  This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":24654,"level":30,"msg":"{\"origin\":\"Request\",\"requrestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"},\"method\":\"HTTP Method\",\"url\":\"https://somosphi.com\",\"data\":{\"data\":\"all data from axios\"},\"params\":{\"data\":\"params used\"},\"status\":200,\"statusText\":\"OK\"}","time":"2019-09-10T00:53:40.767Z","v":0}
*/

Using import

Simple Logger

import { init } from '@somosphi/logger';

const {
  Logger,
} = init({
  PROJECT_NAME: 'project-name',
});

Logger.info(JSON.stringify({
  message: 'This is a cool message',
  origin: 'Some Place',
  status: 200,
})); 
/*
  This will create the following log: 
 {"name":"project-name","hostname":"hostname.local","pid":245,"level":30,"msg":"{\"message\":\"This is a cool message\",\"origin\":\"Some Place\",\"status\":200}","time":"2019-09-10T00:42:46.361Z","v":0}
*/

Express Middleware

import { init } from '@somosphi/logger';

const {
  ExpressLogger,
} = init({
  PROJECT_NAME: 'project-name',
});

/** Request/Response Logger */
app.use(ExpressLogger.onSuccess.bind(ExpressLogger));
app.use(ExpressLogger.onError.bind(ExpressLogger));

/*
  This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":298,"level":30,"msg":"{\"origin\":\"Express\",\"requrestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"}}","time":"2019-09-10T00:49:04.394Z","v":0}
*/

Axios Interceptor

import { init } from '@somosphi/logger';
import axios from 'axios';

const {
  AxiosLogger,
} = init({
  PROJECT_NAME: 'project-name',
});

const axiosInstance = axios.default.create({ ...config });

AxiosLogger.attachInterceptor.bind(AxiosLogger)(axiosInstance);

/*
  This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":24654,"level":30,"msg":"{\"origin\":\"Axios\",\"requrestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"},\"method\":\"HTTP Method\",\"url\":\"https://somosphi.com\",\"data\":{\"data\":\"all data from axios\"},\"params\":{\"data\":\"params used\"},\"status\":200,\"statusText\":\"OK\"}","time":"2019-09-10T00:53:40.767Z","v":0}
*/

Request Debug

import { init } from '@somosphi/logger';
import request from 'request';

const {
  RequestLogger,
} = init({
  PROJECT_NAME: 'project-name',
});

RequestLogger.attachDebug.bind(RequestLogger)(request);

/*
  This will create the following log:
{"name":"project-name","hostname":"hostname.local","pid":24654,"level":30,"msg":"{\"origin\":\"Request\",\"requrestId\":\"77215bf8-f821-4faf-bcc1-2c0260eafc66\",\"type\":\"Request or Response\",\"headers\":{\"data\":\"all headers\"},\"body\":{\"data\":\"all body\"},\"method\":\"HTTP Method\",\"url\":\"https://somosphi.com\",\"data\":{\"data\":\"all data from axios\"},\"params\":{\"data\":\"params used\"},\"status\":200,\"statusText\":\"OK\"}","time":"2019-09-10T00:53:40.767Z","v":0}
*/

Redact Logger

import { init } from '@somosphi/logger';

const {
  RequestLogger,
  Redact,
} = init({
  PROJECT_NAME: 'project-name',
});

RequestLogger.info(Redact.map({
  'password': 'secret',
}));

RequestLogger.info(Redact.map({
  'code': 'secret',
}));

Redact.addKey(/code/i);

RequestLogger.info(Redact.map({
  'code': 'secret',
}));