@ecopages/logger
A lightweight, flexible logging library with color support, timestamps, and timer functionality. This logger supports multiple log levels and allows for easy extension and integration into any project.
Features
- Multiple Log Levels: Supports
INFO, ERROR, WARN, and DEBUG log levels
- Colored Output: Configurable colors for different log levels in both browser and Node.js environments
- Timestamps: Optional timestamps with configurable formats
- Timer Support: Built-in timer functionality for performance measurements
- Debug Utilities: Helper methods for conditional debug operations
- Prefixed Messages: Customizable prefix for all log messages
- Environment Detection: Automatically adapts output format for browser or Node.js environments
- Extensible: Easy to extend with custom functionality
Installation
npm install @ecopages/logger
yarn add @ecopages/logger
bun add @ecopages/logger
Basic Usage
import { Logger } from "@ecopages/logger";
const logger = new Logger("[my-app]");
logger.info("This is an informational message");
logger.warn("This is a warning message");
logger.error("This is an error message");
logger.debug("This is a debug message");
Advanced Configuration
The logger supports various configuration options:
const logger = new Logger("[my-app]", {
debug: true,
color: true,
timestamp: true,
timestampFormat: "full",
verboseTimer: true,
colors: {
INFO: "color: purple",
ERROR: "color: darkred",
},
});
Configuration Options
debug | boolean | false | Enable debug level messages |
color | boolean | true | Enable colored output |
timestamp | boolean | false | Add timestamps to messages |
timestampFormat | "full" | "time" | "short" | "time" | Timestamp format |
verboseTimer | boolean | false | Show timer start messages |
colors | Partial | - | Custom colors for log levels |
Timestamp Formats
full: YYYY-MM-DD HH:mm:ss
time: HH:mm:ss
short: MM-DD HH:mm:ss
Timer Functionality
logger.time("operation");
logger.timeEnd("operation");
Debug Utilities
The logger provides several debug-specific methods:
if (logger.isDebugEnabled()) {
}
logger.debugTime("debug-operation");
logger.debugTimeEnd("debug-operation");
Custom Colors
Colors can be customized differently for browser and Node.js environments:
const logger = new Logger("[my-app]", {
colors: {
INFO: "color: purple",
ERROR: "color: darkred",
WARN: "color: orange",
DEBUG: "color: cyan",
TIMER: "color: magenta",
},
});
const logger = new Logger("[my-app]", {
colors: {
INFO: "\x1b[35m",
ERROR: "\x1b[31m",
WARN: "\x1b[33m",
DEBUG: "\x1b[36m",
TIMER: "\x1b[35m",
},
});
Examples
With Timestamps and Colors
const logger = new Logger("[my-app]", {
timestamp: true,
timestampFormat: "full",
});
logger.info("Application started");
With Debug Messages
const logger = new Logger("[my-app]", { debug: true });
logger.debug("Configuration loaded:", { port: 3000 });
With Timer
const logger = new Logger("[my-app]", { verboseTimer: true });
logger.time("db-query");
logger.timeEnd("db-query");
With Conditional Debug Timers
const logger = new Logger("[my-app]", { debug: true });
logger.debugTime("expensive-calculation");
const result = performExpensiveCalculation();
logger.debugTimeEnd("expensive-calculation");