import ballerina/http;
import ballerina/log;@http:ServiceConfig {
    basePath: "/hello"
}
service helloService on new http:Listener(9095) {
    @http:ResourceConfig {
        methods: ["GET"],
        path: "/"
    }
    resource function hello(http:Caller caller, http:Request request) {
        var result = caller->respond("Successful");
        if (result is error) {
            log:printError("Error occurred while responding", err = result);
        }
    }
}

Access Logs

Ballerina supports HTTP access logs for HTTP services. The access log format used is the combined log format.

import ballerina/http;
import ballerina/log;
@http:ServiceConfig {
    basePath: "/hello"
}
service helloService on new http:Listener(9095) {
    @http:ResourceConfig {
        methods: ["GET"],
        path: "/"
    }
    resource function hello(http:Caller caller, http:Request request) {
        var result = caller->respond("Successful");

Respond with the message “Successful” for each request.

        if (result is error) {
            log:printError("Error occurred while responding", err = result);
        }
    }
}

Log the error in case of a failure.

# At the command line, navigate to the directory that contains the
# `.bal` file and do one of the following depending on whether you want the logs to be logged on the console, or in a file.
# Run the service by setting the `--b7a.http.accesslog.console=true` property to log to the console.
$ ballerina run http_access_logs.bal --b7a.http.accesslog.console=true 
ballerina: HTTP access log enabled
[ballerina/http] started HTTP/WS listener 0.0.0.0:9095
127.0.0.1 - - [30/Oct/2018:11:23:08 +0530] "GET /hello HTTP/1.1" 200 10 "-" "curl/7.58.0"
# Or, run the service by setting `--b7a.http.accesslog.path=path/to/file.txt` to log to the specified file.
$ ballerina run http_access_logs.bal --b7a.http.accesslog.path=path/to/file.txt 
ballerina: HTTP access log enabled
[ballerina/http] started HTTP/WS listener 0.0.0.0:9095
# Invoke the service.
$ curl -k http://localhost:9095/hello
Successful