Tuesday, May 22, 2012

Example of log4.xml to separate your selenium framework's logs from ones generated by webdriver

It is usefull to track the log messages generated as by your framework as by the selenium itself. Here is the example on how to separate the log streams so it is convenient to collect and to review
.
<?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="Threshold" value="INFO"/>
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%-5p [%d] [%c{1}] %x - %m%n" />
  </layout>
 </appender>
 <!-- Famework generated log files -->
 <appender name="FILE_DEBUG" class="org.apache.log4j.RollingFileAppender">
  <param name="Threshold" value="DEBUG"/>
  <param name="File" value="your.framework.debug.log" />
  <param name="append" value="true" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%-5p [%d] [%c{1}] %x - %m%n" />
  </layout>
 </appender>
 <appender name="FILE_INFO" class="org.apache.log4j.RollingFileAppender">
  <param name="Threshold" value="INFO"/>
  <param name="File" value="your.framework.info.log" />
  <param name="append" value="true" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%-5p [%d] [%c{1}] %x - %m%n" />
  </layout>
 </appender>
 <!-- Selenium generated log files -->
 <appender name="FILE_DEBUG_SEL" class="org.apache.log4j.RollingFileAppender">
  <param name="Threshold" value="DEBUG"/>
  <param name="File" value="selenium.debug.log" />
  <param name="append" value="true" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%-5p [%d] [%c{1}] %x - %m%n" />
  </layout>
 </appender>
 <appender name="FILE_INFO_SEL" class="org.apache.log4j.RollingFileAppender">
  <param name="Threshold" value="INFO"/>
  <param name="File" value="selenium.info.log" />
  <param name="append" value="true" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%-5p [%d] [%c{1}] %x - %m%n" />
  </layout>
 </appender>
 <!-- Mapping of the classes -->
 <logger name="com.your.framework.automation">
  <level value="DEBUG" />
  <appender-ref ref="FILE_INFO" />
  <appender-ref ref="FILE_DEBUG" />
  <appender-ref ref="CONSOLE" />
 </logger>
 <logger name="org.apache">
  <level value="DEBUG" />
  <appender-ref ref="FILE_INFO_SEL" />
  <appender-ref ref="FILE_DEBUG_SEL" />
 </logger>
</log4j:configuration>

.
Here we starts from describing separate appenders just to target them to separate files on the hard drive. We'd like to have here four files described. To keep INFO and DEBUG level messages of framework and to keep the same information got from WebDriver.
The area under the last comment means that we push all the DEBUG messages generated under the package (aka namespace com.your.framework.automation.*) to the appenders targeted to framework log files (they are filtered then according to the desired log level) and all the messages from org.apache.* to selenium's related logs in the same way.

Note that to make it work properly you should follow the pattern like

Logger logger = Logger.getLogger(YourClassThatGeneratesLogMessage.class);

as the fully qualified name of the specified class is used to finally determine the target file of the message generated by the logger object.