\n \n \n \n \n \n \n \n \n \n
Write logs anywhere โ files, streams, HTTP endpoints. Zero external dependencies.
Write logs to a local file. Uses Node.js fs directly โ no external packages.
// Append mode (default)
better.log.toFile('app.log')
// Overwrite mode
better.log.toFile('app.log', { append: false })
// Multiple files
better.log.toFile('app.log')
better.log.toFile('error.log')
โ ๏ธ Browser Note: File transport is silently ignored in browsers (fs unavailable).
Write logs to any writable stream. Useful for custom destinations.
// stdout/stderr
better.log.toStream(process.stdout)
// File stream
const fs = require('fs')
const stream = fs.createWriteStream('logs.json')
better.log.toStream(stream)
// Custom writable
const { Writable } = require('stream')
const myStream = new Writable({
write(chunk, encoding, callback) {
const log = JSON.parse(chunk)
// Custom processing
callback()
}
})
better.log.toStream(myStream)
POST logs to a remote HTTP endpoint. Useful for log aggregation services.
// Basic usage
better.log.toHttp('https://api.example.com/logs')
// With custom headers
better.log.toHttp('https://api.example.com/logs', {
method: 'POST',
headers: {
'Authorization': 'Bearer your-token',
'Content-Type': 'application/json'
}
})
// Multiple endpoints
better.log.toHttp('https://logs1.example.com')
better.log.toHttp('https://logs2.example.com')
Combine multiple transports. Each log goes to all destinations.
// All logs go to file AND HTTP
better.log.toFile('app.log')
better.log.toHttp('https://api.example.com/logs')
// All logs go to stdout AND file
better.log.toStream(process.stdout)
better.log.toFile('app.log')
Automatically strip sensitive fields from all log output.
// Redact fields
better.log.redact(['password', 'ssn', 'creditCard', 'authorization'])
// Logs with redaction
better.log('User login', { username: 'alice', password: 'secret123' })
// Output: { username: "alice", password: "[REDACTED]" }
// Clear redaction
better.log.redact([])
Non-blocking buffered mode for high-throughput scenarios.
// Enable async mode
better.log.async(true)
// All logs are buffered
better.log('msg1')
better.log('msg2')
better.log('msg3')
// Force flush
better.log.flush()
// Disable (auto-flushes remaining)
better.log.async(false)
| Feature | better-logger | Pino | Winston |
|---|---|---|---|
| File transport | โ (zero-dep) | โ (pino/file) | โ |
| Stream transport | โ | โ | โ |
| HTTP transport | โ | โ | โ |
| PII redaction | โ | โ (redact) | โ (format) |
| Async mode | โ | โ | โ |
| Dependencies | 0 | 8+ | 20+ |
| Bundle size | 3.8KB | 30KB | 100KB |