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

@mojaloop/logging-bc-client-lib

v0.5.8

Published

Mojaloop logging bounded context client library

Downloads

107

Readme

Mojaloop vNext Nodejs Logging Client Library

Git Commit Git Releases Npm Version NPM Vulnerabilities CircleCI

This library provides usable implementations of the ILogger interface defined in @mojaloop/logging-bc-public-types-lib.

Current implementations:

  • DefaultLogger - Does structured colored logging to the console as well as to a combined.log file;
  • KafkaLogger - Does everything DefaultLogger does and also sends the logs via Kafka to the central logging services.

Do not depend on specific client logging implementations when not needed

All logger implementations like DefaultLogger of KafkaLogger implement the ILogger interface from @mojaloop/logging-bc-public-types-lib.

It is best to only use the public and stable ILogger dependency in important (domain layer) code and not real logger implementations.

Only application layer code should depended on @mojaloop/logging-bc-client-lib.

This way, domain code only depends on the stable @mojaloop/logging-bc-public-types-lib and any concrete implementations as passed via dependency injection.

For very simple logging needs the @mojaloop/logging-bc-public-types-lib library has a ConsoleLogger class that can be used.

Usage

DefaultLogger

This logger implementation will log both to the console and a "combined.log" file.

import { ILogger, LogLevel } from "@mojaloop/logging-bc-public-types-lib";
import { DefaultLogger } from "@mojaloop/logging-bc-client-lib";

const BC_NAME = "your_bounded_context_name";
const APP_NAME = "your_app_name";
const APP_VERSION = "0.0.1";
const LOGLEVEL = LogLevel.TRACE;

const logger: ILogger = new DefaultLogger(BC_NAME, APP_NAME, APP_VERSION, LOGLEVEL);

logger.trace("trace message");
logger.debug("debug message");
logger.info("info message");
logger.warn("warn message");
logger.error("error message");
logger.fatal("fatal message");

// logging errors with complete error stack
logger.error("error message with optional error object", new Error("My error obj"));

// example of metadata
logger.debug("trace message with example metadata", {metaL: "metaCt 1"});

// change the loglevel for subsequent log calls
logger.setLogLevel(LogLevel.ERROR);

// creating a child logger with a component name
// the child logger an instance of the normal logger
const childLogger: ILogger = logger.createChild("childComponent 2");

childLogger.debug("debug message");
childLogger.info("info message");

Avoiding unecessary work

All the log methods debug(), info(), etc, have a guard inside that checks the enabled log level and will ignore the call if the called log level is not enabled. For cases where the parameters of the log calls are computationally expensive, it is a good idea to prefix the log call with a logical guard expression, like so:

this._logger.isDebugEnabled() && this._logger.debug(`Something happened, message is: ${JSON.stringify(message)}`)

With this strategy, we avoid the evaluation of the JSON.stringify(message) entirely when debug loglevel is not enabled. This is specially important in places where the code might be executed repeatedly.

Note that this is this is only worth it when the parameters of the log call are computationally expensive, for simple parameters like static strings this is not needed and should be avoided to keep the code simple.

KafkaLogger

This logger implementation extends the DefaultLogger and will additionally ship the logs via Kafka to the central logging service, which will store them.

import { ILogger, LogLevel } from "@mojaloop/logging-bc-public-types-lib";
import { KafkaLogger } from "@mojaloop/logging-bc-client-lib";

const BC_NAME = "your_bounded_context_name";
const APP_NAME = "your_app_name";
const APP_VERSION = "0.0.1";
const LOGLEVEL = LogLevel.TRACE;

const kafkaProducerOptions = {
    kafkaBrokerList: "localhost:9092"
}

const kafkaLogsTopic = "logs";

let logger: ILogger;


async function start(){
    logger = new KafkaLogger(
            BC_NAME,
            APP_NAME,
            APP_VERSION,
            kafkaProducerOptions,
            kafkaLogsTopic,
            LOGLEVEL
    );
    await (logger as KafkaLogger).init();

    // use the same way as any other ILogger
    // ex:
    logger.debug("debug message");


    setTimeout(async ()=>{
        // NOTE Make sure to call KafkaLogger.destroy
        await (logger as KafkaLogger).destroy();
    }, 500);
}

start();

See also

For the details on how to split the code between interface usage and concrete implementation OR the common types definition and the simplistic ConsoleLogger implementation see the @mojaloop/logging-bc-public-types-lib library here.

Install

To install this library use:

yarn add @mojaloop/logging-bc-client-lib

OR

npm install @mojaloop/logging-bc-client-lib