A simple and efficient logging system that saves records in CSV format with timestamps and severity levels.
Logger is a Node.js application that provides a lightweight and functional logging system. It records messages with different severity levels (INFO, WARN, ERROR, DEBUG) in CSV files organized by date.
- ✅ Logging with multiple levels (INFO, WARN, ERROR, DEBUG)
- ✅ CSV files organized by date
- ✅ Timestamps in ISO 8601 format
- ✅ Support for additional metadata
- ✅ Automatic directory handling
- ✅ Robust CSV format with special character escaping
- ✅ NEW v2.0.0: Colored console output with Chalk library
- ✅ NEW v2.0.0: Real-time console logging while writing to CSV files
- ✅ NEW v2.0.0: Color-coded log levels (blue for INFO, gray for DEBUG, yellow for WARNING, red for ERROR)
- ✅ NEW v2.1.0: Caller information tracking (file name and line number)
- Node.js 12 or higher
- npm (included with Node.js)
- Clone or download this repository:
git clone <repository-url>
cd logger- Install dependencies (if any):
npm installnpm install git+https://github.com/rusker86/logger.gitThe project uses the following dependencies:
- chalk: ^5.6.2 - For colored terminal output
The project includes a comprehensive test suite using Node.js built-in testing framework.
Run all tests:
npm testRun tests with verbose output:
npm run test:verboseThe test suite includes 79 tests covering:
- ✅ Date utilities (getTodayDate, getTimeStamp)
- ✅ CSV formatting (toCSVRow)
- ✅ Caller information extraction (getCallerInfo)
- ✅ Console logging with colors
- ✅ File stream writing (NEW)
- ✅ Logger creation and initialization
- ✅ Optional caller information (NEW)
- ✅ All logging methods (info, warn, error, debug)
- ✅ File operations and organization
- ✅ Metadata handling
- ✅ Edge cases and error handling
See tests/README.md for detailed test documentation.
logger/
├── createLogger.js # Logger factory function
├── index.js # Main entry point
├── package.json # Project dependencies and configuration
└── utils/
├── csv.js # CSV format utilities
├── date.js # Date and timestamp handling utilities
├── logToConsole.js # Console output with color formatting
├── callerInfo.js # Caller information extraction
└── writer.js # File stream writer utilities
npm run startimport { createLogger } from "./createLogger.js"
// Create logger with default options (callerInfo enabled)
const logger = createLogger()
// Create logger with custom directory
const logger = createLogger({ logDir: "./mylogs" })
// Create logger without caller information in messages
const logger = createLogger({ callerInfo: false })
// Combine options
const logger = createLogger({
logDir: "./logs",
callerInfo: true // Default: includes file name and line number
})// Log information
logger.info("Server started")
// Log warnings
logger.warn("Unusual condition detected", { userId: 123 })
// Log errors
logger.error("An error occurred", { code: 500 })
// Log debug information
logger.debug("Variable x = 42", { variable: 42 })All logger methods accept a message and optional metadata:
logger.info(message: string, meta?: object)
logger.warn(message: string, meta?: object)
logger.error(message: string, meta?: object)
logger.debug(message: string, meta?: object)logDir(string): Directory where log files will be saved. Defaults to"logs". Can be absolute or relative path.callerInfo(boolean): Include caller file name and line number in log messages. Defaults totrue.
Logs are saved in CSV files in the logs/ directory with the filename YYYY-MM-DD.csv.
Each line contains:
- Timestamp: Exact time in ISO 8601 format
- Level: [INFO], [WARN], [ERROR], or [DEBUG]
- Message: Descriptive text of the event
- Metadata: Additional data in JSON format (optional)
Example:
2026-01-22T15:30:45.123Z,[INFO],Server started,
2026-01-22T15:30:46.456Z,[WARN],Unusual condition detected,{"userId":123}
2026-01-22T15:30:47.789Z,[ERROR],An error occurred,{"code":500}
Console Output
Starting from v2.0.0, logs are displayed in the console with color coding for better visibility:
- 🔵 [INFO] - Blue
- 🟡 [WARNING] - Yellow
- 🔴 [ERROR] - Red
- ⚫ [DEBUG] - Gray
Example console output:
[INFO], Server started,
[WARNING], Unusual condition detected, {"userId":123}
[ERROR], An error occurred, {"code":500}
Logs are simultaneously written to CSV files and displayed in the console with real-time color formatting This project has no external dependencies. It uses only native Node.js modules.
Utility for displaying logs in the terminal with color formatting using Chalk:
- Maps log levels to specific colors
- Automatically formats messages and metadata for console display
- Enhances log visibility with color-coded severity levels
Central module that exports the logger object with four logging levels. It manages the creation of daily log files and record writing.
Utility for converting arrays of values to valid CSV format, with automatic handling of:
- Null or undefined values
- Objects (converted to JSON)
- Special characters (commas, quotes, line breaks)
Functions to get:
getTodayDate(): Current date in YYYY-MM-DD formatgetTimeStamp(): Timestamp actual en formato ISO 8601
Extracts caller information from the call stack:
getCallerInfo(): Returns an object withfileandlineproperties- Useful for debugging to identify the exact location where a log was called
- Returns
{file: string, line: number}
Manages file stream writing for log files:
createWriter(filePath): Creates a writer instance for appending to fileswrite(line): Writes content to the file streamclose(): Properly closes the file stream- Uses append mode to preserve existing log entries
- Returns
{write, close}object
MIT