Log4j2的Configuration详解

  • Post author:
  • Post category:其他




引言

官方配置文档:

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





<Configuration>

节点



Parameters

Attribute Name Description
advertiser (Optional) The Advertiser plugin name which will be used to advertise individual FileAppender or SocketAppender configurations. The only Advertiser plugin provided is ‘multicastdns”.
dest Either “err” for stderr, “out” for stdout, a file path, or a URL.

monitorInterval
设置配置文件的

动态加载

时间的,单位是秒

The minimum amount of time, in seconds, that must elapse before the file configuration is checked for changes.

Log4j has the ability to

automatically


detect changes

to the configuration file and

reconfigure itself

.for example,

<Configuration monitorInterval="30">

mean the configuration file will be checked for changes only after at least

30 seconds

have elapsed. The

minimum

interval is 5 seconds.

name
The

name

of the configuration.
packages
Use of the packages attribute is deprecated and will be removed in Log4j 3.0.

Plugins should be processed with the Log4j annotation processor. A comma separated list of package names to search for plugins. Plugins are only loaded once per classloader so changing this value may not have any effect upon reconfiguration.
schema Identifies the location for the classloader to located the XML Schema to use to validate the configuration. Only valid when strict is set to true. If not set no schema validation will take place.
shutdownHook Specifies whether or not Log4j should automatically shutdown when the JVM shuts down. The shutdown hook is enabled by default but may be disabled by setting this attribute to “disable”
shutdownTimeout Specifies how many milliseconds appenders and background tasks will get to shutdown when the JVM shuts down. Default is zero which mean that each appender uses its default timeout, and don’t wait for background tasks. Not all appenders will honor this, it is a hint and not an absolute guarantee that the shutdown procedure will not take longer. Setting this too low increase the risk of losing outstanding log events not yet written to the final destination. See

LoggerContext.stop(long, java.util.concurrent.TimeUnit)

. (Not used if

shutdownHook

is set to “disable”.)

status
指定log4j本身的打印日志的级别

The level of

internal Log4j events

that should

be logged to the console

. Valid values for this attribute are “off”, “trace”, “debug”, “info”, “warn”, “error”, “fatal”, and “all”. Log4j will

log details about initialization, rollover and other internal actions

to the status logger. Setting

status="trace"

is one of the first tools available to you if you need to

troubleshoot log4j

.(Alternatively, setting

system property


log4j2.debug

will also

print internal Log4j2 logging

to the

console

, including

internal logging

that took place

before the configuration file was found

.)
strict Enables the use of the strict XML format. Not supported in JSON configurations.
verbose Enables diagnostic information while loading plugins.



场景1:

<!--设置log4j2自身内部的信息输出级别为info-->
<!--Log4j每隔10秒自动检测修改配置文件和重新配置本身,不需要重启服务-->
<Configuration status="info" monitorInterval="10">
</Configuration>

<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL-->