1.0.7 • Published 9 months ago
avlogs v1.0.7
avlogs
A simple and configurable logging utility for Node.js applications. It supports different log levels, log rotation based on size or time, and configurable log file organization.
Features
- Supports log levels:
debug,info,warn, anderror. - Logs can be written to different files based on severity or combined into one file.
- Log rotation strategies: by size, daily, weekly, or monthly.
- Configurations are loaded from a
.envfile in the root directory. - Logs include timestamps and the package name.
- Option to log messages to the console.
Installation
npm install avlogsConfiguration
Create a .env file in the root directory of your project with the following variables:
# Logging level: debug, info, warn, error
LOG_LEVEL=debug
# Directory where logs will be stored
LOG_DIR=logs
# Separate logs: Errors & Warnings in one file, Info & Debug in another
LOG_FILE_ERROR_WARN=errors.log
LOG_FILE_INFO_DEBUG=info_debug.log
# Combined log file (for all logs together)
LOG_FILE_ALL=combined.log
# Log rotation strategy: "size", "daily", "weekly", "monthly"
LOG_ROTATION=daily
# Maximum file size before rotation (in bytes)
LOG_MAX_SIZE=1048576 # 1MB
# Log to console? (true/false)
LOG_CONSOLE=true
# Log file combination mode:
# "all" = all logs in one file
# "error_warn" = errors & warnings in one file, info & debug in another
LOG_COMBINATION=error_warnUsage
Importing and Using AVlogs
require("dotenv").config();
const LogRecorder = require("./avlogs");
const logger = new Avlogs();
logger.info("Application started successfully");
logger.debug("Debugging mode active");
logger.warn("Low memory warning");
logger.error("Unhandled exception occurred");Running the sample test script
Sample test script inside _test_/test.js:
require("dotenv").config();
const LogRecorder = require("../avlogs");
const logger = new Avlogs();
logger.info("Application started successfully");
logger.debug("Debugging mode active");
logger.warn("Low memory warning");
logger.error("Unhandled exception occurred");Run sample test script:
node testLog File Organization
Depending on the .env configuration:
- All logs in one file: Set
LOG_COMBINATION=all, logs are written tocombined.log. - Separate logs for errors/warnings and info/debug: Set
LOG_COMBINATION=error_warn, logs are written toerrors.logandinfo_debug.log.
License
This project is open-source and available under the MIT license.