@spreeloop/logging
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

Logger Interface

Version License: MIT

The Logger interface provides a simple yet effective way to manage logging in your JavaScript or TypeScript applications. With customizable log levels and printers, you can control how log messages are handled and displayed.

Features

  • Log Levels: The Logger supports different log levels, including DEBUG, INFO, WARN, and ERROR. This allows developers to control the verbosity of the logs.
  • Custom Printer: The Logger uses a custom LogPrinter function to output log messages. This function can be configured to print logs to various destinations, such as the console, a file, or a remote logging service.
  • Named Loggers: Each instance of Logger can be named, which helps in identifying the source of log messages.

Installation

You can install the Logger interface via npm:

npm install logger-interface

Run tests

npm run test

Usage

Import the Logger class into your JavaScript or TypeScript file:

import { Logger } from 'logger-interface';

Create an instance of the Logger class with an optional name parameter:

const logger = new Logger('MyLogger');

Log messages using the available log level methods: log, info, warn, and error:

logger.log('This is a debug message');
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message');

Configuration

Log Levels

You can set the log level for the entire application using the Logger.level static property. The available log levels are:

  • LogLevel.DEBUG
  • LogLevel.INFO
  • LogLevel.WARN
  • LogLevel.ERROR
  • LogLevel.SILENT (to disable printing)

Custom Printer

You can define a custom printer function using the Logger.printer static property. This function will be called whenever a log message needs to be printed. By default, log messages are printed to the console.

Example

import { Logger, LogLevel } from 'logger-interface';

Logger.level = LogLevel.DEBUG;

const customPrinter = (log) => {
  // Implement your custom printing logic here
  console.log(`[${log.level}] [${log.name}]: ${log.message}`);
};

Logger.printer = customPrinter;

const logger = new Logger('MyLogger');

logger.log('This is a debug message');
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message');

Author

👤 Spreeloop

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

Readme

Keywords

Package Sidebar

Install

npm i @spreeloop/logging

Weekly Downloads

34

Version

0.1.3

License

MIT

Unpacked Size

19.4 kB

Total Files

17

Last publish

Collaborators

  • spreeloop_ludovic
  • webanalyse237
  • ktabouguia
  • steevecabrel
  • pakona