What are Events?
Events are timestamped milestones within a trace. They record significant moments in your application flow, such as user actions, state changes, or external interactions.Adding Events
UseaddEvent() to record an event:
With Details
Events can include additional details as a string or object:With Custom Timestamp
By default, events use the current time. You can provide a custom timestamp using aDate object:
Event Patterns
User Interaction Flow
Error Handling
State Transitions
Method Signature
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Event name |
details | string | object | No | Additional context |
options | AddEventOptions | No | Options (e.g. { captureStackTrace: true, timestamp: date }) |
AddEventOptions
| Option | Type | Default | Description |
|---|---|---|---|
captureStackTrace | boolean | false | Capture stack trace at event location |
timestamp | Date | now | Custom timestamp for the event |
severity | Severity | — | Event severity level: info, warn, or error |
Severity Levels
Events can carry a severity level to distinguish informational milestones from warnings and errors. You can set the severity explicitly via theseverity option:
Convenience Methods
info(), warn(), and error() are shortcuts that set the severity automatically:
When to Use Each Level
| Level | Use For | Examples |
|---|---|---|
info | Normal milestones and successful operations | User actions, state transitions, completions |
warn | Degraded states or conditions that may need attention | Rate limits, retries, fallback paths |
error | Failures that prevent an operation from completing | Reverted transactions, API errors, timeouts |
Example: Severity in a Try/Catch Flow
Best Practices
Use Consistent Naming
Adopt a naming convention for events:Record Both Success and Failure
Always capture the outcome:Include Relevant Context
Add details that help with debugging:Don’t Over-Event
Record significant milestones, not every micro-operation:Next Steps
Attributes & Tags
Add metadata to your traces
Transaction Hints
Correlate with blockchain transactions