当前位置: 首页 > 知识库问答 >
问题:

Log4j给出Log4j:警告:找不到记录器错误的附加器

华良平
2023-03-14

我已经把我的log4j放好了。我的webapp的lib文件夹中的properties文件我还尝试在META-INF中输入如下内容:

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

这是我的log4j.properties.我得到以上警告和记录器是不写任何文件。我也创建了文件夹结构,如上所述。

    # 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}

共有2个答案

梁丘威
2023-03-14

默认情况下,log4j查找log4j。类路径中的属性文件,因此不需要使用类org。阿帕奇。log4j。PropertyConfigurator,仅当类路径的根目录中不存在该文件时。

确保配置文件(log4j.xmllog4j.properties)位于web应用程序的类路径中,例如:

WEB-INF/classes/log4j.properties

如果两个文件都有(log4.propertieslog4j.xml),则只考虑一个文件,即log4j。xml。第一次初始化或使用org的某个实例时。阿帕奇。log4j。Logger,log4j在类路径中搜索配置文件,然后加载log4j的配置

如果要更仔细地查看搜索和加载过程,请向虚拟机添加以下参数:

-Dlog4j.debug
米丰
2023-03-14

放置log4j。属性位于WEB-INF\classes文件夹下。

加载属性文件-

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

 类似资料: