更新:有关使用Groovy的基于所有配置的方法,请参见DeanHiller的答案。
好的,这是我最喜欢的xml方法。我为Eclipse版本执行此 *** 作,因此我可以
单击内容将我带到日志语句,然后
参见信息,以下为黑色,警告/严重为红色
由于某种原因,SO不能正确显示所有内容,但大多数似乎都在那…
<configuration scan="true" scanPeriod="30 seconds"> <appender name="STDOUT" > <filter > <evaluator > <expression> e.level.toInt() <= INFO.toInt() </expression> </evaluator> <OnMismatch>DENY</OnMismatch> <OnMatch>NEUTRAL</OnMatch> </filter> <enprer> <pattern>%date{ISO8601} %X{sessionid}-%X{user} %caller{1} %-4level: %message%n</pattern> </enprer> </appender> <appender name="STDERR" > <filter > <level>warn</level> </filter> <enprer> <pattern>%date{ISO8601} %X{sessionid}-%X{user} %caller{1} %-4level: %message%n</pattern> </enprer> <target>System.err</target> </appender> <root> <level value="INFO" /> <appender-ref ref="STDOUT"/> <appender-ref ref="STDERR"/> </root></configuration>
您可以使用Logback过滤器做一些有趣的事情。以下配置仅将警告和错误消息打印到stderr,其他所有内容打印到stdout。
logback.xml
<appender name="stdout" > <target>System.out</target> <filter /> ...</appender><appender name="stderr" > <target>System.err</target> <filter /> ...</appender><logger name="mylogger" level="debug"> <appender-ref ref="stdout" /> <appender-ref ref="stderr" /></logger>
com.foo.StdOutFilter
public class StdOutFilter extends ch.qos.logback.core.filter.AbstractMatcherFilter{ @Override public FilterReply decide(Object event) { if (!isStarted()) { return FilterReply.NEUTRAL; } LoggingEvent loggingEvent = (LoggingEvent) event; List<Level> eventsToKeep = Arrays.asList(Level.TRACE, Level.DEBUG, Level.INFO); if (eventsToKeep.contains(loggingEvent.getLevel())) { return FilterReply.NEUTRAL; } else { return FilterReply.DENY; } }}
com.foo.ErrOutFilter
public class ErrOutFilter extends ch.qos.logback.core.filter.AbstractMatcherFilter{ @Override public FilterReply decide(Object event) { if (!isStarted()) { return FilterReply.NEUTRAL; } LoggingEvent loggingEvent = (LoggingEvent) event; List<Level> eventsToKeep = Arrays.asList(Level.WARN, Level.ERROR); if (eventsToKeep.contains(loggingEvent.getLevel())) { return FilterReply.NEUTRAL; } else { return FilterReply.DENY; } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)