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

@jefferywa/node-logger

v2.4.6

Published

Banyan runtime logger with output to stdout and files in JSON format

Downloads

181

Readme

README

Simple runtime logging library based on bunyan

Version npm Version git Version git

NPM

Quick Start

The recommended use of NodeLogger is to create a singleton logger instance when the server application is initialized. The easiest way to do this is to use the static Logger.create method, passing the settings object into it.

// TypeScript
import { Logger } from '@jefferywa/node-logger';
// or JavaScript
const { Logger } = require('@jefferywa/node-logger');

const logger = Logger.create({}); // Your logger instance

Logging

Logging levels

The values for the levels are taken from the Bunyan library and extended with a Z value (for JSON logging)

const levels = {
  70: 'Z', // JSON
  50: 'E', // ERROR
  40: 'W', // WARNING
  30: 'I', // INFO
  20: 'D', // DEBUG
  10: 'T', // TRACE
};

Creating your Logger instance

You get started by creating a logger using Logger.create:

const logger = Logger.create({
  name: 'EXAMPLE_PROJECT_NAME', // - Write your project name
  type: 'backend', // - Write your project type, for example `backend` or `api` 
  level: 'INFO', // - Write default logger level, in default settings it is INFO
  serializers: {
    // Your serializers
    err: function (err: any): any {
      return {
        message: JSON.stringify(err.message),
        name: err.name,
        stack: err.stack,
      };
    },
    stringData: (data: any[] | object): string => {
      return JSON.stringify(data);
    },
    secureStringData: (data: any[] | object): string => {
      const dataStr = JSON.stringify(data);
      return maskString(dataStr); // You can use functions to hide values
    },
    ...
  },
  maxMessageLength: 256, // - Write maximum log row length, this setting worked with field `isTrim`
  isTrim: true, // - If set to `true`, will enable the `Trim` mode using the `maxMessageLength` parameter, this setting working with setting 'isMapper=true' 
  isMapper: false, // If set to `true`, Mapper mode will be enabled, for a more detailed listing of the value in the entry log line
  isJSON: true, //  If set to `true`, `logger.json` method support will be enabled, by default `false`
  isGelf: false, // If set to `true`, logs will be sent to graylog via gelf, this setting working with setting 'isMapper=true' 
  gelfConfig: {
    graylogPort: 12201,
    graylogHostname: '127.0.0.1',
    connection: 'wan',
    maxChunkSizeWan: 1420,
    maxChunkSizeLan: 8154
  }
});

Serializers

Configuration object containing functions that you can use in logging mods such as info and error

// Default serializers
const serializers = {
  header: (headers) => {
    const headerList = { ...headers };

    if (headerList.cookie) {
      headerList.cookie = headerList.cookie
        .replace(HEADER_SID_REGEX, HEADER_REPLACE_PATTERN)
        .replace(HEADER_RM_REGEX, HEADER_REPLACE_PATTERN);
    }

    if (headerList.authorization) {
      headerList.authorization = HEADER_AUTHORIZATION_PATTERN;
    }

    return headerList;
  },
  req: (request) => {
    return {
      url: request.url,
      method: request.method,
      headers: NodeLogger.Serializers.header(request.headers),
    };
  },
  err: (err) => {
    return {
      name: err.name,
      message: JSON.stringify(err.message),
      stack: err.stack,
    };
  },
};

You can also extend the set of these functions through the settings object, as shown above in the creation of a logger instance

Logger methods

logger.info('Your info log string'); // For logging string value
// {"@timestamp":"2022-08-12T15:15:30.999Z","name":"EXAMPLE_PROJECT_NAME","type":"backend","hostname":"notebook.local","pid":18585,"time":"2022-08-12T15:15:30.999Z","v":0,"level":"I","msg":"Your info log string","level_number":30}

logger.json({stringData: {data: {message: 'your data'}}}, 'Your log string'); // For logging json values
// {"@timestamp":"2022-08-12T15:15:30.999Z","name":"example","type":"example","hostname":"notebook.local","pid":18585,"stringData":"{\"data\":{\"message\":\"your data\"}}","time":"2022-08-12T15:15:30.999Z","v":0,"level":"Z","msg":"Your log string","level_number":70}

logger.error({err: {name: 'Error', message: 'Error message', stack: "Error: Error message stack trace" }}, 'Your error log string'); // For loggin errors
// {"@timestamp":"2022-08-14T17:01:24.499Z","name":"example","type":"example","hostname":"notebook.local","pid":18585,"err":{"message":"\"Error message\"","name":"Error","stack":"Error: Error message stack trace"},"time":"2022-08-14T17:01:24.498Z","v":0,"level":"E","msg":"Your error log string","level_number":50}

logger.warn('Your warning log string'); // For logging warnings
// {"@timestamp":"2022-08-14T17:04:16.330Z","name":"example","type":"example","hostname":"notebook.local","pid":18585,"time":"2022-08-14T17:04:16.330Z","v":0,"level":"W","msg":"Your warning log string","level_number":40}

// etc. As well as all the methods supported by bunyan

Installation

npm install @jefferywa/node-logger
yarn add @jefferywa/node-logger

TypeScript support

npm install @types/bunyan
yarn add @types/bunyan

Author: JefferyWa (Vsevolod Golubinov)