import ballerina/io;
import ballerina/log;
public function main() {
error e = error("error occurred");
// The Ballerina log API provides functions to log at five levels, which are
// `DEBUG`, `ERROR`, `INFO`, `TRACE`, and `WARN`. By default, all log
// messages are logged to the console at the `INFO` level. In addition to
// these log levels, there are 2 additional levels named `OFF` and `ALL`.
// `OFF` turns off logging and `ALL` enables all the log levels. The log
// level can be configured via a Ballerina configuration file or CLI
// parameters.
log:printDebug("debug log");
log:printError("error log");
log:printError("error log with cause", e);
log:printInfo("info log");
log:printTrace("trace log");
log:printWarn("warn log");
// To set the log level of the API, use the following CLI parameter: <br>
// `--b7a.log.level=[LOG_LEVEL]`
//
// To configure using a configuration file, place the entry given below in
// the file:
//
// ```
// [b7a.log]
// level="[LOG_LEVEL]"
// ```
// Each module can also be assigned its own log level. To assign a
// log level to a module, provide the following configuration
// `<MODULE_NAME>.loglevel`.
//
// E.g., `--foo.loglevel=DEBUG`
Fruit apple = new ("Apple");
Fruit orange = new ("Orange");
log:printDebug("Name of the fruit is Strawberry.");
log:printDebug(io:sprintf("Names of the fruits are %s, %s.", apple.getName(), orange.getName()));
// Logic constructing log messages with expensive operations can alternatively be passed as a function
// pointer implementation. The function will be executed if and only if that particular log level is enabled.
log:printDebug(function() returns string {
return io:sprintf("Name of the fruit is is %s", apple.getName());
});
}
public type Fruit object {
string name;
public function __init(string name) {
self.name = name;
}
function getName() returns string {
return self.name;
}
};
LogThe |
import ballerina/io;
import ballerina/log;
public function main() {
error e = error("error occurred");
log:printDebug("debug log");
log:printError("error log");
log:printError("error log with cause", e);
log:printInfo("info log");
log:printTrace("trace log");
log:printWarn("warn log");
The Ballerina log API provides functions to log at five levels, which are
DEBUG
, ERROR
, INFO
, TRACE
, and WARN
. By default, all log
messages are logged to the console at the INFO
level. In addition to
these log levels, there are 2 additional levels named OFF
and ALL
.
OFF
turns off logging and ALL
enables all the log levels. The log
level can be configured via a Ballerina configuration file or CLI
parameters.
To set the log level of the API, use the following CLI parameter:
--b7a.log.level=[LOG_LEVEL]
To configure using a configuration file, place the entry given below in the file:
[b7a.log]
level="[LOG_LEVEL]"
Fruit apple = new ("Apple");
Fruit orange = new ("Orange");
Each module can also be assigned its own log level. To assign a
log level to a module, provide the following configuration
<MODULE_NAME>.loglevel
.
E.g., --foo.loglevel=DEBUG
log:printDebug("Name of the fruit is Strawberry.");
log:printDebug(io:sprintf("Names of the fruits are %s, %s.", apple.getName(), orange.getName()));
log:printDebug(function() returns string {
return io:sprintf("Name of the fruit is is %s", apple.getName());
});
}
Logic constructing log messages with expensive operations can alternatively be passed as a function pointer implementation. The function will be executed if and only if that particular log level is enabled.
public type Fruit object {
string name;
public function __init(string name) {
self.name = name;
}
function getName() returns string {
return self.name;
}
};
# As shown in the output, only the `INFO` and higher level logs are logged by default.
# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run log_api.bal
2019-08-09 11:47:07,334 ERROR [ballerina/log] - error log
2019-08-09 11:47:07,340 ERROR [ballerina/log] - error log with cause : error error occurred
2019-08-09 11:47:07,341 INFO [ballerina/log] - info log
2019-08-09 11:47:07,342 WARN [ballerina/log] - warn log
# If the log level is set to `TRACE`, logs of all log levels are logged.
ballerina run log_api.bal --b7a.log.level=TRACE
2019-08-09 11:48:04,411 DEBUG [ballerina/log] - debug log
2019-08-09 11:48:04,415 ERROR [ballerina/log] - error log
2019-08-09 11:48:04,416 ERROR [ballerina/log] - error log with cause : error error occurred
2019-08-09 11:48:04,416 INFO [ballerina/log] - info log
2019-08-09 11:48:04,417 TRACE [ballerina/log] - trace log
2019-08-09 11:48:04,421 WARN [ballerina/log] - warn log
2019-08-09 12:17:35,946 DEBUG [ballerina/log] - Name of the fruit is Strawberry.
2019-08-09 12:17:35,947 DEBUG [ballerina/log] - Names of the fruits are Apple, Orange.
2019-08-09 12:17:35,947 DEBUG [ballerina/log] - Name of the fruit is is Apple.