@falaksingh/logger
v1.0.0
Published
A lightweight logger utility
Readme
Logger Utility
A simple and customizable logging utility for Node.js applications. This logger provides different methods for logging errors, info, and success messages with optional features like error trace, line separators, terminal colorization, and writing logs to disk.
Installation
To use this logger in your project, you can install it via npm:
npm install @falaksingh/loggerUsage:
Importing the Logger
First, import the Logger class from the module:
import Logger from '@falaksingh/logger';Creating a Logger Instance
You can create an instance of the Logger class with optional configuration:
const logger = new Logger({
parseErrorInstance: true,
enableLineSeparator: true,
enableTerminalColorizer: true,
writeToDisk: { enable: true, errorfilePath: 'logs/error.log', infoFilePath: 'logs/info.log' },
});Logging Messages
The logger provides several methods for logging different types of messages:
Error Logging
Logs error messages. If parseErrorInstance is set to true, it will also log the file path and line number where the error occurred.
logger.error(new Error('This is an error message'));
logger.error('This is a plain text error message');Info Logging
Logs informational messages.
logger.info('This is an info message');Success Logging
Logs success messages.
logger.success({ type: 'Fiat', model: '500', color: 'white' });Configuration Options
The Logger class accepts the following configuration options:
parseErrorInstance(boolean): If set totrue, the logger will include the file path and line number in error logs. Default isfalse.enableLineSeparator(boolean): If set totrue, the logger will add a line separator after each log message. Default isfalse.enableTerminalColorizer(boolean): If set totrue, log messages will be colorized in the terminal. Default isfalse.writeToDisk(object): If enabled, logs will be written to disk. Default isdisabled.errorfilePath(string): Path for storing error logs. Default is'logs/error.log'.infoFilePath(string): Path for storing info logs. Default is'logs/info.log'.enable(boolean): Enables writing logs to disk. Default isfalse.
Additional Features
Writing Logs to Disk
If writeToDisk.enable is set to true, error and info logs will be written to their respective files.
const logger = new Logger({
writeToDisk: { enable: true, errorfilePath: 'logs/error.log', infoFilePath: 'logs/info.log' },
});Terminal Colorization
If enableTerminalColorizer is set to true, logs will be styled with colors for better readability.
const logger = new Logger({ enableTerminalColorizer: true });Example
Here is a complete example of how to use the logger:
import Logger from '@falaksingh/logger';
const logger = new Logger({
parseErrorInstance: true,
enableLineSeparator: true,
enableTerminalColorizer: true,
writeToDisk: { enable: true, errorfilePath: 'logs/error.log', infoFilePath: 'logs/info.log' },
});
logger.error(new Error('This is an error message'));
logger.error('This is a plain text error message');
logger.info('This is an info message');
logger.success({ type: 'Fiat', model: '500', color: 'white' });Dependencies
This logger uses the @falaksingh/sketch package for styling the log messages.
