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

@gromo-fintech/log4g

v1.0.15

Published

`gromo-log4g-js` is a versatile and easy-to-use logging framework for JavaScript and Nest.js applications. It provides powerful logging capabilities to help developers track and debug their applications efficiently.

Downloads

1,132

Readme

@gromo-fintech/log4g

gromo-log4g-js is a versatile and easy-to-use logging framework for JavaScript and Nest.js applications. It provides powerful logging capabilities to help developers track and debug their applications efficiently.

Features

  • Customizable Logging Levels: Configure different log levels (e.g., DEBUG, INFO, WARN, ERROR) according to your application's needs.

  • Log Formats: Choose between formatting logs a text or json

  • Log Meta Data: Logs have meta data useful for debugging like: methodName, lineNumer, className, filePath, timestamp etc.

  • Saves Logs as stdout or files: Saves logs as stdout / stderr or save as file depending upon config. When storing logs in a file you can choose between storing in a single file, or bifurcated based on log levels. All logs files are stored at ~/.gromo-logger/<project-name>/.

  • Plug and Play Access Logs: store logs of every API request that reaches your micro-service. Track host-ip, requester ip, response time of each API alongside trace-id as part of access logs

Installation

You can install log4g.js via npm:

npm install @gromo-fintech/log4g

Usage

Basic Usage

// import logger
import { logger, LogLevel, LogFormat } from '@gromo-fintech/log4g';

// configure logger [example config]
logger.setConfig({
    enableStdout: true, // if true, prints logs on stdout and stderr
    nameOfProject: "gromoinsure-insurance", // name of your project. Used to creating log files.
    fileOptions: {
      enableFile: true, // if true, write logs to a file
      logLevel: LogLevel.INFO,// define log level to track for files
      datePattern: "DD-MM-YYYY",
      zippedArchive: false, // if true, log files are archived
      maxSize: "10k", // max size of a log file. Set to 10 KB here. 
      maxDuration: "7d", // max duration after which log rotation starts. Set to 7 days here.
    },
    logLevel: LogLevel.INFO,
    logFormat: LogFormat.TEXT,// Choose between TEXT and JSON to format logs accordingly
    transporterType: TransporterType.SINGLE_FILE, // Choose between SINGLE_FILE and BIFIRUCATED_BY_LOG_LEVEL
    overrideConsole: true, // if true, it overrides existing `console.log` in your project to log4g's implementation
    enableAccessLog?: true, // if true, stores access logs using interceptor / middleware (needs to be attached)
})

Configuration

gromo-log4g.js can configured to your needs.

| key | values | example | what it does | | |---------------------------|-----------------------------------------------|-----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---| | enableStdout | true / false | true | if true, prints logs on stdout and stderr | | | nameOfProject | any string | "testingProject" | name of your project. Used to creating log files. | | | fileOptions .enableFile | true / false | true | if true, write logs to a file | | | fileOptions.logLevel | info / debug / warn / error (use enum) | LogLevel.DEBUG | define log level to track for files | | | fileOptions.datePattern | string like "YYYY-MM-DD" or "DD-MM-YYYY" | "DD-MM-YYYY" | define date pattern to follow for logs | | | fileOptions.zippedArchive | true / false | true | if true, log files are archived | | | fileOptions.maxSize | size as string alongside unit. | "10k" | max size of a log file. Set to 10 KB here. | | | fileOptions.maxDuration | max duration after which log rotation starts. | "7d" | max duration after which log rotation starts. Set to 7 days here. | | | logLevel | info / debug / warn / error (use enum) | | Overall log level (for stdout), depends | | | logFormat | 'text' / 'json' (use enum) | LogFormat.TEXT | Defines how logs are printed, Choose between TEXT or JSON | | | transporterType | 'single_file' / 'bifurcated_by_log_level' | TransporterType.SINGLE_FILE | Defines how logs files are structured.Choose between SINGLE_FILE and BIFIRUCATED_BY_LOG_LEVEL SINGLE_FILE -> All application logs are dumped into a single file. BIFIRUCATED_BY_LOG_LEVEL -> Separate log file is created for each log level. | | | overrideConsole | true / false | true | if true, it overrides existing console.log in your project to log4g's implementation | | | enableAccessLog | true / false | true | if true, stores access logs using interceptor / middleware (needs to be attached) | |

