@dynamic-mock-server/logger
v0.1.0-beta
Published
Logger for Dynamic Mock Server — Pino-based structured logging with namespace support
Maintainers
Readme
@dynamic-mock-server/logger
Pino-based logger with pretty formatting and namespacing
Lightweight logger wrapper around Pino with pino-pretty for beautiful terminal output. Provides namespacing for organized logging across different modules.
Features
- 📝 Pino-Based: Built on high-performance Pino logger
- 🎨 Pretty Output: Colorized, readable logs with pino-pretty
- 🏷️ Namespacing: Create child loggers with namespace bindings
- 🎯 Log Levels: Full support for trace, debug, info, warn, error, fatal
- ⚙️ Configurable: Custom log levels and options
- 🚀 Fast: Zero-cost abstractions over Pino
Installation
pnpm add @dynamic-mock-server/loggerQuick Start
Basic Usage
import { Logger } from "@dynamic-mock-server/logger";
// Create logger with default settings
const logger = new Logger();
logger.info("Server started");
logger.debug("Loading configuration");
logger.warn("Deprecation warning");
logger.error("Failed to load file");With Log Level
import { Logger } from "@dynamic-mock-server/logger";
const logger = new Logger({ level: "debug" });
logger.trace("Very detailed"); // Won't show (below debug)
logger.debug("Debugging info"); // Shows
logger.info("Information"); // ShowsNamespaced Loggers
import { Logger } from "@dynamic-mock-server/logger";
const logger = new Logger();
// Create namespaced loggers
const routesLogger = logger.namespace("routes");
const pluginsLogger = logger.namespace("plugins");
routesLogger.info("Route registered");
// Output: [routes] Route registered
pluginsLogger.info("Plugin loaded");
// Output: [plugins] Plugin loadedChild Loggers with Bindings
import { Logger } from "@dynamic-mock-server/logger";
const logger = new Logger();
// Create child with custom bindings
const requestLogger = logger.child({ requestId: "abc123", userId: "user1" });
requestLogger.info("Processing request");
// Output includes: requestId: "abc123", userId: "user1"API Reference
Logger Class
Constructor
constructor(options?: LoggerOptions)LoggerOptions:
level?: string- Log level (trace, debug, info, warn, error, fatal). Default:"info"options?: pino.LoggerOptions- Additional Pino options
Log Methods
trace(...args: Parameters<LogFn>)
debug(...args: Parameters<LogFn>)
info(...args: Parameters<LogFn>)
warn(...args: Parameters<LogFn>)
error(...args: Parameters<LogFn>)
fatal(...args: Parameters<LogFn>)
Log at different levels.
logger.info("Simple message");
logger.info({ userId: 123 }, "User logged in");
logger.error({ err: error }, "Failed to process");Utility Methods
child(bindings?: Record<string, unknown>): Logger
Create a child logger with additional bindings.
const childLogger = logger.child({ module: "auth" });
childLogger.info("Authentication successful");namespace(name: string): Logger
Create a namespaced logger (shorthand for child({ namespace: name })).
const apiLogger = logger.namespace("api");
apiLogger.info("API request received");get raw(): pino.Logger
Access the underlying Pino logger instance.
const pinoLogger = logger.raw;Log Levels
Available log levels (in order of severity):
trace- Very detailed debuggingdebug- Detailed debugginginfo- General information (default)warn- Warningserror- Errorsfatal- Fatal errors
Set via environment variable:
LOG_LEVEL=debug npm startOr via constructor:
const logger = new Logger({ level: "debug" });Examples
Complete Server Logging
import { Logger } from "@dynamic-mock-server/logger";
const logger = new Logger({ level: process.env.LOG_LEVEL || "info" });
// Module-specific loggers
const serverLogger = logger.namespace("server");
const routesLogger = logger.namespace("routes");
const dbLogger = logger.namespace("database");
serverLogger.info("Starting server on port 3000");
routesLogger.info("Loaded 15 routes");
dbLogger.warn("Connection pool nearing capacity");Request Logging
import { Logger } from "@dynamic-mock-server/logger";
const logger = new Logger();
function handleRequest(req: Request) {
const requestLogger = logger.child({
requestId: req.id,
method: req.method,
url: req.url,
});
requestLogger.info("Request started");
try {
// Process request
requestLogger.info("Request completed");
} catch (error) {
requestLogger.error({ err: error }, "Request failed");
}
}Dependencies
pino- High-performance loggerpino-pretty- Pretty formatting for development
Related Packages
- @dynamic-mock-server/core - Uses logger for all core operations
- @dynamic-mock-server/config - Respects
logLevelconfig
License
Apache-2.0 © Miguel Martínez
