UNPKG

deadslog

Version:

A dead simple logger module for Node.js

206 lines (166 loc) โ€ข 6.32 kB
# deadslog [![CI & Publish](https://github.com/c4lyp5o/deadslog/actions/workflows/ci-publish.yml/badge.svg)](https://github.com/c4lyp5o/deadslog/actions/workflows/ci-publish.yml) ![GitHub issues](https://img.shields.io/github/issues/c4lyp5o/deadslog) ![GitHub pull requests](https://img.shields.io/github/issues-pr/c4lyp5o/deadslog) [![codecov](https://codecov.io/gh/c4lyp5o/deadslog/graph/badge.svg?token=CBXCJDUJS9)](https://codecov.io/gh/c4lyp5o/deadslog) ![npm](https://img.shields.io/npm/v/deadslog) ![npm](https://img.shields.io/npm/dt/deadslog) ![GitHub](https://img.shields.io/github/license/c4lyp5o/deadslog) A dead simple logger module for Node.js. Provides console and file-based logging with support for log rotation, custom formatting, colored output, and robust error handling. ## โœจ Features - ๐Ÿ–ฅ Console and file logging - ๐Ÿ”„ Log rotation with delete/archive strategies - ๐Ÿงฉ Customizable log formatting - ๐ŸŒˆ Colored log levels in console - ๐Ÿงฑ Handles undefined/non-serializable messages - ๐Ÿง  TypeScript type definitions included - ๐Ÿ” ESM + CommonJS support ## ๐Ÿ“ฆ Installation ```sh npm install deadslog # or bun add deadslog ``` ## ๐Ÿš€ Usage ### ๐Ÿ”น Basic ```js import deadslog from "deadslog"; const logger = deadslog(); logger.info("Hello, world!"); ``` ### ๐ŸŽจ With Custom Formatter ```js const logger = deadslog({ formatter: (level, message) => { const timestamp = new Date().toLocaleString(); return `---\nTime: ${timestamp}\nLevel: ${level}\nMessage: ${message}\n---`; }, }); logger.info("Custom formatted log!"); ``` ### ๐Ÿ“ File Logging & Rotation ```js const logger = deadslog({ fileOutput: { enabled: true, logFilePath: "./logs/app.log", rotate: true, maxLogSize: 1024 * 1024, // 1MB maxLogFiles: 3, onMaxLogFilesReached: "archiveOld", // or "deleteOld" }, }); logger.info("This will be written to a file!"); ``` ### ๐Ÿ“ฆ CommonJS Usage ```js const deadslog = require("deadslog"); const logger = deadslog(); logger.info("Hello from CJS!"); ``` ## ๐Ÿ“˜ API ### deadslog(config) Returns a logger instance. #### โš™๏ธ Configuration Options | Option | Type | Description | | --------------------------------- | ---------- | -------------------------------------------------------------------------------- | | `consoleOutput.enabled` | `boolean` | Enable console logging (default: `true`) | | `consoleOutput.coloredCoding` | `boolean` | Enable colored output using `chalk` (default: `true`) | | `fileOutput.enabled` | `boolean` | Enable file logging (default: `false`) | | `fileOutput.logFilePath` | `string` | File path for log output (required if file logging is enabled) | | `fileOutput.rotate` | `boolean` | Enable automatic log file rotation | | `fileOutput.maxLogSize` | `number` | Maximum log file size in bytes before rotation | | `fileOutput.maxLogFiles` | `number` | Number of rotated files to keep | | `fileOutput.onMaxLogFilesReached` | `string` | Rotation strategy: `"deleteOld"` or `"archiveOld"` | | `formatter` | `function` | Optional custom formatter for log messages | | `minLevel` | `string` | Minimum log level: `trace`, `debug`, `info`, `success`, `warn`, `error`, `fatal` | | `filters.include` | `string` | Word filter to include from log | | `filters.exclude` | `string` | Word filter to exclude from log | #### ๐Ÿงฐ Logger Methods - `trace(msg)` - `debug(msg)` - `info(msg)` - `success(msg)` - `warn(msg)` - `error(msg)` - `fatal(msg)` - `flush()` - `destroy()` ## ๐Ÿง  TypeScript Type definitions are included and will be picked up automatically. ## ๐Ÿ“š Formatter Examples For Use ### ๐Ÿงพ 1. Simple Timestamp Formatter ```javascript const simpleFormatter = (level, message) => { const timestamp = new Date().toISOString(); return `[${timestamp}] [${level}] ${message}`; }; ``` ```yaml [2025-05-03T13:45:21.123Z] [INFO] Application started ``` ### ๐Ÿ“œ 2. Multiline Developer-Friendly Formatter ```javascript const multilineFormatter = (level, message) => { const timestamp = new Date().toLocaleString(); return `---\nTime: ${timestamp}\nLevel: ${level}\nMessage: ${message}\n---`; }; ``` ```yaml --- Time: 5/3/2025, 1:46:11 PM Level: DEBUG Message: Connected to database --- ``` ### ๐Ÿ“ 3. File-Friendly CSV Formatter ```javascript const csvFormatter = (level, message) => { const timestamp = new Date().toISOString(); const escaped = message.replace(/"/g, '""'); return `"${timestamp}","${level}","${escaped}"`; }; ``` ```yaml "2025-05-03T13:47:02.789Z","ERROR","Failed to load module: ""auth.js""" ``` ### ๐ŸŒˆ 4. Emoji-Coded Formatter ```javascript const emojiFormatter = (level, message) => { const emojis = { trace: '๐Ÿ”', debug: '๐Ÿ›', info: 'โ„น๏ธ', success: 'โœ…', warn: 'โš ๏ธ', error: 'โŒ', fatal: '๐Ÿ’€' }; const timestamp = new Date().toISOString(); return `${emojis[level] || ''} [${timestamp}] ${level}: ${message}`; }; ``` ```yaml โœ… [2025-05-03T13:48:15.456Z] SUCCESS: Task completed ``` ### ๐Ÿชต 5. JSONL (JSON Lines) Formatter for Parsing ```javascript const jsonlFormatter = (level, message) => { return JSON.stringify({ ts: Date.now(), level, message }); }; ``` ```yaml {"ts":1714740493123,"level":"INFO","message":"Something happened"} ``` ## Changelog ## [v1.2.2] - 2025-05-24 ### Fixed - Types file was not included in the package, causing issues for TypeScript users. --- See [CHANGELOG.md](./CHANGELOG.md) for previous versions and more details. ## License MIT