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

sp-uni-logger

v1.0.16

Published

The sp-uni-logger module is a wrapper on standard winston package, which exposes three different logger instance to use in the node application.

Downloads

7

Readme

sp-uni-logger

The sp-uni-logger module is a wrapper on standard winston package, which exposes three different logger instance to use in the node application.

  • Logger: to log developer logs directly from application
  • Event-logger: to log for some specific events under specific services.
  • Access-logger (Middleware): To intercept request and response, blacklist some fields before logging and log to the file

Install

You can npm install the sp-uni-logger module.

npm i sp-uni-logger --save

Usage

Initalization

You can intialize the sp-uni-logger module in a separate file passing in the required parameters in the init method exposed by tht package. For Example -

const splogger = require("sp-uni-logger");
const config = {
  eventLogger: {
    filenamePrefix: "events" // Prefix to use for the event logger file.
  },
  accessLogger: {
    logResponseBody: false, //Flag for Access Logger to log response body. Defaults to true
    logRequestBody: false, //Flag for Access Logger to log request body. Defaults to true
    filenamePrefix: "access", // Prefix to use for the access logger file.
    requestWhitelist: [
      "url",
      "headers",
      "method",
      "httpVersion",
      "originalUrl",
      "query",
      "params"
    ], //Whitelist for request fields.
    responseWhitelist: ["_header", "statusCode", "statusMessage"] // Whitelist for response fields.
  },
  logger: {
    filenamePrefix: "dev" // Prefix to use for the Dev logger file
  },
  logDir: "/var/log/sp-logger/", // Directory to store all the logs. Defaults to `/var/log/sp-logger/`
  blackList: ["x-access-token", "u-access-token", "otp", "password"] // Array of strings containing fields to mask.
};
module.exports = splogger.init(config);

Dev Logger

const splogger = require("../../utils/splogger");
splogger.info({
  first: 1,
  second: 2,
  otp: 3423 // will get masked after logging
});
splogger.error("Unauthorized transaction");

Event-logger

const splogger = require('../../utils/splogger');
const eventLogger = splogger.getEventLogger('Order'); // 'Order' is a Service name here to log data related to Orders

eventLogger.log("<EVENT_NAME>",{  
                                  reason :"<REASON>", 
                                  meta : {
                                    code: "SERVER_ERROR" / "OPS_ERROR",
                                    description: "<CX_FRIENDLY_DESCRIPTION>"
                                  } 
                               }
);

Middlewares

Access-logger (Middleware)

const { accessLoggerMW } = require("../../utils/splogger");;

const express = require("express");
const bodyParser = require("body-parser");

const app = express();

app.use(bodyParser.json());
app.use(accessLoggerMW);

app.get("/", function(req, res) {
  res.send("hello world");
});

Request Meta Details (Middleware)

This attaches Request ID and Device ID fields to all the logs.

const { addRequestMeta } = require("../../utils/splogger");

const express = require("express");
const bodyParser = require("body-parser");

const app = express();

app.use(bodyParser.json());
app.use(addRequestMeta);

app.get("/", function(req, res) {
  res.send("hello world");
});

Exposes reqId and deviceId field to the log.

User Meta Details (Middleware)

This attaches User ID to all the logs. Can be used to identidy events by a single user.

const { addUserDetails } = require("../../utils/splogger");

const express = require("express");
const bodyParser = require("body-parser");

const app = express();

app.use(bodyParser.json());
app.use(addUserDetails);

app.get("/", function(req, res) {
  res.send("hello world");
});

Exposes userID field to the log.

Blacklist fields

Following fields will get masked as default after logging. You can pass blacklist fields in the configuration object to overwrite these.

["token", "u-access-token", "otp", "password"];

Log-file directory

By default, all the logs will get stored in /var/log/sp-logger/ folder. Below is file name pattern for every logger.

  • access-%DATE%.log
  • events-%DATE%.log
  • sp-service-%DATE%.log

Notes

  • Avoid logging sensitive data i.e, otp, tokens, password in the nested level of object.
  • When using event logger, enter details in a way which can be easily understood by the CX.