我已将我的log4j.properties文件放在我的webapp的lib文件夹中,我也尝试在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}
答案 0 :(得分:0)
将log4j.properties
放在WEB-INF\classes
文件夹下。
并加载属性文件 -
Properties props = new Properties();
try(InputStream is = new FileInputStream("log4j.properties")) {
props.load(is);
}
catch(IOException ex) {
...
}
答案 1 :(得分:0)
log4j在类路径中查找log4j.properties
文件,因此如果文件不存在,则不需要使用类org.apache.log4j.PropertyConfigurator
。类路径的根。
确保配置文件(log4j.xml
或log4j.properties
)位于Web应用程序的类路径中,例如:
WEB-INF/classes/log4j.properties
如果您同时拥有两个文件(log4.properties
,log4j.xml
),则只考虑一个log4j.xml
。第一次启动或使用org.apache.log4j.Logger
的某个实例时,log4j会在类路径中搜索配置文件,然后会加载log4j的配置。
如果您希望更密切地查看此搜索和加载过程,请将以下参数添加到虚拟机:
-Dlog4j.debug