Log4j2设置日志级别

Ric*_*ard 3 java logging spring rollingfileappender log4j2

我在我的春季应用程序中使用log4j2作为我的日志工具.我想将特定库/包的日志级别设置为与根不同的日志级别.例如,我想要org.springframeworkINFO并com.google成为WARN.我发现这个log4j2.properties:

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
logger.rolling.name = com.test.app
logger.rolling.level = ALL
logger.rolling.appenderRef.rolling.ref = RollingFile

rootLogger.level = info
Run Code Online (Sandbox Code Playgroud)

我不明白是什么logger.rolling意思?我一直在log4j2文档中找到它,但没有解释它是什么或什么ALL意思.

如何为特定包添加日志级别以及这些滚动级别的内容是什么?

Dev*_*and 7

Level intLevel
OFF   0
FATAL 100
ERROR 200
WARN  300
INFO  400
DEBUG 500
TRACE 600
ALL   Integer.MAX_VALUE
Run Code Online (Sandbox Code Playgroud)

https://logging.apache.org/log4j/2.x/manual/customloglevels.html

log4j记录器将记录其阈值下的所有事件,因此如果设置为ALL,它将记录每个事件,因为它使用其阈值的最大值.

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
Run Code Online (Sandbox Code Playgroud)

这是一个名为RollingFile的新滚动文件追加器.滚动文件appender是一个appender,例如,可以增长到固定大小,然后在删除最旧的条目时继续添加新条目.它们也可以是基于时间的等等.

logger.rolling.name = com.test.app
logger.rolling.level = ALL
logger.rolling.appenderRef.rolling.ref = RollingFile
Run Code Online (Sandbox Code Playgroud)

这告诉log4j将来自名为com.test.app的记录器的任何事件发送到前面提到的RollingFile appender.log4j不会过滤掉任何事件,因为此记录器的级别设置为ALL."logger.rolling"中的"rolling"只是记录器的标识符.这是必要的,因为属性文件是非结构化的,因此您需要一种方法来区分哪些行组合在一起.使用XML配置消除了这种需求

rootLogger.level = info
Run Code Online (Sandbox Code Playgroud)

由属性中未定义的其他记录器创建的任何事件都将被过滤,并且仅记录INFO或以下.

文档在这里:https://logging.apache.org/log4j/2.x/manual/configuration.html#Properties

要回答您的问题,这取决于在应用程序中如何创建记录器.记录器实际上是在java中创建的,这是他们的名称分配的位置.属性文件仅指示log4j如何处理每个记录器.您需要使用类中定义的名称将记录器添加到属性文件中.如果类本身用于创建记录器,就像apache建议的那样,那么名称将始终是类的完全限定名称.例如:

 logger.secondclass.name = com.test.AnotherClass
 logger.secondclass.level = DEBUG
 logger.secondclass.appenderRef.rolling.ref = RollingFile
Run Code Online (Sandbox Code Playgroud)

现在,com.test.AnotherClass创建的任何DEBUG或以下事件也将被发送到RollingFile appender.

所有这些都在这里解释:https://logging.apache.org/log4j/2.x/manual/architecture.html