我要求将INFO(不是错误和较低级别的INFO)日志写入控制台,将ERROR日志写入文件。需要使用log4j。
答案 0 :(得分:0)
您需要在appender上为控制台输出设置阈值。这是从log4j发行版修改的示例:
# A1 is set to be a FileAppender sending its output to
# System.out. However, only info messages and above will be printed
# in A1 because A1's threshold is set to Level.INFO.
# The fact that the root level is set to Prority.DEBUG only influences
# log requests made to the root logger. It has no influence on the
# *appenders* attached to root.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Threshold=INFO
这样,打印到ConsoleAppender
A1
的任何内容都将被忽略,如果它不是INFO
级别的话。如果您将日志记录级别设置为INFO
,则只会记录INFO
消息。
在apache log4j网站上有一个FAQ question。
答案 1 :(得分:0)
试试这个。
log.file.path=C:/log
datestamp=yyyy-MM-dd HH:mm:ss.SSS
# Define the root logger with appender file
log4j.rootLogger=INFO, stdout
log4j.logger.biz.netweb=error, LOG_FILE
#, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p,[%d{${datestamp}}],[Thread:%t],[%C.%M(%L)]==>: %m%n
# LOG_FILE
log4j.appender.LOG_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOG_FILE.File=${log.file.path}/system-log/system-log.txt
log4j.appender.LOG_FILE.ImmediateFlush=true
log4j.appender.LOG_FILE.MaxFileSize=5MB
log4j.appender.LOG_FILE.MaxBackupIndex=50
log4j.appender.LOG_FILE.Append=true
log4j.appender.LOG_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG_FILE.layout.conversionPattern=%5p,[%d{${datestamp}}],[%t,%C.%M(%L)]==>, %m%n
答案 2 :(得分:0)
log4j.rootLogger = debug,stdout,D,E
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log ## log file name
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## ERROR log info!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
答案 3 :(得分:0)
下面的log4j配置文件将仅以“ INFO”级别登录到控制台,而在“ INFO”和“ FATAL”之间登录到log.log文件。 我要更改日志级别,可以在控制台和滚动文件附加程序上更改LevelRangeFilter部分的MinLevel / MaxLevel参数。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--Redirect log messages to console-->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<!--Redirect log messages to a log file, support file rolling.-->
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO"/>
<param name="File" value="log.log"/>
<param name="maxFileSize" value="2Mb"/>
<param name="maxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L -- %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<level value="INFO"></level>
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>