jslog vs Go slog
How jslog compares to Go's log/slog package.
Core Compatibility
jslog implements the complete Go slog API:
| Feature | Go slog | jslog | Status |
|---|---|---|---|
slog.Debug() | Yes | Yes | Implemented |
slog.Info() | Yes | Yes | Implemented |
slog.Warn() | Yes | Yes | Implemented |
slog.Error() | Yes | Yes | Implemented |
slog.New() | Yes | Yes | Implemented |
slog.Default() | Yes | Yes | Implemented |
slog.SetDefault() | Yes | Yes | Implemented |
slog.With() | Yes | Yes | Implemented |
slog.WithGroup() | Yes | Yes | Implemented |
slog.TextHandler | Yes | Yes | Implemented |
slog.JSONHandler | Yes | Yes | Implemented |
slog.Level | Yes | Yes | Implemented |
slog.LevelVar | Yes | Yes | Implemented |
Handler interface | Yes | Yes | Implemented |
ReplaceAttr | Yes | Yes | Implemented |
Features jslog Has That Go slog Doesn't
1. ColorHandler
Beautiful ANSI color-coded console output:
const logger = New(new ColorHandler());
logger.error('Critical error detected'); // Red!
Go slog: Requires third-party packages.
2. FileHandler with Rotation
Built-in file logging with automatic rotation:
const logger = New(new FileHandler({
filepath: './logs/app.log',
maxSize: 10 * 1024 * 1024, // 10MB
maxFiles: 5
}));
Go slog: Requires external libraries like lumberjack.
3. BufferedHandler
Batch log writing for better performance:
const logger = New(new BufferedHandler({
handler: new JSONHandler(),
bufferSize: 100,
flushInterval: 1000
}));
Go slog: Manual buffering implementation required.
4. SamplingHandler
Probabilistic sampling for high-traffic apps:
const logger = New(new SamplingHandler({
handler: new TextHandler(),
rate: 0.1 // Log 10% of messages
}));
Go slog: Custom handler wrapper needed.
5. FilterHandler
Advanced filtering beyond level-based filtering:
const logger = New(new FilterHandler({
handler: new ColorHandler(),
filter: (record) => record.level >= Level.ERROR || record.message.includes('critical')
}));
Go slog: Custom handler implementation required.
6. AsyncHandler
Non-blocking log operations:
const logger = New(new AsyncHandler({
handler: new FileHandler({ filepath: './logs/app.log' }),
errorHandler: (err) => console.error('Log error:', err)
}));
Go slog: While Go has goroutines, explicit async patterns require manual setup.
7. Middleware Pattern
Composable handler middleware:
const logger = New(new MiddlewareHandler({
handler: new JSONHandler(),
middleware: [
hostnameMiddleware(),
pidMiddleware(),
dedupeMiddleware(1000),
rateLimitMiddleware(100)
]
}));
Go slog: Handler wrapping pattern must be manually implemented.
8. Built-in Metrics
Automatic logging statistics:
const metrics = new MetricsMiddleware();
const logger = New(new MiddlewareHandler({
handler: new JSONHandler(),
middleware: [metrics.middleware()]
}));
// Later...
console.log(metrics.getStats());
Go slog: No built-in metrics.
9. Deduplication
Automatic spam prevention:
const logger = New(new MiddlewareHandler({
handler: new ColorHandler(),
middleware: [dedupeMiddleware(1000)]
}));
logger.info('Repeated message'); // Shows
logger.info('Repeated message'); // Deduplicated!
Go slog: Manual implementation needed.
10. Rate Limiting
Automatic rate limiting:
const logger = New(new MiddlewareHandler({
handler: new TextHandler(),
middleware: [rateLimitMiddleware(100)] // Max 100/second
}));
Go slog: Manual rate limiting required.
11. Fluent Attribute Builder
Chain attributes easily:
const attrs = attrs()
.str('user', 'alice')
.num('age', 30)
.bool('active', true)
.if(condition, 'conditional', 'value')
.build();
logger.info('User created', ...attrs);
Go slog: No fluent API.
12. Performance Timers
Built-in timing utilities:
const timer = startTimer('operation');
// ... do work ...
logger.info('Operation complete', timer.elapsed());
Go slog: Manual timer implementation needed.
13. Correlation IDs
Global request/trace tracking:
setCorrelationId('trace-123');
logger.info('Request 1', CorrelationId()); // Includes trace-123
logger.info('Request 2', CorrelationId()); // Includes trace-123
Go slog: Manual context management required.
14. HTTP Helpers
Easy request/response logging:
logger.info('HTTP request', ...HttpReq({
method: 'POST',
url: '/api/users',
ip: '192.168.1.1'
}));
logger.info('HTTP response', ...HttpRes({
status: 201,
duration: 45,
size: 1024
}));
Go slog: Manual attribute construction.
15. System Info
Environment and memory helpers:
logger.info('App started', ...EnvInfo());
logger.info('Memory status', ...MemoryUsage());
Go slog: Manual info gathering.
16. Data Masking
Built-in PII redaction:
logger.info('User data',
String('email', maskEmail('alice@example.com')), // a***@example.com
String('card', maskCreditCard('4532-1234-5678-9010')) // ****-****-****-9010
);
Go slog: Manual masking functions needed.
17. Stack Traces
Automatic stack trace capture:
logger.error('Error occurred', StackTrace());
Go slog: Manual stack trace capture.
18. Caller Info
Automatic source location:
logger.info('Log with caller', Caller());
// Includes file and line number
Go slog: Has addSource but limited.
19. Error Boundaries
Catch handler errors safely:
const logger = New(new MiddlewareHandler({
handler: new JSONHandler(),
middleware: [errorBoundaryMiddleware()]
}));
Go slog: Manual error handling needed.
20. Circular Reference Handling
Safe JSON serialization:
const circular = { a: 1 };
circular.self = circular;
logger.info('Data', Any('obj', circular)); // Safely handled!
Go slog: Would panic on circular references.
Summary
jslog gives you everything Go's slog has, plus 20+ additional features specifically designed for Node.js production environments!
When to Use jslog
You should use jslog if:
- You want Go's slog API in Node.js
- You need production-ready logging features
- You want file rotation, buffering, sampling
- You need async handlers and middleware
- You want built-in utilities and helpers
You came from Go and want familiar logging:
- API-compatible with Go's slog
- Same mental model and patterns
- Easy transition for Go developers
Next Steps
- Getting Started - Install and start using jslog
- Core Concepts - Learn the fundamentals
- Advanced Features - Explore advanced handlers