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

@avanio/logger-like

v0.2.7

Published

Logger like interface and tools

Downloads

611

Readme

@avanio/logger-like

TypeScript npm version Maintainability github action

This package contains:

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