// Create a `DataAPIClient` with emission enabled for all failed/warning commands
const client = new DataAPIClient('*TOKEN*', {
logging: [{ events: /.*(Failed|Warning)/, emits: ['stderr:verbose', 'event']}],
});
client.on('commandFailed', (event) => {
console.error('Some command failed:', event.commandName);
});
// Override the logging config for this `Db` to emit *all* events as events
const db = client.db('*ENDPOINT*', {
logging: [{ events: 'all', emits: 'event' }],
});
db.on('commandStarted', (event) => {
console.log('Command started:', event.commandName);
});
db.on('commandSucceeded', (event) => {
console.log('Command succeeded:', event.commandName);
});
// Resulting output:
// 'Command started: "createCollection"'
// 'Some command failed: "createCollection"'
await db.createCollection('$invalid-name$');
Various configuration examples:
// Set sane defaults for logging
const client = new DataAPIClient({
logging: 'all',
});
// Just emit all events as events
const client = new DataAPIClient({
logging: [{ events: 'all', emits: 'event' }],
});
// Define specific outputs for specific events
const client = new DataAPIClient({
logging: [
{ events: ['commandStarted', 'commandSucceeded'], emits: ['stdout', 'event'] },
{ events: ['commandFailed'], emits: ['stderr', 'event'] },
],
});
// Use 'all' as a base configuration, and override specific events
const client = new DataAPIClient({
logging: ['all', { events: /.*(Started|Succeeded)/, emits: [] }],
});
// Enable specific events with default behaviors
const client = new DataAPIClient({
logging: ['commandSucceeded', 'adminCommandStarted'],
});
Overview
The configuration for logging events that may be logged or emitted by the DataAPIClient or any of its children classes.
This can be set at any level of the major class hierarchy, and will be inherited by all child classes.
Configuration inheritance
Logging is configured through a list of hierarchical rules, determining which events to emit where. Each rule layer overrides previous rules for the same events.
Logging configuration is inherited by child classes, but may be overridden at any level (e.g. pass a base config to the
DataAPIClient
constructor, and then override it for a specificCollection
).Configuration & shorthands
There are multiple ways to configure logging, and the rules may be combined in various ways:
logging: 'all'
logging: [{ events: 'all', emits: 'event' }]
logging: '<event>' | ['<events>']
logging: /<regex>/ | [/<regex>/]
logging: [{ events: ['<events>'], emits: ['<outputs>'] }]
logging: ['all', { events: ['<events>'], emits: [] }]
'all'
may be used as a base configuration, to be overridden by specific rulesemits
array effectively disables outputs for the specified eventsEvent types & defaults
See DataAPIClientEventMap for more information on the types of events emitted & their defaults.
As a TL;DR, when enabled, all events are emitted as events by default, and
commandStarted
andcommandSucceeded
are the only events not logged tostderr
as well by default; all other events are logged tostderr
.Output types
The
emits
field can be set to either'event'
,'stdout'
,'stderr'
, or their verbose variants.'event'
will emit the event to each HierarchicalEmitter in the hierarchyCollection
, then theDb
, then theDataAPIClient
'stdout'
&'stderr'
will log the event to stdout or stderr, respectively'stdout:verbose'
&'stderr:verbose'
variants will use a verbose format containing all the events propertiesYou may use BaseClientEvent.setDefaultFormatter to change the format of the events as they're logged to
stdout
/stderr
. See EventFormatter for more information.Examples
For more advanced examples, see the
examples/logging
directory in the astra-db-ts repositoryHierarchical usage example: