Tracing

Content

Introduction

JEAF X-Fun's tracing is like an abstraction for many other tracing solutions that are available. When writing your application code you do not have to care about the tracing solution that should be used in the current environment. Therefore JEAF defines a Tracing API that is independent from any implementation. In addition to regular tracing it also adds some feature that are very helpful in enterprise environments.

JEAF knows the following trace levels trace (ordered from low to high):

  • TRACE, DEBUG, INFO, WARN, ERROR, FATAL

 

Full example class can be found here: TracingSample.java

Simple Tracing

One big difference to other tracing solutions is that JEAF's API does not require something like a logger. Instead the logger is taken from the context. This means that the current service / component from which the tracing is called is used to identify the needed "Logger". Idea behind that is that enterprise applications are usually separated into multiple services. When it comes to tracing then it's sufficient to configure tracing on the level of services / components. Through this approach usage of the API is simpler and less error-prone. If tracing is called not from within a service then the logger of the application will be used.

Simple tracing with JEAF X-Fun

// First let's start with simple tracing. Trace lTrace = XFun.getTrace(); lTrace.trace("Message with level TRACE"); lTrace.debug("Message with level DEBUG"); lTrace.info("Message with level INFO"); lTrace.warn("Message with level WARN"); lTrace.error("Message with level ERROR"); lTrace.fatal("Message with level FATAL");

 

This will cause the following output in logs. As you can see on the generated output the so called Application-ID is used as logger as we called the tracing not from within a service call.

TRACE XFunSampleApp - Message with level TRACE DEBUG XFunSampleApp - Message with level DEBUG INFO XFunSampleApp - Message with level INFO WARN XFunSampleApp - Message with level WARN ERROR XFunSampleApp - Message with level ERROR FATAL XFunSampleApp - Message with level FATAL


In addition to standard tracing JEAF X-Fun also offers additional features that help in enterprise environments. One example therefore is classes from JEAF's Internationalization features such as MessageID and ErrorCode can be directly used in Tracing-API. There you have the option to programmatically define the trace level or you can use the trace level that was defined in the message resource. In general it's recommend to not overwrite the trace level from the message definition.

Tracing in combination with ErrorCode / MessageIDs

lTrace.info(AccountingMessages.BANK_BALANCE_NOT_SUFFICIENT); lTrace.write(AccountingMessages.BANK_BALANCE_NOT_SUFFICIENT); // Of course same as for messages also traces can be parameterized. In order to avoid // garbage JEAF only converts the error code and it's parameters into a message string // if the trace level is enabled. lTrace.info(AccountingMessages.BANK_BALANCE_NOT_SUFFICIENT, "100.00 EUR"); lTrace.write(AccountingMessages.BANK_BALANCE_NOT_SUFFICIENT, "100.00 EUR");

 

Log output

Additional Tracing Features

In many cases there are several places inside your code where you want to log the same information about an object. Instead of implementing a trace specific variant of toString() you also have the option to make use of a so called ObjectFormatter. This is a class that provides a string representation of an object for tracing.

As the code below shows the implementation of such an ObjectFormatter is pretty simple. As you can see the trace output of an object can also vary depending on the current trace level.

Implementation of an ObjectFormatter

 

The tracing of the object itself is very simple. 

 

Tracing with ObjectFormatter

 

Log output


Fallback when no ObjectFormatter is defined


Closet.java

 

Log output