\n \n \n \n \n \n \n \n \n \n
โ† Back to Home

Transports

Write logs anywhere โ€” files, streams, HTTP endpoints. Zero external dependencies.

File Transport

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).

Stream Transport

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)

HTTP Transport

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')

Multiple Transports

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')

Production Features

PII Redaction

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([])

Async Logging

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)

Comparison with Pino/Winston

Featurebetter-loggerPinoWinston
File transportโœ… (zero-dep)โœ… (pino/file)โœ…
Stream transportโœ…โœ…โœ…
HTTP transportโœ…โœ…โœ…
PII redactionโœ…โœ… (redact)โœ… (format)
Async modeโœ…โœ…โœ…
Dependencies08+20+
Bundle size3.8KB30KB100KB