Skip to main content

LoggerFactory

Upgather SDK


Upgather SDK / LoggerFactory

Class: LoggerFactory

Factory for creating logger instances based on configuration. Centralizes logger instantiation logic following the Factory pattern.

Remarks​

This factory simplifies logger creation by handling the logic of choosing between custom loggers, default loggers, and silent loggers based on configuration.

The factory follows these rules:

  1. If a custom logger is provided, use it
  2. If log level is 'silent', create a SilentLogger
  3. Otherwise, create a DefaultLogger with the given configuration

Examples​

Creating a default logger:

import { LoggerFactory } from '@upgather/sdk';

const logger = LoggerFactory.createLogger(undefined, {
level: 'debug',
prefix: 'my-app'
});

Using a custom logger:

import winston from 'winston';
import { LoggerFactory, LoggerInterface } from '@upgather/sdk';

const winstonLogger = winston.createLogger({ level: 'info' });
const customLogger: LoggerInterface = {
debug: (msg, ...meta) => winstonLogger.debug(msg, ...meta),
info: (msg, ...meta) => winstonLogger.info(msg, ...meta),
warn: (msg, ...meta) => winstonLogger.warn(msg, ...meta),
error: (msg, ...meta) => winstonLogger.error(msg, ...meta),
};

const logger = LoggerFactory.createLogger(customLogger);

Constructors​

new LoggerFactory()​

new LoggerFactory(): LoggerFactory

Returns​

LoggerFactory

Methods​

createLogger()​

static createLogger(customLogger?, config?): LoggerInterface

Creates a logger instance from configuration. If a custom logger is provided, returns it; otherwise creates a default logger.

Parameters​

customLogger?​

LoggerInterface

Optional custom logger implementation

config?​

LoggerConfig

Optional configuration for the default logger

Returns​

LoggerInterface

A logger instance implementing LoggerInterface

Remarks​

The factory prioritizes custom loggers over configuration. If both are provided, the custom logger is returned and the configuration is ignored.