Attaching middleware / interceptor for access logging

In order to store access logs and extract request scope information like trace-id, host-ip, requester-ip etc you need to attach a middleware / interceptor provided by this library to your project.

For Nest.js projects

For projects implemented using nest.js framework you can use the LoggerInterceptorNest class and provide it in the provider in app.module.ts with the APP_INTERCEPTOR under provide and LoggerInterceptorNest under useClass as shown below:

import { LoggerInterceptorNest } from '@gromo-fintech/log4g';
import { APP_INTERCEPTOR } from '@nestjs/core';
@Module({
  imports: [],
  controllers: [AppController],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggerInterceptorNest
    },
    AppService
  ],
})
export class AppModule {}

or you can have it for specific controllers with:

@UseInterceptors(LoggerInterceptorNest)

For Express.js projects

For projects implemented in javascript with express.js as their core server engine you can use the provided requestMiddleware function and use as middleware:

const { logger, LogFormat, LogLevel, TransporterType, ExpressMiddleware } = require("@gromo-fintech/log4g");
const loggerOptions = {
  enableStdout: true ,
  nameOfProject : "payoutgrid",
  fileOptions: {
      enableFile: true,
      logLevel : LogLevel.INFO,
      datePattern: 'DD-MM-YYYY',
      zippedArchive: false,
      maxSize: '10k', 
      maxDuration: '1d',
  },
  logLevel : LogLevel.INFO,
  logFormat : LogFormat.JSON,
  transporterType : TransporterType.BIFIRUCATED_BY_LOG_LEVEL,
  overrideConsole : true,
  enableAccessLog : true,
};
logger.setConfig(loggerOptions);
const loggingMiddleware = new ExpressMiddleware();
app.use(loggingMiddleware.requestMiddleware);

Remember to do this before initializing all routes.

For Fastify projects

For projects implemented in javascript with fastify as their core server engine you can use the provided requestMiddleware, responseMiddleware, and errorMiddleware hooks:

const { logger, LogFormat, LogLevel, TransporterType, FastifyMiddleware } = require('@gromo-fintech/log4g');
const loggerOptions = {
  enableStdout: true,
  nameOfProject : "gromoinsure-insurance",
  fileOptions: {
      enableFile: true,
      logLevel : LogLevel.INFO,
      datePattern: 'DD-MM-YYYY',
      zippedArchive: false,
      maxSize: '10k', 
      maxDuration: '1d',
  },
  logLevel : LogLevel.INFO,
  logFormat : LogFormat.JSON,
  transporterType : TransporterType.BIFIRUCATED_BY_LOG_LEVEL,
  overrideConsole : true,
  enableAccessLog : true,
};
logger.setConfig(loggerOptions);

const fastifyMiddleware = new FastifyMiddleware();
fastify.addHook('preHandler', fastifyMiddleware.requestMiddleware);
fastify.addHook('onResponse', fastifyMiddleware.responseMiddleware);
fastify.addHook('onError', fastifyMiddleware.errorMiddleware);

The above changes will enable access logs under ~/.gromo-logger/<project-name>/access_logs and add trace id, requester-ip, URI path, API method, API response status code, and host-ips to application logs as well.

Authors

Rohan Nagariya

Intern - SDE

Rohan Nagariya is an intern at GroMo who has come from DTU and has written majority of the code for this package.

Paramdeep Singh Obheroi

Senior Software Engineer

Paramdeep is a Senior Engineer at GroMo. He has designed and reviewed the package alongside mentoring Rohan in building this package.

Sahej Aggarwal

Engineering Manager

Sahej is the Engineering Manager at GroMo who has initiated the project.