简体   繁体   中英

Log4j giving log4j:WARN No appenders could be found for logger error

I have placed my log4j.properties file in lib folder of my webapp also I tried to make entry in META-INF as:

Manifest-Version: 1.0
Class-Path: ..\WEB-INF\lib\log4j.properties

here is my log4j.properties. I am getting above warning and logger is not writing anything to files. I have created folder structure also as mentioned in it. any help is appreciated.

    # Define the root logger with appender file
log4j.rootLogger = DEBUG,ERROR1,INFO1,DEBUG1

# Define the file appender
log4j.appender.ERROR1=org.apache.log4j.RollingFileAppender
# Set the name of the file
log4j.appender.ERROR1.File=G:\\logs\\error\\Errorlog.log

# Set the immediate flush to true (default)
log4j.appender.ERROR1.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.ERROR1.Threshold=error

# Set the append to false, should not overwrite
log4j.appender.ERROR1.Append=true

# Set the maximum file size before rollover
log4j.appender.ERROR1.MaxFileSize=20MB

# Set the the backup index
log4j.appender.ERROR1.MaxBackupIndex=100

# Define the layout for file appender
log4j.appender.ERROR1.layout=org.apache.log4j.PatternLayout
log4j.appender.ERROR1.layout.conversionPattern=%d{dd/MM/yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n

# file rolling pattern defined here
log4j.appender.ERROR1.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.ERROR1.RollingPolicy.FileNamePattern=G:\logs\error\Errorlog.log.%d{yyyy-MM-dd-HH-mm-ss}

########################################################INFO logs###################################################

# Define the file appender
log4j.appender.INFO1=org.apache.log4j.RollingFileAppender
# Set the name of the file
log4j.appender.INFO1.File=G:\\logs\\info\\Infolog.log

# Set the immediate flush to true (default)
log4j.appender.INFO1.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.INFO1.Threshold=info

# Set the append to false, should not overwrite
log4j.appender.INFO1.Append=true

# Set the maximum file size before rollover
log4j.appender.INFO1.MaxFileSize=20MB

# Set the the backup index
log4j.appender.INFO1.MaxBackupIndex=100

# Define the layout for file appender
log4j.appender.INFO1.layout=org.apache.log4j.PatternLayout
log4j.appender.INFO1.layout.conversionPattern=%d{dd/MM/yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n

# file rolling pattern defined here
log4j.appender.INFO1.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.INFO1.RollingPolicy.FileNamePattern=G:\logs\info\Infolog.log.%d{yyyy-MM-dd-HH-mm-ss}

########################################################DEBUG logs###################################################

# Define the file appender
log4j.appender.DEBUG1=org.apache.log4j.RollingFileAppender
# Set the name of the file
log4j.appender.DEBUG1.File=G:\\logs\\debug\\debuglog.log

# Set the immediate flush to true (default)
log4j.appender.DEBUG1.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.DEBUG1.Threshold=debug

# Set the append to false, should not overwrite
log4j.appender.DEBUG1.Append=true

# Set the maximum file size before rollover
log4j.appender.DEBUG1.MaxFileSize=20MB

# Set the the backup index
log4j.appender.DEBUG1.MaxBackupIndex=100

# Define the layout for file appender
log4j.appender.DEBUG1.layout=org.apache.log4j.PatternLayout
log4j.appender.DEBUG1.layout.conversionPattern=%d{dd/MM/yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n

# file rolling pattern defined here
log4j.appender.DEBUG1.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.DEBUG1.RollingPolicy.FileNamePattern=G:\logs\info\Infolog.log.%d{yyyy-MM-dd-HH-mm-ss}

Place the log4j.properties under WEB-INF\\classes folder.

And load the property file -

Properties props = new Properties();
try(InputStream is = new FileInputStream("log4j.properties")) {
    props.load(is);
}
catch(IOException ex) {
   ...
}

log4j, by default, looks for the log4j.properties file in the classpath, so you don't need to use the class org.apache.log4j.PropertyConfigurator , ony if the file doesn't exist in the root of the classpath.

Make sure the configuration file ( log4j.xml or log4j.properties ) is in the classpath of the web application, eg:

WEB-INF/classes/log4j.properties

If you have both files ( log4.properties , log4j.xml ) only one is considered, the log4j.xml . The first time you init or use some instance of org.apache.log4j.Logger , log4j search the configuration file in the classpath, then the configuration for log4j is loaded.

If you want to see this process of searching and loading more closely, add the following argument to the virtual machine:

-Dlog4j.debug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM