When you place @Log annotation on a class, aspect4log will log execution of every method and constructor in it. When you place @Log annotation on a method or constructor you override all parameters of @Log annotation on this class. Default behavior is the following:
@Log public class OrderService { ... }
By default indent logging is on and each nested method call is logged with an indent.
An example of how log might look like with indent on:
21:24:16 DEBUG r.Robot : ↓start() 21:24:16 DEBUG r.Robot : ↓ readConfiguration(/path/to/configuration) 21:24:16 DEBUG r.Robot : ↑ readConfiguration(/path/to/configuration) → Configuration(ttl=4000,numbersOfCpuToUse=1) 21:24:16 DEBUG r.Robot : ↓ launch(Configuration(ttl=4000,numbersOfCpuToUse=1)) 21:24:16 DEBUG r.Robot : ↑ launch(Configuration(ttl=4000,numbersOfCpuToUse=1)) 21:24:16 DEBUG r.Robot : ↑start()
To control it use indent attribute of @Log annotation. In case you want to log a method which is using recursion you may consider to disable indent.
@Log(indent=false) public List<Item> recursiveItemSearch(Item itemExample){ //... seach code goes here ... return recursiveItemSearch(itemExample); }
In case use you want to use a Logger to add additional messages to the log with current indent level, instantiate logger via LoggerFactory.
There are cases when you want to switch a log level for a method/constructor enter/exit from the default DEBUG level. Some of these cases are:
Here is an example of how to set log levels of the particular method to NONE:
@Log(enterLevel=Level.NONE, exitLevel=Level.NONE, on=@Exceptions(level=Level.NONE)) public List<Item> recursiveItemSearch(Item itemExample){ //... here could be search code ... return recursiveItemSearch(itemExample); }
There are cases when you want to switch from the default exception log levels, some of such cases are:
@Log(on = {@Exceptions(exceptions = {RuntimeException.class}), @Exceptions(exceptions = {PaymentException.class,SecurityException.class}, level=Level.WARN)}) public void pay(Customer customer, Order order, String cardNumber) throws SecurityException, PaymentException { ... }
Stack trace printing is very useful when something unexpected happens. By default a stack trace is printed for runtime exceptions and not printed for checked exceptions. To change this behavior use stackTrace attribute of @Exceptions annotation.
@Log(on = {@Exceptions(exceptions = {RuntimeException.class}, stackTrace = true), @Exceptions(exceptions = {PaymentException.class,SecurityException.class}, level=Level.WARN, stackTrace = false)}) public void pay(Customer customer, Order order, String cardNumber) throws SecurityException, PaymentException { ... }
By default all arguments and result are logged by calling toString() method. There are cases when you may want to customize what is logged. Some of these cases are:
In order to control arguments and result log representation you may change @Log.argumentsTemplate @Log.resultTemplate and @Exceptions.template default values. For each of those attributes the corresponding context variables are available:
@Log(argumentsTemplate = "customerId=${args[0].id}", resultTemplate = "orderId=${result.id}") public Order createOrder(Customer customer, String address, Item... items){ ... }
Mapped Diagnostic Context aka MDC support is a very important feature of slf4j and underlying logging systems such as logback and log4j. During concurrent program execution there maybe several threads executing the same method simultaneously. The log will be written in the order of execution in time, to distinguish one thread from another you can place an MDC key to a thread specific value. To analyze log use utility like unix grep to extract log lines with the specific MDC key value. E.g. customerId is a reasonable candidate for the MDC key.
@Log( mdcKey = "customerId", mdcTemplate = "${args[0].id}") public Order createOrder(Customer customer, String address, Item... items){ ... }
Log decorations are can be configured via aspect4log.xml and aspect4log-test.xml placed in root of a classpath. If aspect4log-test.xml is found in classpath, it is used instead of aspect4log.xml. Usually you will place aspect4log-test.xml in to maven src/test/resources folder, while aspect4log.xml is placed to src/main/resources.
The parameters that may be changed are:
Make sure that your logging framework uses UTF8, see how it is done with logback in our sample project in the quick start sections.
In case you want to alter default settings, download file aspect4log.xml , change required values and place it to your classpath.
It is worth mentioning that some old operating system do not support UTF8 in console or even IDE (i.e. they don't have unicode fonts). In this case it is recommend to use an alternative aspect4log.xml that do not use unicode symbols.