Introducing the log4j framework

Debugging the framework is an equally important activity when errors occur. Logging is the process of creating logs of the framework execution. Because of this, errors occurring during the execution can be easily diagnosed, since we know from the log about the last successful action that happened.

Log4j is an open source framework for logging. For using the Log4J framework, the following dependency has to be added in pom.xml:

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>test</scope>
</dependency>

Follow these steps to download the Log4J dependency:

  1. Save the pom.xml
  2. Right-click on Project and select Update Project
  3. The required dependency will get downloaded

In the case of firewall restrictions, a manual download of the log4J JAR has to be done, and later this Jar has to be added to the project build path by right-clicking on the project in Eclipse, then selecting properties. In the properties popup, select Java Build Path and click on Libraries. Add the log4j jar by clicking Add External JARs and selecting the JAR from the local drive.

For using Log4J, we either require a log4j.xml or log4j.properties for configuring log4j to generate logs. We will be using log4j.properties in this chapter.

Log4J.properties is shown in the following snippet:

#Define the root logger. Level of Root logger is defined as DEBUG
log4j.rootLogger=DEBUG, CONSOLE, LOGFILE

#Define the CONSOLE Appender, the threshhold, the layout and the conversion #pattern
# The logging levels are in the order ALL(Integer.MAX_VALUE) < TRACE(600) < #DEBUG(500) < #INFO(400) < WARN(300) < ERROR(200) < FATAL(100) < OFF(0). Since we #have kept the logging #level # at INFO here, all INFO,WARN,ERROR,FATAL messages #will be displayed. The DEBUG and #TRACE level messages are not displayed

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %C{1}:%L - %m%n
# Define the File Appender
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.File=D:\logging.log
log4j.appender.LOGFILE.MaxFileSize=20MB
log4j.appender.LOGFILE.MaxBackupIndex=5
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=INFO

The log messages will be pushed to the D:logging.log file, which will be appended. On reaching 50 MB, a new file will get created. The new file will be an archived version and in the type it will have the version number listed. For example, the first file will be of type 1, the second of type 2. When the version 2 file is created, the version 1 file data will be pushed to version 2 and the current version data will be pushed to version 1. The next thing worth mentioning is that there can be only five versions that can be created. After the fifth version, once maxFileSize is reached, data will start flowing from the current version to version 1, from version 1 to version 2, version 2 to version 3, and so on. Data in version 5 that was originally there will be overwritten by data from version 4.

Here is what a sample log looks like:

2018-08-14 14:29:05 INFO Navigate:101 - Navigating to http://www.freecrm.com
2018-08-14 14:29:11 INFO WorkspaceSelection:35 - Created Logger object
2018-08-14 14:29:11 INFO WorkspaceSelection:90 - Entering SelectWindowByTitleAndURL
2018-08-14 14:29:16 INFO WorkspaceSelection:98 - windowset size: 2

Let's dissect a sample line from the log:

2018-08-14 14:29:16 INFO WorkspaceSelection:98 - windowset size: 2

The lines from log4j.properties that correspond to the following output are as follows:

log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %C{1}:%L - %m%n

In the previous two lines of code, %d{yyyy-MM-dd HH:mm:ss} corresponds to 2018-08-14 14:29:16 in the output. %-5p corresponds to the text INFO. The '-' in %-5p indicates that the entry should be left justified with maximum five characters.
'%C{1}:' corresponds to 'WorkspaceSelection:'. This specifies the class name.
'%L -' corresponds to the line number in the class followed by a hyphen.
'%m corresponds to the application supplied message windowset size: 2.
%n corresponds to the platform dependent line separator character or characters.

For details on the Log4J framework, refer to https://logging.apache.org/log4j/2.x/javadoc.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.170.183