Type alias EventFormatter

EventFormatter: ((event, fullMessage) => string)
Overview

A function that formats an event into a string.

Used with BaseClientEvent.format, which dictates how the event should be logged to stdout/stderr.

There are two ways to use this method:

  • Pass it to BaseClientEvent.format if you're manually formatting an event.
  • Set it as the default formatter using BaseClientEvent.setDefaultFormatter if you want all events to use the same formatter.
    • This is useful if you want to use the default stdout/stderr logging, but still want to customize the format.
Default format

The default format is [timestamp] [requestId[0..8]] [eventName]: message.

  • The timestamp is of the format YYYY-MM-DD HH:MM:SS TZ.
  • The requestId is the first 8 characters of the requestId.
  • The eventName is the name of the event.
  • The message is the message generated by the event.

For example:

2025-02-11 12:24:59 IST [e31bc40e] [CommandFailed]: (default_keyspace.basic_logging_example_table) findOne (took 249ms) - 'Invalid filter expression: filter clause path ('$invalid') contains character(s) not allowed'
Custom formatter example

Type declaration

    • (event, fullMessage): string
    • Parameters

      Returns string

Example

// Define a custom formatter
const customFormatter: EventFormatter = (event, message) => {
return `[${event.requestId.slice(0, 8)}] (${event.name}) - ${message}`;
}

// Set the custom formatter as the default
BaseClientEvent.setDefaultFormatter(customFormatter);

// Now all events will use the custom formatter
const coll = db.collection('*COLLECTION_NAME*', {
logging: [{ events: 'all', emits: 'stdout' }],
});

// Logs:
// - [e31bc40e] (CommandStarted) - (default_keyspace.basic_logging_example_table) findOne
// - [e31bc40e] (CommandFailed) - (default_keyspace.basic_logging_example_table) findOne (took 249ms) - 'Invalid filter expression: filter clause path ('$invalid') contains character(s) not allowed'
coll.findOne({ $invalid: 1 });

See

  • LoggingConfig
  • BaseClientEvent.setDefaultFormatter