Skip to content

Types

Type definitions exported by konsole-logger.

LogEntry

Represents a single log entry stored in the circular buffer.

typescript
type LogEntry = {
  msg: string;                       // primary message string
  messages: unknown[];               // original arguments (kept for compatibility)
  fields: Record<string, unknown>;   // structured key-value pairs (includes bindings)
  timestamp: Date;
  hrTime?: number;                   // high-res nanosecond offset (when highResolution: true)
  namespace: string;
  level: LogLevelName;               // 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'
  levelValue: number;                // 10 | 20 | 30 | 40 | 50 | 60
  logtype?: string;                  // @deprecated — use level
};

Properties

PropertyTypeDescription
msgstringThe primary log message
messagesunknown[]Original arguments passed to the log method
fieldsRecord<string, unknown>Structured key-value pairs (child bindings merged with call-site fields)
timestampDateWhen the log was created
hrTimenumber | undefinedHigh-resolution monotonic timestamp in nanoseconds (present when highResolution: true)
namespacestringThe logger namespace
levelLogLevelNameSeverity: 'trace' 'debug' 'info' 'warn' 'error' 'fatal'
levelValuenumberNumeric severity: 10 / 20 / 30 / 40 / 50 / 60

Example

typescript
import { Konsole } from 'konsole-logger';

const logger = new Konsole({ namespace: 'App', format: 'silent' });
logger.error('Something failed', { code: 500, path: '/users' });

const [entry] = logger.getLogs();
// {
//   msg: 'Something failed',
//   messages: ['Something failed', { code: 500, path: '/users' }],
//   fields: { code: 500, path: '/users' },
//   timestamp: Date,
//   namespace: 'App',
//   level: 'error',
//   levelValue: 50,
// }

Transport (interface)

All transport implementations satisfy this interface.

typescript
interface Transport {
  readonly name: string;
  write(entry: LogEntry): void;
  flush?(): Promise<void>;
  destroy(): Promise<void>;
}

Implementing a custom transport

typescript
import type { Transport, LogEntry } from 'konsole-logger';

class MyTransport implements Transport {
  readonly name = 'my-transport';

  write(entry: LogEntry): void {
    // deliver the entry somewhere
    externalService.send({
      level: entry.level,
      msg: entry.msg,
      ...entry.fields,
    });
  }

  async destroy(): Promise<void> {
    // clean up resources
  }
}

const logger = new Konsole({
  namespace: 'App',
  transports: [new MyTransport()],
});

KonsoleOptions

Configuration for the Konsole constructor.

typescript
interface KonsoleOptions {
  namespace?: string;
  level?: LogLevelName;
  format?: KonsoleFormat;
  timestamp?: TimestampFormat | TimestampOptions;
  redact?: string[];
  transports?: (Transport | TransportConfig)[];
  maxLogs?: number;
  defaultBatchSize?: number;
  retentionPeriod?: number;
  cleanupInterval?: number;
  useWorker?: boolean;
  criteria?: Criteria; // @deprecated
}

Properties

PropertyTypeDefaultDescription
namespacestring'Global'Logger namespace
levelLogLevelName'trace'Minimum level — entries below are discarded
formatKonsoleFormat'auto'Output format (see below)
timestampTimestampFormat | TimestampOptions'datetime'Timestamp format (see below)
redactstring[][]Dot-notation field paths to mask with '[REDACTED]' — see Redaction
transports(Transport | TransportConfig)[][]External log destinations
maxLogsnumber10000Circular buffer capacity
defaultBatchSizenumber100Entries per viewLogs() call
retentionPeriodnumber17280000048 hours in ms
cleanupIntervalnumber36000001 hour in ms
useWorkerbooleanfalseWorker mode (Web Worker in browser, worker_threads in Node.js)
criteriaCriteriatrueOutput filter (deprecated — use level and format)

KonsoleFormat

typescript
type KonsoleFormat = 'auto' | 'pretty' | 'json' | 'text' | 'browser' | 'silent';
ValueDescription
'auto'Browser → browser, Node.js TTY → pretty, Node.js pipe → json
'pretty'ANSI-colored human-readable output; respects NO_COLOR / FORCE_COLOR
'json'Newline-delimited JSON — aggregator-friendly (Datadog, Loki, CloudWatch)
'text'Plain text, no ANSI — for CI logs or plain log files
'browser'%c CSS badge styling in browser DevTools
'silent'No output; entries still stored in buffer and forwarded to transports

KonsoleChildOptions

Options accepted by logger.child().

