Skip to content

Custom Transports

Create custom transports to send logs anywhere you need.

Overview

A transport is simply a function that receives log data and does something with it. Crowlog's transport interface is intentionally simple, making it easy to create custom transports for any destination.

typescript
type LoggerTransport = (args: LoggerTransportLogArgs) => void;

Transport Interface

LoggerTransportLogArgs

Every transport receives a log object with this structure:

typescript
type LoggerTransportLogArgs = {
  level: 'debug' | 'info' | 'warn' | 'error';
  message: string;
  timestampMs: number;
  namespace: string;
  data: Record<string, unknown>;
};

Fields:

FieldTypeDescription
levelLogLevelLog severity level
messagestringLog message
timestampMsnumberUnix timestamp in milliseconds
namespacestringLogger namespace
dataRecord<string, unknown>Structured log data

Basic Transport Template

The simplest custom transport:

typescript
import type { LoggerTransport, LoggerTransportLogArgs } from '@crowlog/logger';

function createMyTransport(): LoggerTransport {
  return (args: LoggerTransportLogArgs) => {
    // Do something with the log
    console.log(args);
  };
}

// Usage
const logger = createLogger({
  namespace: 'my-app',
  transports: [createMyTransport()]
});

Released under the MIT License.