Logging
Logging is an essential part of any application, as it allows you to track the behavior of your application and diagnose issues when they arise. In AxiSpark.js, you can use the built-in logging system to log messages at different levels (e.g., info, warn, error) and configure the logging output to suit your needs.
Logging Levels
The logging system in AxiSpark.js supports the following levels, ordered from lowest to highest priority:
| Level | Description |
|---|---|
trace | Detailed information for diagnosing issues, typically used for debugging. |
debug | Detailed information useful for debugging. |
info | General information about the application's operation. |
warn | Information about potential issues that do not prevent the application from functioning. |
error | Information about errors that affect the application's ability to function correctly. |
fatal | Critical errors that cause the application to terminate. |
Logging Formatters
AxiSpark.js allows you to configure the logging output format using formatters. Formatters define how log messages are structured and displayed. You can use built-in formatters or create your own custom formatters to suit your needs.
Built-in Formatters
Here are the built-in formatters available in AxiSpark.js. In total are 5 built-in formatters, which you can use to format your log messages. You can choose the formatter that best fits your application's logging requirements:
| Formatter | Description |
|---|---|
SimpleFormatter | Outputs log messages in plain text format, which is suitable for console output and simple logging needs. |
PrettyFormatter | Outputs log messages in a human-readable format with colors and indentation, which is suitable for console output and debugging. |
JsonFormatter | Outputs log messages in JSON format, which is suitable for structured logging and integration with log management systems. |
XmlFormatter | Outputs log messages in XML format, which is suitable for structured logging and integration with log management systems. |
TemplateFormatter | Allows you to define your own custom formatter by providing a template string. |
Create your own formatter
In order to create your own custom formatter, you can extend the LogFormatter class and implement the format method. The format method takes a log message object as input and returns a formatted string. Here's an example of how to create a custom formatter that outputs log messages in a specific format:
import { LogEntry, LogFormatter } from '@axisparkjs/logger';
export class CustomFormatter implements LogFormatter {
format(entry: LogEntry): string {
...
return `Custom formatted message`;
}
}
Logging Transporters
Transporters are responsible for sending log messages to their final destination, such as a file, console, or external logging service. AxiSpark.js provides several built-in transporters that you can use to send log messages to different destinations.
Built-in Transporters
| Transporter | Description |
|---|---|
ConsoleTransporter | Sends log messages to the console (stdout). |
DailyFileTransporter | Sends log messages to a specified file, creating a new file each day. |
FileTransporter | Sends log messages to a specified file. |
HttpTransporter | Sends log messages to a specified HTTP endpoint. |
MemoryTransporter | Sends log messages to memory. |
NullTransporter | Discards log messages, effectively disabling logging. |
RotatingFileTransporter | Sends log messages to a specified file, rotating the file when it reaches a certain size or age. |
StreamTransporter | Sends log messages to a specified writable stream. |
WebhookTransporter | Sends log messages to a webhook endpoint. |
Create your own transporter
As well as formatters, you can create your own custom transporter by extending the LogTransport class and implementing the write method. The write method takes a log message object as input and sends it to the desired destination. Here's an example of how to create a custom transporter that sends log messages to a specific destination:
import { LogEntry, LogTransport } from '@axisparkjs/logger';
export class CustomTransport extends LogTransport {
async write(entry: LogEntry): Promise<void> {
if (entry.level < this.config.minLevel) return;
console.log(this.config.formatter.format(entry));
}
}
Configuring the application logger
When you create an AxiSpark.js application, you can configure the logger by providing a configuration object to the AxiSparkFactory method. The configuration object allows you to specify the logging level, formatter, and transporter for the application logger.
AxiSparkFactory.create({
logTransports: [
new ConsoleTransport({
minLevel: LogLevel.Info,
formatter: new SimpleFormatter()
})
]
...
});
You can add multiple transporters to the
logTransportsarray, allowing you to send log messages to multiple destinations simultaneously. Each transporter can have its own configuration, including the minimum logging level and formatter.