@avanio/logger-like
v0.2.7
Published
Logger like interface and tools
Downloads
611
Readme
@avanio/logger-like
This package contains:
- A common ILoggerLike logger interface that works with console, winston, and log4js.
- A LevelLogger logger class implementation that can set log levels and acts as filter for the actual logger implementation.
- A ISetLogger ISetOptionalLogger interfaces that provides a method to set the logger.
- A MapLogger, logger class implementation that can be used to map log levels to different log keys. (works also as extended)
- A LogLevel const "enum.", getLogLevelName function, isLogLevel function and assertLogLevel function.
- A IGetLoggerLevel and ISetLoggerLevel interfaces that provides a method to get and set the log level.
See Examples for more details.
Installation
npm install @avanio/logger-like
Usage
Importing
import {ILoggerLike, LevelLogger, LogLevel} from '@avanio/logger-like';
Const: LogLevel "Enum"
The LogLevel const enum defines the following log levels and type:
const LogLevel = {
None: 0,
Trace: 1,
Debug: 2,
Info: 3,
Warn: 4,
Error: 5,
} as const;
type LogLevelValue = 0 | 1 | 2 | 3 | 4 | 5;
Function: getLogLevelName
The getLogLevelName function returns the log level name for the given log level value. The function have the following type:
function getLogLevelName(level: LogLevelValue): LogLevelKey;
// type LogLevelKey = "Trace" | "Debug" | "Info" | "Warn" | "Error"
Function: isLogLevel
The isLogLevel function validates the given log level value. The function have the following type:
function isLogLevel(value: unknown): value is LogLevelValue;
// return boolean
Function: assertLogLevel
The assertLogLevel function validates the given log level value and throws an error if the value is not a valid log level. The function have the following type:
function assertLogLevel(value: unknown): asserts value is LogLevelValue;
Interface: IGetLoggerLevel
The IGetLoggerLevel interface provides a method to get the current log level. The interface includes the following method:
interface IGetLoggerLevel {
getLogLevel(): LogLevelValue;
}
Interface: ISetLoggerLevel
The ISetLoggerLevel interface provides a method to set the log level. The interface includes the following method:
interface ISetLoggerLevel {
setLogLevel(level?: LogLevelValue): void;
}
As example this interface can be utilized something like controlling multiple services different log levels on fly.
Class: LevelLogger
The LevelLogger class is a logger implementation that can set current log levels and acts as filter for the actual logger implementation. The class implements the ILoggerLike, IGetLoggerLevel and ISetLoggerLevel interfaces. This is useful when you need to set the log level dynamically, like in internal class or one part of application by creating a new instance of the LevelLogger for that specific part of the application.
Constructor:
new LevelLogger(logger: ILoggerLike, level: LogLevelValue = LogLevel.Debug);
- logger - the actual logger implementation to use (must implement ILoggerLike interface).
- level - the initial log level (optional), defaults to LogLevel.Debug.
Examples
import {ILoggerLike, LevelLogger, LogLevel} from '@avanio/logger-like';
const consoleLogger: ILoggerLike = console;
// const log4jsLogger: ILoggerLike = log4js.getLogger();
// const winstonLogger: ILoggerLike = winston.createLogger();
// Note: log4js and winston also might define actual final log levels for output.
const logger = new LevelLogger(consoleLogger, LogLevel.Info);
logger.trace('This is a trace message');
logger.debug('This is a debug message');
logger.info('This is an info message');
logger.warn('This is a warn message');
logger.error('This is an error message');
// Output:
// This is an info message
// This is a warn message
// This is an error message
// set new log level to warn
logger.setLogLevel(LogLevel.Warn);
logger.getLogLevel(); // returns 3 = LogLevel.Warn
getLogLevelName(logger.getLogLevel()); // returns 'Warn'
or if just like to use ILoggerLike interface:
import type {ILoggerLike} from '@avanio/logger-like';
// import only type of ILoggerLike