Logging in Curam
Curam uses log4j for logging. By default it only logs stuff that you put in using curam.util.resources.Trace to SystemOut.log. However, since it is using log4j for logging you can tweak it in many different ways by putting your own log4j configuration file.
Setting up log4j configuration
There is a property that you can change using the web UI or through Application.prx called curam.trace.configfile.location. This specifies the path to a log4j xml configuration file. The following is an example I got from the log4j wiki that you can use to get yourselves started which will dump practically the same data as the standard logger.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> </layout> </appender> <root> <priority value ="debug" /> <appender-ref ref="console" /> </root> </log4j:configuration>
Tuning log4j configuration
Of course you'd probably want to tune things in logging like:
- Only show level of "ERROR" and above for Root logger
- Only show level of "WARN" and above for "Trace" logger. The "Trace" logger is where Curam sends its trace logs when you do Trace.kTopLevelLogger.
- Only show level of "ERROR" and above for "curam.customapp" logger. This is an example of a custom application logger.
- Show the timestamp.
This ends up being:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p %c{1} - %m%n"/> </layout> </appender> <logger name="Trace"> <level value ="warn" /> <appender-ref ref="console" /> </logger> <logger name="curam.customapp"> <level value ="error" /> <appender-ref ref="console" /> </logger> <root> <priority value="error" /> <appender-ref ref="console" /> </root> </log4j:configuration>
Just don't forget that trace_ultra_verbose messages will not appear with the above configuration so you'd lose some logging. So how do we get around that? Well thanks to log4j, you can create a separate appender to put stuff in there, but since Curam does not include log4j-extras.jar so org.apache.log4j.varia.LevelRangeFilter is not available.
Custom application logging
There are two ways of doing application logging in Curam.
- The best way to set up logging in for your custom application is to create a custom application logger so you can dictate the structure of what gets logged. However, you need to do some extra coding.
- The easy way is to use Trace.kTopLevelLogger so you don't need to set anything else up. But you lose the flexibility that log4j provides.
The choice generally depends on the logging "control" requirements of the solution.
To perform custom logging, all you have to do is standard log4j. That means
// Define a logger private final Logger log = Logger.getLogger(this.class); // Use the logger log.trace("hello world");
I just define the logger within a private final variable in the class. We don't need it in static usually because most Curam process classes are not meant to be executed in a static fashion. Plus that line can be copied and pasted without having to change anything. If it were static then we need to explicitly specify the class name.

0 comments:
Post a Comment