Skip to main content

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:

LevelDescription
traceDetailed information for diagnosing issues, typically used for debugging.
debugDetailed information useful for debugging.
infoGeneral information about the application's operation.
warnInformation about potential issues that do not prevent the application from functioning.
errorInformation about errors that affect the application's ability to function correctly.
fatalCritical 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:

FormatterDescription
SimpleFormatterOutputs log messages in plain text format, which is suitable for console output and simple logging needs.
PrettyFormatterOutputs log messages in a human-readable format with colors and indentation, which is suitable for console output and debugging.
JsonFormatterOutputs log messages in JSON format, which is suitable for structured logging and integration with log management systems.
XmlFormatterOutputs log messages in XML format, which is suitable for structured logging and integration with log management systems.
TemplateFormatterAllows 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:

custom-formatter.ts
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​

TransporterDescription
ConsoleTransporterSends log messages to the console (stdout).
DailyFileTransporterSends log messages to a specified file, creating a new file each day.
FileTransporterSends log messages to a specified file.
HttpTransporterSends log messages to a specified HTTP endpoint.
MemoryTransporterSends log messages to memory.
NullTransporterDiscards log messages, effectively disabling logging.
RotatingFileTransporterSends log messages to a specified file, rotating the file when it reaches a certain size or age.
StreamTransporterSends log messages to a specified writable stream.
WebhookTransporterSends 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:

custom-transporter.ts
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.

app.ts
AxiSparkFactory.create({
logTransports: [
new ConsoleTransport({
minLevel: LogLevel.Info,
formatter: new SimpleFormatter()
})
]
...
});

You can add multiple transporters to the logTransports array, allowing you to send log messages to multiple destinations simultaneously. Each transporter can have its own configuration, including the minimum logging level and formatter.