Skip to content

litert/logger.js

Repository files navigation

LiteRT/Logger

npm version License node GitHub issues GitHub Releases

A logs writer for LiteRT framework.

Features

  • Log stack trace.
  • Enable/Disable log levels.
  • Custom levels.
  • Custom formats.
  • Custom output drivers.

Requirement

  • TypeScript v5.0.0 (or newer)
  • Node.js v18.0.0 (or newer)

Installation

Install by NPM:

npm i @litert/logger --save

Quick Start

Simple usage:

// The imported module is a LoggerFactory singleton object, so you can use it directly.
import LoggerFactory from "@litert/logger";

const logger = LoggerFactory.createLogger('Demo');

logger.info('Hello, world!');
logger.warning('Hello, world!');

// Now, just disable a level, and it will not be printed.
logger.setLevelOptions({ levels: ['error'], enabled: false });

logger.error('Hello, world! (WILL NOT BE PRINTED)');

// let's turn on the stack trace, print 2 lines of stack trace.
//
// without specifying levels, all levels will be affected.
logger.setLevelOptions({ traceDepth: 2 });

logger.notice('Hello, world!');

Use custom log formatter:

import LoggerFactory from "@litert/logger";

LoggerFactory.setLevelOptions({ enabled: true });

const logger = LoggerFactory.createLogger('Demo');

logger.setLevelOptions({
    formatter: function(log, subj, lv, dt, traces): string {

        if (traces?.length) {

            return `${new Date(dt).toISOString()} - ${subj} - ${lv} - ${log}

  ${traces.join('\n  ')}
`;
        }

        return `${new Date(dt).toISOString()} - ${subj} - ${lv} - ${log}`;
    }
});

logger.info('Hello, world!');
logger.warning('Hello, world!');

logger.setLevelOptions({ traceDepth: 2 });

logger.error('Hello, world!');

More Examples:

License

This library is published under Apache-2.0 license.