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

@safaricom-mxl/logger

v0.0.15

Published

Node.js Logging Library for [MXL Console](http://mxl-console.service-availability.apps.ocpthikadev01.safaricom.net/)

Downloads

396

Readme

MXL Node.js Logger

Node.js Logging Library for MXL Console

Installation

Install the Node.js logger into your project.

npm install @safaricom-mxl/node-logger

or

yarn add @safaricom-mxl/node-logger

or

pnpm add @safaricom-mxl/node-logger

Setup

Before starting with the code, make sure to obtain your API key by logging into MXL and visiting your team page: http://mxl-console.service-availability.apps.ocpthikadev01.safaricom.net/team. This key is necessary for sending logs to your account.

Winston Transport

Create a new MXL Winston Transport and append it to your list of transports. Example:

import winston from 'winston';
import { MXLWinston } from '@safaricom-mxl/node-logger';

const mxlTransport = new MXLWinston({
  apiKey: ***MXL_API_KEY***,
  maxLevel: 'info',
  service: 'my-app',
});


const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new winston.transports.Console(),
    mxlTransport, // append this to the existing transports
  ],
});

export default logger;

Options

  • apiKey - Required. Your MXL ingestion API key.
  • service - The name of the service.
  • sendIntervalMs - Time in milliseconds to wait between retry attempts. Default: 2000 (2 sec)
  • bufferSize - The maximum number of messages the logger will accumulate before sending them all as a bulk. Default: 100.
  • timeout - The read/write/connection timeout in milliseconds. Default: 30000.

Pino Transport

Create a new MXL Pino Transport and append it to your list of transports. Example:

import pino from 'pino';

const logger = pino(
  pino.transport({
    targets: [
      {
        target: '@safaricom-mxl/node-logger/build/src/pino',
        options: {
          apiKey: ***MXL_API_KEY***,
          service: 'my-app',
        },
        level: 'info',
      },
      // other transports
    ],
  }),
);

export default logger;

Options

  • apiKey - Required. Your MXL ingestion API key.
  • service - The name of the service.
  • sendIntervalMs - Time in milliseconds to wait between retry attempts. Default: 2000 (2 sec)
  • bufferSize - The maximum number of messages the logger will accumulate before sending them all as a bulk. Default: 100.
  • timeout - The read/write/connection timeout in milliseconds. Default: 30000.

NestJS Custom Logger

(powered by nest-winston)

Import MXLNestLoggerModule into the root AppModule and use the forRoot() method to configure it.

import { Module } from '@nestjs/common';
import { MXLNestLoggerModule } from '@safaricom-mxl/node-logger';

@Module({
  imports: [
    MXLNestLoggerModule.forRoot({
      apiKey: ***MXL_API_KEY***,
      maxLevel: 'info',
      service: 'my-app',
    }),
  ],
})
export class AppModule {}

Afterward, the winston instance will be available to inject across entire project using the MXL_LOGGER_MODULE_PROVIDER injection token:

import { Controller, Inject } from '@nestjs/common';
import { MXLNestLoggerModule, MXLNestLogger } from '@safaricom-mxl/node-logger';

@Controller('cats')
export class CatsController {
  constructor(
    @Inject(MXLNestLoggerModule.MXL_LOGGER_MODULE_PROVIDER)
    private readonly logger: MXLNestLogger,
  ) { }

  meow() {
    this.logger.info({ message: '🐱' });
  }
}

Replacing the Nest logger (also for bootstrapping)

Important: by doing this, you give up the dependency injection, meaning that forRoot and forRootAsync are not needed and shouldn't be used. Remove them from your main module.

Using the dependency injection has one minor drawback. Nest has to bootstrap the application first (instantiating modules and providers, injecting dependencies, etc.) and during this process the instance of MXLNestLogger is not yet available, which means that Nest falls back to the internal logger.

One solution is to create the logger outside of the application lifecycle, using the createLogger function, and pass it to NestFactory.create. Nest will then wrap our custom logger (the same instance returned by the createLogger method) into the Logger class, forwarding all calls to it:

Create the logger in the main.ts file

import { MXLNestLoggerModule } from '@safaricom-mxl/node-logger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: MXLNestLoggerModule.createLogger({
      apiKey: ***MXL_API_KEY***,
      maxLevel: 'info',
      service: 'my-app',
    })
  });
  await app.listen(3000);
}
bootstrap();

Change your main module to provide the Logger service:

import { Logger, Module } from '@nestjs/common';

@Module({
  providers: [Logger],
})
export class AppModule {}

Then inject the logger simply by type hinting it with Logger from @nestjs/common:

import { Controller, Logger } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  constructor(private readonly logger: Logger) {}

  meow() {
    this.logger.log({ message: '🐱' });
  }
}