mainlog

1.5.2 • Public • Published

Logger

Logger with channels & domain metadata decorator

  1. Usage:
  2. Prepare:
    1. Import types;
    2. Prepare channel;
    3. Create logger;
    4. Setup logger.
  3. Commands:
    1. Call logger.

Usage

Prepare

Import types

Optional

/**
 * @typedef {import('mainlog').ILoggerChannel} ILoggerChannel
 * @typedef {import('mainlog').LoggerChannels} LoggerChannels
 * @typedef {import('mainlog').LoggerLevel} LoggerLevel
 * @typedef {import('mainlog').LoggerStringLevel} LoggerStringLevel
 */

Prepare channel

import { Logger, DomainLogger, ConsoleLoggerChannel } from 'mainlog';

/** @type {ILoggerChannel} */ 
const consoleChannel = {
  setup: () => { },

  trace: message => console.log(message),
  debug: message => console.debug(message),
  info: message => console.info(message),
  warn: message => console.warn(message),
  error: message => console.error(message),
  fatal: message => console.error(message),

  getLevels: () => Object.entries(levels)
    .reduce((acc, entries) => {
      const level = /** @type {LoggerStringLevel} */ (entries[0]);
      const isEnabled = entries[1];
      if (!isEnabled) {
        return acc;
      }

      acc.push(level);

      return acc;
    }, /** @type {LoggerStringLevel[]} */ ([])),
  setLevel: () => { },
  setLevels: () => { }
};

Create logger

/** @type {LoggerChannels} */
const channels = {
  console: consoleChannel,
  console2: new ConsoleLoggerChannel()
};

const logger = new Logger({
  channels
});

const domainLogger = new DomainLogger({
  logger
});

or

import { Logger, DomainLogger, LoggerFactory } from 'mainlog';

const loggerFactory = new LoggerFactory({
  Logger,
  DomainLogger
});

const logger = loggerFactory.create({
  channels
});

const domainLogger = loggerFactory.createDomain({
  logger
});

Setup logger

/** @type {LoggerLevel} */
const level = 'trace';
/** @type {LoggerStringLevel} */
const levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];

logger.setup({
  level
});

domainLogger.setup({
  level,
  options: {
    prefix: 'prefix',
    postfix: 'postfix',
    metadata: {
      organization: 'Organization',
      context: 'Context',
      app: 'App',

      sourceClass: 'ClassName'
    }
  }
});

or

logger.setLevels(levels);

or

logger.setLevel(level);

Commands

Call logger

logger.trace('Trace message');
logger.debug('Debug message');
logger.info('Info message');
logger.warn('Warn message');
logger.error('Error message');
logger.fatal('Fatal message');

or

const correlationId = 'requestId';

domainLogger.trace('Trace message', { metadata: { correlationId } });
domainLogger.debug('Debug message', { metadata: { correlationId } });
domainLogger.info('Info message', { metadata: { correlationId } });
domainLogger.warn('Warn message', { metadata: { correlationId } });
domainLogger.error('Error message', { metadata: { correlationId } });
domainLogger.fatal('Fatal message', { metadata: { correlationId } });

Package Sidebar

Install

npm i mainlog

Weekly Downloads

2

Version

1.5.2

License

MIT

Unpacked Size

50.4 kB

Total Files

16

Last publish

Collaborators

  • ilushling