dilogger
v0.2.0
Published
A simple file based logging utility for Node.js
Readme
Dilogger
A lightweight, customizable logging utility for Node.js applications. This package provides a simple and flexible way to log messages to a file with timestamps, caller information, and support for multiple log levels (INFO, DEBUG, WARNING, ERROR). It also supports custom time zones for log timestamps.
Features
- File Logging: Log messages to a file with automatic directory creation.
- Log Levels: Supports
INFO,DEBUG,WARNING, andERRORlog levels. - Custom Time Zones: Specify a time zone for log timestamps.
- Caller Information: Automatically includes the caller function name in logs.
- Flexible Configuration: Customize log file names, paths, and backup paths.
Installation
Install the package using npm:
npm install diloggerUsage
Basic Example
import { createLogger } from "dilogger";
// Create a logger instance
const logger = createLogger({
FILE_NAME: "app.log",
TIMEZONE: "America/New_York",
});
// Log messages
logger.info("This is an info message.");
logger.debug("This is a debug message.");
logger.warning("This is a warning message.");
logger.error("This is an error message.");Output in app.log
[10/15/2023, 3:45:12 PM] - (main) - [INFO] - This is an info message.
[10/15/2023, 3:45:12 PM] - (main) - [DEBUG] - This is a debug message.
[10/15/2023, 3:45:12 PM] - (main) - [WARNING] - This is a warning message.
[10/15/2023, 3:45:12 PM] - (main) - [ERROR] - This is an error message.API Documentation
createLogger(options)
Creates a new logger instance.
Parameters
options(Object, optional): Configuration options for the logger.FILE_NAME(String, optional): Name of the log file. Default:"index.log".LOG_MOD(String, optional): Log level. Must be one of"INFO","DEBUG","WARNING", or"ERROR". Default:"DEBUG".FILE_PATH(String, optional): Directory for log files. Default:"./logs/".BACKUP_FILE_PATH(String, optional): Directory for backup log files. Default:"./logs/backup/".TIMEZONE(String, optional): Time zone for log timestamps. Default:"UTC".
Returns
- A
LOGGERinstance.
LOGGER Class
Methods
info(message)- Logs an
INFOlevel message. - Example:
logger.info("This is an info message.");
- Logs an
debug(message)- Logs a
DEBUGlevel message (only ifLOG_MODis set to"DEBUG"). - Example:
logger.debug("This is a debug message.");
- Logs a
warning(message)- Logs a
WARNINGlevel message. - Example:
logger.warning("This is a warning message.");
- Logs a
error(message)- Logs an
ERRORlevel message. - Example:
logger.error("This is an error message.");
- Logs an
Configuration
Log Levels
The following log levels are supported:
INFO: General information.DEBUG: Debugging information (only logged ifLOG_MODis set to"DEBUG").WARNING: Warnings that may require attention.ERROR: Errors that need immediate attention.
Time Zones
You can specify a custom time zone for log timestamps using IANA time zone identifiers (e.g., "America/New_York", "UTC"). For a full list of valid time zones, refer to the IANA Time Zone Database.
Example
Advanced Configuration
import { createLogger } from "dilogger";
// Create a logger with custom settings
const logger = createLogger({
FILE_NAME: "app.log",
TIMEZONE: "Asia/Karachi",
LOG_MOD: "INFO",
FILE_PATH: "./custom-logs/",
BACKUP_FILE_PATH: "./custom-logs/backup/",
});
// Log messages
logger.info("Application started.");
logger.debug("This debug message will not be logged because LOG_MOD is INFO.");
logger.warning("Low disk space.");
logger.error("Failed to connect to the database.");Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Commit your changes.
- Submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Support
If you encounter any issues or have questions, please open an issue on the GitHub repository.
Enjoy logging with dilogger! 🚀
