High-performance structured logging library inspired by Go's slog.
- Zero-allocation byte buffer operations for maximum performance
- Structured logging with key-value attributes
- Automatic toString() handling for objects with custom toString methods
- Configurable log levels
- Optimized timestamp caching to minimize date formatting overhead
- Built for Node.js and Bun with ESNext targeting
npm install @superbuilders/slog
# or
bun add @superbuilders/slogimport*asloggerfrom'@superbuilders/slog'// Basic logginglogger.info("user logged in",{userId: "123",ip: "192.168.1.1"})logger.error("database connection failed",{error: err})// Set log levellogger.setDefaultLogLevel(logger.DEBUG)import{DEBUG,INFO,WARN,ERROR}from'@superbuilders/slog'// Available levels (lower numbers = more verbose)DEBUG// -4INFO// 0 (default)WARN// 4ERROR// 8// Level-specific functionslogger.debug("debug message",{key: "value"})logger.info("info message",{key: "value"})logger.warn("warning message",{key: "value"})logger.error("error message",{key: "value"})// Set minimum log levellogger.setDefaultLogLevel(WARN)// Only WARN and ERROR will be output- Uses pre-allocated byte buffers to avoid garbage collection pressure
- Direct ASCII byte writing for common string operations
- Optimized object serialization with custom toString() detection
- Caches formatted timestamps to avoid repeated date formatting
- Sub-second precision with efficient updates
- Fast path for arrays and common object types
- Automatic detection and use of custom toString() methods (Error objects, Date, etc.)
- Handles special number cases (NaN, Infinity) efficiently
2024/05/27 10:15:30 INFO user logged in userId=123 ip=192.168.1.1
2024/05/27 10:15:31 ERROR database connection failed error=Connection timeout
try{awaitriskyOperation()}catch(error){// Error objects automatically use toString()logger.error("operation failed",{ error,userId: "123"})}logger.info("user action",{userId: "123",action: "purchase",items: ["item1","item2"],metadata: {source: "mobile",version: "2.1.0"}})conststart=Date.now()awaitlongRunningOperation()constduration=Date.now()-startlogger.info("operation completed",{operation: "data_export",
duration,recordCount: 1500})Full TypeScript support with comprehensive type definitions:
import*asloggerfrom'@superbuilders/slog'// Type-safe attribute objectsinterfaceUserAttributes{userId: stringemail: stringrole: 'admin'|'user'}constattrs: UserAttributes={userId: "123",email: "user@example.com",role: "admin"}logger.info("user created",attrs)0BSD
Contributions welcome! Please ensure all changes maintain the zero-allocation performance characteristics.