import ballerina/log;
import ballerina/io;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");
    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()));
    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;
     }
};

Log

The ballerina/log module contains the Ballerina-log-handling functions including ‘DEBUG’, ‘ERROR’, ‘INFO’, ‘TRACE’, ‘WARN’, ‘OFF’, and ‘ALL’.

import ballerina/log;
import ballerina/io;
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());
    });
}

Retrieving log messages through complex operations is allowed through an optional function pointer parameter that will be executed if and only if that particular log level is activated.

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.
$ 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.