@spreeloop/logging
v0.1.3
Published
spreeloop core logging package
Readme
Logger Interface
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
Loggersupports different log levels, includingDEBUG,INFO,WARN, andERROR. This allows developers to control the verbosity of the logs. - Custom Printer: The
Loggeruses a customLogPrinterfunction 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
Loggercan be named, which helps in identifying the source of log messages.
Installation
You can install the Logger interface via npm:
npm install logger-interfaceRun tests
npm run testUsage
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.DEBUGLogLevel.INFOLogLevel.WARNLogLevel.ERRORLogLevel.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
- Website: spreeloop.com
- Github: @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!
