ballerina/log

Overview

This module provides APIs to log information when running applications.

A sample log message logged from the foo module would look as follows:

1time = 2021-05-12T11:20:29.362+05:30 level = ERROR module = myorg/foo message = "Something went wrong"

Log Levels

The log module provides APIs to log at four levels, which are DEBUG, ERROR, INFO, and WARN. By default, all log messages are logged to the console at the INFO level.

The log level can be configured via a Ballerina configuration file. To set the global log level, place the entry given below in the Config.toml file:

1[ballerina.log]
2level = "[LOG_LEVEL]"

Each module can also be assigned its own log level. To assign a log level to a module, provide the following entry in the Config.toml file:

1[[ballerina.log.modules]]
2name = "[ORG_NAME]/[MODULE_NAME]"
3level = "[LOG_LEVEL]"

Log Output

Logs are written to the stderr stream by default.

To publish the logs to a file, redirect the stderr stream to a file as follows.

1$ bal run program.bal 2> b7a-user.log

By default, logs are printed in the LogFmt format. To set the output format to JSON, place the entry given below in the Config.toml file.

1[ballerina.log]
2format = "json"

A sample log message logged from the foo module in JSON format would look as follows:

1{"time":"2021-05-12T11:26:00.021+05:30", "level":"INFO", "module":"myorg/foo", "message":"Authenticating user"}

For information on the operations, which can be performed with the log module, see the below Functions.

Functions

[5]

printDebug

Prints debug logs.

printError

Prints error logs.

printInfo

Prints info logs.

printWarn

Prints warn logs.

setOutputFile

Set the log output to a file.

Records

[1]

KeyValues

Key-Value pairs that needs to be desplayed in the log.

Enums

[1]

FileWriteOption

Represents file opening options for writing.

Types

[2]

Value

A value of anydata type.

Valuer

A function that returns anydata type.

Errors

[1]

Error

Represents Log module related errors.