Get in touch Contact icon Contact icon Contact icon Request a demo

Digital Commerce Insights

Preparing Brands for Exponential Growth

Logging in Java Applications

By Fernando Cano | May 3, 2016

Categories: Mozu Developers & Users

Logging in Java Applications

Java provides excellent logging capabilities that allow you to implement a logging strategy that is easy to code, easy to change, and standards based. This blog demonstrates how to use sl4j, a logging abstraction layer that allows you to select between various logging frameworks for your logging purposes (in our example, we choose log4j). You can apply this logging strategy to a standard Java application or to a Mozu Java SDK application.

The main components of the logging code include:

  • A log4j XML configuration file that determines where logging is displayed and at what level.
  • A Java logger class that specifies different logging levels.

As structured, one of the principal benefits of the code is that it provides different levels of logging without re-coding.

Project Dependencies

Add the following dependencies to the classpath to enable the logging capabilities discussed in this blog:

  • sl4j-api
  • sl4j-log4j
  • log4j

Configure log4j

Log4j uses an XML configuration file to handle different setups across different environments. For example, in development, you may want to output logs to both the console and a file, while in production, you may want to output only to a continuously rolling file that has a max size limit. In QA, you may want more verbose logging. Clearly, the flexibility to address different logging scenarios is beneficial, and you can provide this flexibility in the log4j XML file.

Configure log4j.xml as shown in the following code block:

Code Comments:

  • The <root> element defines the high-level configuration.
  • The default log level is set to info.
  • There are two appenders (output locations). One is the console and the other a rolling file for logging output, given a minimum threshold of debug for the log entry.
  • A <logger> element takes a package with a level of debug.
  • Because the default log level is info, the code logs info, warn, error, and fatal messages. By using the <logger> element, you can choose to get even finer logging of any package hierarchy. This is useful when you are only looking for verbose logging from a specific framework or from your own custom classes.

Configure your Java Class File

To incorporate logging into a Java class file, instantiate a static logger from the sl4j logger factory. Since log4j is the only logging provider available, the code finds the log4j.xml file and configures the logging as specified. The method used to log a message determines the logging level produced, so if you are only interested in errors, the debug and trace messages are not logged.

Configure your Java class file as shown in the following code block:

Configure Logging in the Mozu Java SDK

The Mozu Java SDK uses the same logging frameworks discussed in this blog. Therefore, to configure logging through the SDK, you can create a <logger> element on the com.mozu.api package name in your log4j.xml file.

Since the SDK provides an Apache Commons HttpClient, you have advanced logging features available through the <logger> element. Some areas of interest are the client context, request/response over the wire, and HTTP header logging, as shown in the following code block:


Leave a reply