我正在为Sentry使用Log4j 1.x appender来跟踪错误记录。
我想忽略某些来源的警告,例如:来自记录器dre.mytest
。我可以使用log4j.properties文件执行此操作吗?目前它看起来像这样。
# Root logger option
log4j.rootLogger=INFO, stdout, Sentry
# Configure the Sentry appender, overriding the logging threshold to the WARN level
log4j.appender.Sentry=com.getsentry.raven.log4j.SentryAppender
log4j.appender.Sentry.threshold=WARN
log4j.appender.Sentry.tags=system:annotator
我试过
log4j.logger.dre.mytest=INFO, stdout
log4j.logger.dre.mytest=ERROR, Sentry
但是这不会将任何低于错误的内容记录到stdout。
答案 0 :(得分:1)
使用另一个appender解决它:
# Root logger option
log4j.rootLogger=INFO, stdout, SentryWarn
# Direct log messages to 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=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Configure Sentry appenders, each with their own threshold
log4j.appender.SentryInfo=com.getsentry.raven.log4j.SentryAppender
log4j.appender.SentryInfo.threshold=INFO
log4j.appender.SentryInfo.tags=system:app
log4j.appender.SentryWarn=com.getsentry.raven.log4j.SentryAppender
log4j.appender.SentryWarn.threshold=WARN
log4j.appender.SentryWarn.tags=system:app
log4j.appender.SentryError=com.getsentry.raven.log4j.SentryAppender
log4j.appender.SentryError.threshold=ERROR
log4j.appender.SentryError.tags=system:app
# To suppress warnings from loggers to Sentry, use the following
# log4j.logger.<your-logger-here>=INFO, stdout, SentryError
# log4j.additivity.<your-logger-here> = false
# To send info and higher to Sentry, use the following
# log4j.logger.dre.mytest=INFO, stdout, SentryInfo
# log4j.additivity.dre.mytest = false
升级到Log4j 2后,它看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"
packages="org.apache.logging.log4j.core,com.getsentry.raven.log4j2">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</Console>
<Raven name="Sentry">
<tags>system:app</tags>
</Raven>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="Sentry" level="warn"/>
</Root>
<Logger name="dre.test"
level="INFO"
additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="Sentry" level="error"/>
</Logger>
</Loggers>
</Configuration>