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

@zebpay/colt

v2.0.0

Published

LoggerSDK for Microservices with multiple adapters eg: Pino, Winston, Bunyan with support for [Mapped Diagnostics Context](http://logback.qos.ch/manual/mdc.html).

Downloads

182

Readme

@zebpay/colt

LoggerSDK for Microservices with multiple adapters eg: Pino, Winston, Bunyan with support for Mapped Diagnostics Context.

Installation

  npm install @zebpay/colt --save

Configuration Required

  process.env.SERVICE = 'Payment'; // Name of the microservice
  process.env.LOG_ADAPTER = 'pino'; // any one of the pino, winston, bunyan values
  process.env.LOG_LEVEL = 'debug';
  process.env.LOG_PATH = 'logs'; // log directory path
  process.env.LOG_FILE = 'payment.log'; // log file name
  process.env.LOG_ONLY_STDOUT = 'true'; // to skip writing logs to file supports only `pino` adapter.

Usage with VaniallJS

  • Step1 : Configure
  // Entry Point of your Microservice
  // server.js
  const { Logger } = require('@zebpay/colt');

  //...other initialization code
  function configureLogger() {
    const logOptions = {
      level: process.env.LOG_LEVEL,
      logPath: process.env.LOG_PATH,
      logFile: process.env.LOG_FILE,
  };

  Logger.setLoggerOptions(logOptions);
  Logger.addAdapter(process.env.LOG_ADAPTER, Logger.setAdapter(process.env.LOG_ADAPTER));
}

// should be configured once hence in entry point file
configureLogger();

// create logger
const logger = new Logger(__filename);
logger.debug('debug message');

Usage with Typescript

import { Logger, ILogOptions } from '@zebpay/colt';

function configureLogger() {
  const logOptions:ILogOptions = {
    level: process.env.LOG_LEVEL,
    logPath: process.env.LOG_PATH,
    logFile: process.env.LOG_FILE,
  };
  Logger.setLoggerOptions(logOptions);
  Logger.addAdapter(process.env.LOG_ADAPTER, Logger.setAdapter(process.env.LOG_ADAPTER));

  // create logger
  const logger = new Logger(__filename);
  logger.debug('debug message');
}
  • Step2 : Create instances
// mymodule.js
const Logger = require('@zebpay/colt');
const logger = new Logger('mymodule');
logger.debug('debug');
logger.info('info');
logger.error('Error occurred', new Error('some error'));
logger.log('level', msg, arbitarydata, correlationIdJson)
logger.fatal('DatabaseException', errorInstance);

NOTE: scope will be truncated to first 6 chars for formatting.

Application Errors vs System Errors

  • Application Errors: Errors which are caused by any validation error or any cases which do not satisfy the business rules are categorized as application error. Always use logger.error for logging application logs. eg: userIsNotAuthenticated, recordsNotFound
  • System Errors: Errors which are unhandled eg: DatabaseException, DatabaseConnectionLost or any syntax errors that may arise at runtime. Always use logger.fatal

CorrelationIds

In order to add the correlationIds often known as MDC (Mapped Domain Context or Context Mapping) related to specific event it can be supplied to any of the loggers API as the last argument. CorrelationId should be a valid JSON Object. Incase if correlationIds are not passed on default correlationIds assigned during instantiation will be used.

Motive for implementing MDC

Often when there are lot of microservices and logs are been forwarded it becomes difficult to find the reason for error and sequence of events that might have caused the error. MDC approach helps to group together the events that are related to specific event for eg. Order Checkout failed or Payment Failure on Ecommerce site.As customer is unaware of the internal things it is wise idea to add some related information as correlationIds like { orderID: 101 } so it can be searched quickly.

Usage of CorrelationIds

 processPayment(orderData) {
   logger.debug(`Processing payment for orderID ${orderData.id}`, { orderId: orderData.id });
 }

Run Examples

  node examples server.js

Setup Requirement For New Relic Integration

  • Install td-agent 4 Installation
  • Configure td-agent.conf in /etc/td-agent/td-agent.conf
<source>
  @type tail
  path /tmp/logs/payment.log
  tag payment.stdout
  pos_file /tmp/payment.log.pos
  <parse>
    @type json
  </parse>
</source>

<match payment.stdout>
  @type http
  log_level debug
  endpoint https://log-api.eu.newrelic.com/log/v1
  http_method post
  content_type application/json
  headers {"x-license-key":"ENTER_LICENSE_KEY" }
  <format>
      @type json
  </format>
  <buffer>
    flush_interval 2s
  </buffer>
</match>

HappyCoding :bowtie: