Adding logging to applications

When you are running WebSphere® Application Server Liberty applications, you can use Java logging (using java.util.logging) for generating application logging.

About this task

This approach has advantages over adding System.out.println statements to your code:
  • Your messages are displayed in the messages.log and trace.log files, using a standard message format with additional data, such as a date and time stamp that are added automatically.
  • You can more easily correlate problems and events in your own application to problems and events that are associated with Liberty components.
  • You can use the Liberty log file management features.
  • You can control your logger levels using the logging element's traceSpecification in server.xml file.

Procedure

Configure the LogRecordContext API to add name and value pairs to your log and trace records.
Liberty provides an API that you can use to add context, in the form of name-value pairs, to your log and trace records. These name-value pairs are displayed in the following locations:
  • The messages.log file when that log is configured to use JSON format
  • Console output when your console is configured to use JSON format
  • Records sent by logstashCollector to Logstash

Use the following example to include name-value pairs using JSON format:

LogRecordContext API
Applications that use the LogRecordContext API will have the name-value pairs that have been added to the LogRecordContext included in the JSON mapping for logs and trace emitted on the same thread.

STRING/STRING pairs
// included in json at root level as "ext_someName":"someValue"
LogRecordContext.addExtension("someName","someValue");          

STRING/INTEGER pairs
// included in json at root level as "ext_someName_int":someValue (or entirely omitted if someValue isn't parseable as an int)
LogRecordContext.addExtension("someName_int","someValue");      

STRING/FLOAT pairs
// included in json at root level as "ext_someName_float":someValue (or entirely omitted if someValue isn't parseable as a float)
LogRecordContext.addExtension("someName_float","someValue");      

STRING/BOOLEAN pairs
// included in json at root level as "ext_someName_bool":someValue (or entirely omitted if someValue isn't parseable as a bool)
LogRecordContext.addExtension("someName_bool","someValue");

See the following example of name-value pairs that the LogRecordContext API uses:

LogRecordContext.addExtension("userName","don");          
LogRecordContext.addExtension("isGoldCustomer_bool":"true");
Logger.info("some message");
LogRecordContext.removeExtension("userName");
LogRecordContext.removeExtension("isGoldCustomer_bool");
Logger.info("some other message");

// first message includes the extensions
{"ibm_datetime":"2018-02-04T18:56:30.318-0500","type":"liberty_message","host":"192.168.1.1","ibm_userDir":"\/wlp\/usr\/","ibm_serverName":"server1","ibm_sequence":"1517788590318_000000003A4A7","loglevel":"INFO","module":"com.ibm.somelogger.QuickLogTest","ibm_threadId":"00002db5","message":"some message", "ext_userName":"don", "isGoldCustomer_bool":true}

// second message does not include extensions
{"ibm_datetime":"2018-02-04T18:56:30.318-0500","type":"liberty_message","host":"192.168.1.1","ibm_userDir":"\/wlp\/usr\/","ibm_serverName":"server1","ibm_sequence":"1517788590318_000000003A4A8","loglevel":"INFO","module":"com.ibm.somelogger.QuickLogTest","ibm_threadId":"00002db5","message":"some other message"}