lightweight logging app that handles the life cycle of a log
npm install js-log-managerjavascript
npm install js-log-manager
`
๐ ๏ธ Quick Start
`javascript
import jsLog from 'js-log-manager';
// Auto-tune buffers based on your hardware
jsLog.bufferAutoTune();
// Initialize a log category
const logger = new jsLog.Logs({
filename: 'api-server',
level: 'info',
txtColor: 'cyan'
});
// Start logging
logger.file("User 'JohnSpriggs' authenticated successfully.");
logger.terminal("System heartbeat stable.");
`
#
๐ Freedom of Logging (The Manager Approach)
JS-Log-Manager removes the strict constraints of traditional loggers. Most libraries force you into one file; we give you the freedom to create as many specialized log instances as your application requires.
`JavaScript
import jsLog from 'js-log-manager';
// Define logs by their purpose, not just their level
const user_api_errors = new jsLog.Logs({ filename: 'user-api', txtColor: 'red' });
const payment_logs = new jsLog.Logs({ filename: 'payments', txtColor: 'green' });
const auth_audit = new jsLog.Logs({ filename: 'security', toTerminal: false }); // Quietly log to file only
`
#
$3
The engine provides four distinct ways to output your data. Each instance can be configured to toggle these on or off via the toFile, toTerminal, and terminalRaw settings.
| Function | Output | Synchronized? | Best Use Case |
| :--- | :--- | :--- | :--- |
| logg(data) | File + Terminal | โ
Yes | Your go-to function for standard app logging. |
| file(data) | Disk Only | N/A | High-volume background data (e.g., raw API payloads). |
| terminal(data) | Console Only | N/A | Real-time debugging and system heartbeats. |
| terminal_raw(data) | Raw Console | N/A | Minimalist output without timestamps or branding. |
$3
The logg() function solves the "Timestamp Drift" problem. In high-traffic environments, calling .file() and .terminal() separately can result in slight millisecond differences. logg() captures the time once and distributes it to all enabled outputs, ensuring your records match perfectly.
`JavaScript
const logger = new jsLog.Logs({
filename: 'app-engine',
toFile: true, // Default
toTerminal: true, // Default
terminalRaw: false // Default
});
// Sends to both file and terminal with the exact same timestamp
logger.logg("Process Initialized");
`
#
๐ ๏ธ Performance Tuning
$3
Stop guessing your buffer sizes. Call bufferAutoTune() to let the engine inspect your CPU and set the optimal write-buffer (64kb, 128kb, or 256kb) for your specific hardware.
`JavaScript
// Optimized for your specific CPU (i7, M3, Threadripper, etc.)
jsLog.bufferAutoTune();
`
$3
Out-of-the-box support for high-traffic file splitting. If a log file hits your maxSize limit, the engine automatically indexes it (e.g., api_2024-01-01_001.log) so no data is ever overwritten or lost.
๐งน The Janitor: 3 Ways to Handle Cleanup
The jsLog.start() function manages your log retention using only one dependency: node-cron.
$3
Just starting the function uses the 30 day default
`JavaScript
jsLog.start()
`
Keep all logs for a set amount of days.
`JavaScript
jsLog.start(7); // Deletes everything older than 7 days
`
#
$3
Set specific rules for specific files with a fallback for the rest.
`JavaScript
jsLog.start({
default: 7,
payments: 90, // Keep financial logs for 3 months
debug: 1 // Purge debug logs daily
});
`
#
$3
If you don't provide a default key, the janitor will only touch the files you explicitly name, leaving all other logs safe on the disk forever.
`JavaScript
jsLog.start({
temp_logs: 1,
test_results: 2
});
`
#
$3
Our configuration is strict to ensure production stability.
`javascript
jsLog.configuration({
setDir: 'logs', // Custom directory
setBufferSize: 512, // Manual buffer override (kb)
setDeletionBatchSize: 20, // Number of files deleted per batch
cleanupTime: '00 05 *' // Run Janitor at 5:00 AM (Cron format)
});
``