Lightweight async logger for Gilbert static site generator with zero dependencies and runtime-agnostic design.
- Zero Dependencies: No external packages required
- Async by Design: Uses
setTimeout(..., 0)for non-blocking logging when enabled - Zero Overhead: No-op functions when logging is disabled
- Runtime Agnostic: Works across Node.js, Deno, Bun, Cloudflare Workers
- WinterCG Compatible: Uses only Web API standards
- Performance First: Optimized for Gilbert's 200+ pages/second target
import { createLogger } from "@tforster/gilbert-logger";
// Production mode - zero overhead
const logger = createLogger(false);
logger.log("This does nothing"); // No-op function
// Development mode - async logging
const debugLogger = createLogger(true);
debugLogger.log("This logs asynchronously"); // Non-blocking
debugLogger.warn("Async warning"); // Non-blocking
debugLogger.debug("Debug information"); // Non-blocking
debugLogger.error("Critical error"); // Synchronous (always enabled)
// Check if logging is enabled
if (logger.isEnabled()) {
// Do expensive logging operations
}Creates a logger instance with configurable debug mode.
Parameters:
debug(boolean, optional): Enable/disable logging. Defaults tofalse.
Returns:
- Logger instance with
log,warn,error,debug, andisEnabledmethods.
log(...args): General information logging (async when enabled, no-op when disabled)warn(...args): Warning logging (async when enabled, no-op when disabled)error(...args): Error logging (always synchronous for reliability)debug(...args): Debug information logging (async when enabled, no-op when disabled)isEnabled(): Returns boolean indicating if logging is enabled
When disabled, all methods (except error) are no-op functions with zero overhead.
When enabled, log, warn, and debug use setTimeout(..., 0) for async execution.
Benchmark Results:
- Disabled logger: ~0.1ms for 1000 calls (no-op overhead)
- Enabled logger: ~5-10ms for 1000 calls (async scheduling overhead)
- Both modes: Sub-100ms for production workloads
Gilbert-logger follows Gilbert's core principles:
- Zero Dependencies: Lean implementation using only JavaScript standards
- Runtime Agnostic: Compatible with all WinterCG-compliant runtimes
- Performance First: Designed for high-throughput static site generation
- Simple API: Minimal surface area, easy to understand and maintain
Synchronous console.log can significantly impact performance in high-throughput scenarios. In Gilbert's template processing, even 6 console.log calls per file can create measurable slowdown when processing hundreds of files.
Gilbert-logger solves this by:
- Making logging async when enabled (non-blocking)
- Using no-op functions when disabled (zero overhead)
- Keeping errors synchronous (reliability over performance)
MIT