Logger
The Logger module provides structured, leveled logging with support for JSON and human-readable output formats.
Usage
import { FluxKit } from "@fluxkitdev/core";
const app = new FluxKit({
database: { uri: process.env.MONGODB_URI },
logger: {
level: "debug",
format: "pretty", // "json" for production
},
});
app.logger.debug("Debugging info", { userId: "123" });
app.logger.info("User logged in", { userId: "123", ip: "1.2.3.4" });
app.logger.warn("Rate limit approaching", { current: 95, max: 100 });
app.logger.error("Failed to send email", { error: err.message });
API Reference
logger.debug(message: string, meta?: object): void
Log a debug-level message. Only visible when LOG_LEVEL is set to debug.
logger.info(message: string, meta?: object): void
Log an informational message.
logger.warn(message: string, meta?: object): void
Log a warning message.
logger.error(message: string, meta?: object): void
Log an error message.
logger.child(meta: object): Logger
Create a child logger that inherits the parent's configuration and merges additional metadata into every log entry.
const reqLogger = app.logger.child({
requestId: "abc-123",
path: "/api/users",
});
reqLogger.info("Processing request");
// Output includes requestId and path automatically
Log Levels
| Level | Priority | Description |
|---|---|---|
debug | 0 | Detailed debugging information |
info | 1 | General operational messages |
warn | 2 | Warning conditions |
error | 3 | Error conditions |
Setting LOG_LEVEL=warn will suppress debug and info messages.
Output Formats
Pretty (Development)
[2025-01-15 10:30:45] INFO User logged in userId=123 ip=1.2.3.4
JSON (Production)
{
"timestamp": "2025-01-15T10:30:45.123Z",
"level": "info",
"message": "User logged in",
"userId": "123",
"ip": "1.2.3.4"
}
Configuration
| Variable | Default | Description |
|---|---|---|
LOG_LEVEL | info | Minimum log level |
LOG_FORMAT | json | Output format (json or pretty) |