Interfaces
Logging Interface
Explore the Logging interface that provides structured logging capabilities across your Antelopejs application.
Key Features
The Logging
interface provides a structured, standardized way to log information across your Antelopejs application. It offers multiple severity levels, channel-based categorization, and an event-based architecture for capturing and processing log entries.
- Severity levels: Predefined levels (ERROR, WARN, INFO, DEBUG, TRACE) for categorizing log importance
- Channels: Separate logging channels for different components or concerns
- Event-based: Uses the EventProxy system for flexible log processing
- Standardized API: Consistent logging interface across the entire application
Basic Usage
Importing the Logging Interface
import { Logging } from '@ajs/logging/beta';
Using Standard Logging Methods
// Log an error
Logging.Error('Failed to connect to database', error);
// Log a warning
Logging.Warn('Connection pool running low', { available: 2, total: 10 });
// Log general information
Logging.Info('User logged in', { userId: 'user123' });
// Log debug information
Logging.Debug('Processing request', request);
// Log highly detailed trace information
Logging.Trace('Function called with arguments', args);
Using Custom Channels
// Log to a custom channel with specific severity
Logging.Write(Logging.Level.INFO, 'database', 'Query executed in 15ms', { query, params });
Log Levels
The Logging interface provides five standard severity levels:
Level | Value | Description | Use Case |
---|---|---|---|
ERROR | 40 | Critical errors that may cause application failure | Failed database connections, API errors, crashes |
WARN | 30 | Important issues that don't prevent functioning | Degraded performance, retries, deprecated feature use |
INFO | 20 | General application information and status updates | User actions, business events, normal operations |
DEBUG | 10 | Detailed information useful for debugging | Variable values, control flow, decision points |
TRACE | 0 | Highly detailed tracing information | Function entry/exit, loop iterations, detailed flow |
Listening to Log Events
You can capture and process logs using the event system:
import eventLog from '@ajs/logging/beta/listener';
// Register a log listener
eventLog.register((log) => {
const { time, channel, levelId, args } = log;
// Example: Send to console
const timestamp = new Date(time).toISOString();
const level = getLevelName(levelId);
if (levelId >= Logging.Level.WARN) {
sendToExternalService({ timestamp, level, channel, message: args });
}
});
// Helper function to get level name
function getLevelName(levelId: number): string {
switch (levelId) {
case Logging.Level.ERROR: return 'ERROR';
case Logging.Level.WARN: return 'WARN';
case Logging.Level.INFO: return 'INFO';
case Logging.Level.DEBUG: return 'DEBUG';
case Logging.Level.TRACE: return 'TRACE';
default: return 'UNKNOWN';
}
}