acidlog
v1.0.0
Published
A lightweight persistent logger tool using SQLite for storing logs with optional retention and max entries limit.
Maintainers
Readme
acidlog
Canonical URL:
https://alexstevovich.com/a/acidlog-nodejs
Software URL:
https://midnightcitylights.com/software/acidlog-nodejs
Acidlog is a lightweight persistent logger built using SQLite for efficient storage and management of logs. It provides features like automatic log retention, log pruning based on maximum entries, and support for custom loggers.
Installation
npm install acidlogExample
import AcidLog from 'acidlog';
// Create an instance with a custom retention period and max entries limit
const logger = new AcidLog('./logs/logs.db', {
retentionDays: 7,
maxEntries: 5000,
});
// Log entries at different levels
logger.info('System started');
logger.warn('Potential issue detected');
logger.error('Critical error occurred');
// Get the most recent 100 logs
const recentLogs = logger.getRecent(100);
console.log(recentLogs);
// Get logs by level
const errorLogs = logger.getByLevel('error', 50);
console.log(errorLogs);API
new AcidLog(filePath, options?)
Creates an instance of the AcidLog logger.
Parameters:
filePath(string): Path to the SQLite database file where logs will be stored.options(object, optional):retentionDays(number): Number of days to retain logs. Default is5.maxEntries(number): Maximum number of log entries to store. Default is10000.logger(object): Custom logger to use internally. Default isconsole.
log(options)
Records a log entry.
Parameters:
level(string): The log level (e.g.,"info","warn","error").message(string): The log message.system(string, optional): The subsystem or module name.
getRecent(limit = 100)
Returns the most recent limit logs.
Parameters:
limit(number): The number of logs to retrieve. Default is100.
getAll()
Returns all logs, ordered by timestamp (most recent first).
getByLevel(level, limit = 100)
Returns logs filtered by log level (info, warn, error).
Parameters:
level(string): The log level to filter by (e.g.,info).limit(number): The number of logs to retrieve. Default is100.
info(message, system = null)
Logs an "info" level message.
warn(message, system = null)
Logs a "warn" level message.
error(message, system = null)
Logs an "error" level message.
createLogger(system = "default")
Creates a logger with a specific system or module name.
Notes
- Retention: Logs older than the
retentionDaysvalue are automatically deleted. - Max Entries: If the log entries exceed the
maxEntriesvalue, the oldest logs are deleted to keep the database within the set limit. - Custom Logger: You can pass a custom logger (like a file logger) if you prefer to log elsewhere, but the default is
console.
License
Licensed under the Apache License 2.0.