typescript
interface KonsoleChildOptions {
  namespace?: string;
  level?: LogLevelName;
  timestamp?: TimestampFormat | TimestampOptions;
  redact?: string[];
}
PropertyTypeDefaultDescription
namespacestringParent'sOverride namespace for this child
levelLogLevelNameParent'sOverride minimum level (can only be more restrictive)
timestampTimestampFormat | TimestampOptionsParent'sOverride timestamp format
redactstring[][]Additional paths to redact (merged with parent's paths — child can never redact fewer)

RotationOptions

Configuration for file rotation on FileTransport.

typescript
interface RotationOptions {
  maxSize?: number;
  interval?: 'daily' | 'hourly' | number;
  maxFiles?: number;
  compress?: boolean;
}
PropertyTypeDefaultDescription
maxSizenumberRotate when file exceeds this size in bytes
interval'daily' | 'hourly' | numberRotate on a time schedule ('daily', 'hourly', or ms)
maxFilesnumber5Maximum rotated files to retain; oldest are deleted
compressbooleanfalseGzip-compress rotated files (.gz suffix)

Rotated files use numeric suffixes: app.log.1 (newest) → app.log.2 → etc. The current log file always stays at the configured path. When both maxSize and interval are set, rotation triggers on whichever condition is met first.


TransportConfig

Configuration for an HTTP transport (auto-wrapped in HttpTransport).

typescript
interface TransportConfig {
  name: string;
  url: string;
  method?: 'POST' | 'PUT';
  headers?: Record<string, string>;
  batchSize?: number;
  flushInterval?: number;
  retryAttempts?: number;
  filter?: (entry: LogEntry) => boolean;
  transform?: (entry: LogEntry) => unknown;
  fetchImpl?: typeof fetch;
}

Properties

PropertyTypeDefaultDescription
namestringRequiredUnique identifier
urlstringRequiredEndpoint URL
methodstring'POST'HTTP method
headersobject{}Additional request headers
batchSizenumber50Entries per batch
flushIntervalnumber10000Auto-flush interval (ms)
retryAttemptsnumber3Retry attempts with exponential backoff
filterfunctionOnly forward entries matching predicate
transformfunctionTransform entry before sending
fetchImpltypeof fetchglobalThis.fetchCustom fetch (required on Node.js < 18)

TimestampFormat

typescript
type TimestampFormat =
  | 'iso'       // 2025-03-16T10:23:45.123Z
  | 'datetime'  // 2025-03-16 10:23:45.123
  | 'date'      // 2025-03-16
  | 'time'      // 10:23:45.123
  | 'unix'      // 1710583425 (epoch seconds)
  | 'unixMs'    // 1710583425123 (epoch milliseconds)
  | 'none'      // omit timestamp
  | ((date: Date, hrTime?: number) => string); // custom function
PresetOutputNotes
'datetime'2025-03-16 10:23:45.123Default for pretty, text, and browser formatters
'iso'2025-03-16T10:23:45.123ZDefault for JSON formatter; UTC
'time'10:23:45.123Time only, local timezone
'date'2025-03-16Date only, local timezone
'unix'1710583425Epoch seconds
'unixMs'1710583425123Epoch milliseconds
'none'(empty)Omit from output
functionCustomReceives Date and optional hrTime (nanoseconds)

TimestampOptions

typescript
interface TimestampOptions {
  format?: TimestampFormat;     // default: 'datetime'
  highResolution?: boolean;     // default: false
}
PropertyTypeDefaultDescription
formatTimestampFormat'datetime'Timestamp output format
highResolutionbooleanfalseCapture nanosecond-precision timing via process.hrtime.bigint() (Node) or performance.now() (browser)

When highResolution is true, each LogEntry receives an hrTime field (nanoseconds, monotonic). The JSON formatter includes this in output as "hrTime": 123456789.


Criteria (deprecated)

typescript
type Criteria = boolean | ((logEntry: LogEntry) => boolean);

Controls whether the formatter outputs a log entry. Prefer level and format for new code.


LogLevelName

typescript
type LogLevelName = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';

FileFormat

typescript
type FileFormat = 'json' | 'text';

Used by FileTransport and StreamTransport to control the per-line serialization format.


KonsolePublic

Public interface surfaced by __Konsole.getLogger() in the browser.

typescript
// Per-logger interface (via __Konsole.getLogger())
{
  viewLogs(batchSize?: number): void;
  setTimestamp(opts: TimestampFormat | TimestampOptions): void;
  setLevel(level: LogLevelName): void;
}

// Global interface (via __Konsole)
{
  getLogger(namespace?: string): { viewLogs, setTimestamp, setLevel };
  listLoggers(): string[];
  enableAll(): void;
  disableAll(): void;
  setTimestamp(opts: TimestampFormat | TimestampOptions): void; // changes all loggers
  disableRedaction(disabled: boolean): void; // browser only — bypass redaction for debugging
}