LOG4J: no Appenders could be found for logger
I Issue : Getting warning message as below
=============================================================
log4j:WARN No appenders could be found for logger (LoggerCode.UnderstandLogger).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
=============================================================
Fix :
Ø To resolve above issue we need to implement Logging in our framework or class.
Ø Define the root logger properly based on the levels and syntax given below
l log4j.rootLogger = FATAL, FILE
==================================================================
1. So basically Log4j has three major components
Ø
Loggers
Ø
Appenders / Handlers
Ø Layouts / Formatters
2.
All above mentioned components are basically
work together to enable dev/QA to log message
accordingly based on the message type and levels also formatting and location where
we can see these logs.
3.
Advantage of using logger over plain System.out.println
is to categories the logging based on some specific criteria/type or level you can
say.
4.
Generally we have three questions in mind while
working with logger.
Ø
How to log
o In
Appenders with the help of Formatters
Ø
What to log
o Based
on logger function and level define in the log4j.config file
Ø
Where to log
o Appender
-console/File
Levels and visibility in Log4j |
||||||||
|
1. ALL |
2. TRACE |
3. DEBUG |
4. INFO |
5. WARN |
6. ERROR |
7. FATAL |
|
1. ALL |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
|
2. TRACE |
|
Yes |
Yes |
Yes |
Yes |
Yes |
Yes |
|
3. DEBUG |
|
|
Yes |
Yes |
Yes |
Yes |
Yes |
|
4. INFO |
|
|
|
Yes |
Yes |
Yes |
Yes |
|
5. WARN |
|
|
|
|
Yes |
Yes |
Yes |
|
6. ERROR |
|
|
|
|
|
Yes |
Yes |
|
7. FATAL |
|
|
|
|
|
|
Yes |
|
8. OFF |
|
|
|
|
|
|
|
|
Based on the
above table lets understand how we can set the levels in logger.
If in the configuration
file we set Log4j.rootLogger =
Debug it will Log all the values related to levels which are lesser then it
like Info, Warn, error etc.
But let suppose
if you set Log4j.rootLogger =
Error and try to log the values using debug , info and warn appender it will
not log these informations.
This is how we
can control the levels of logging
Log4j.Properties
file
# Define the root logger with
appender file
# where you want to log the data
while
log = C:/Users/test/Music/Batch-Feb-2021/Online/Selenium/src/results/
# Define the root logger and level
of logging
log4j.rootLogger = FATAL, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
# Rolling file appender is a child
class of file appender file to take the back of log file when they reach to
certain size in below lines you can see size is defined with backup index
log4j.appender.FILE.File=${log}/logs.out
log4j.appender.FILE.MaxFileSize=5MB
log4j.appender.FILE.MaxBackupIndex=3
# Define the layout for file
appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
# Define to print the log value with
IST timestamp
log4j.appender.FILE.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
#do not append the old file.
Create a new log file everytime
log4j.appender.dest1.Append=true
Java Program Code
package LoggerCode;
import org.apache.log4j.Logger;
public class UnderstandLogger {
static Logger log = Logger.getLogger(UnderstandLogger.class.getName());
public static void main(String[] args) {
log.debug("Hello this is a debug message");
log.info("Hello this is an info message");
log.trace("failed");
}
}
Post a Comment