Log4J: What Is log4J-Log4j Is Free Open Source Tool Given by Apache
Log4J: What Is log4J-Log4j Is Free Open Source Tool Given by Apache
Components of log4J
1. Loggers
2. Appenders
3. Layouts
Note: In log4j properties we can call appender with any name. There are other
appenders as well but we will restrict to these few.
The Logger class provides different methods to handle logging activities. It provides
two static methods for obtaining a Logger Object.
Step 3- Open notepad and copy the below code and save the file
as log4j.properties.
Note- Please create the log folder inside home directory and while
saving use “” to save file
1
2
3
4
5
6
7
8
// Here we have defined root logger
9
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC
1
0
// Here we define the appender
1
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
1
log4j.appender.R=org.apache.log4j.RollingFileAppender
1
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
2
log4j.appender.HTML=org.apache.log4j.FileAppender
1
3
// Here we define log file location
1
log4j.appender.R.File=./log/testlog.log
4
log4j.appender.TTCC.File=./log/testlog1.log
1
log4j.appender.HTML.File=./log/application.html
5
1
// Here we define the layout and pattern
6
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
1
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
7
log4j.appender.R.layout=org.apache.log4j.PatternLayout
1
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
8
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
1
log4j.appender.TTCC.layout.DateFormat=ISO8601
9
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
2
log4j.appender.HTML.layout.Title=Application log
0
log4j.appender.HTML.layout.LocationInfo=true
2
1
2
2
2
3
2
4
Step 5- Execute your test case and verify the output and log files
as well
Finally, here are three different log files.
Log4j properties
log4j.properties Syntax:
Following is the syntax of log4j.properties file for an appender X:
# Define the root logger with appender X
log4j.rootLogger = DEBUG, X
log4j.properties Example
Using the above syntax, we define the following in log4j.properties file:
Debug Level
We have used DEBUG with both the appenders. All the possible options are:
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
ALL