在使用 log4j1 迁移到 log4j2 默认配置后
Posted
技术标签:
【中文标题】在使用 log4j1 迁移到 log4j2 默认配置后【英文标题】:After log4j1 migration to log4j2 default configuration is being used 【发布时间】:2021-11-22 07:32:37 【问题描述】:我已将以下内容添加到我的 pom.xml 文件中:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
还添加了系统属性以获得实验支持:
System.setProperty("log4j2.debug", "true");
System.setProperty("log4j1.compatibility","true");
System.setProperty("log4j.configuration","C:/playground/HelloLogging/src/main/resources/log4j.properties");
并按照 apache 网站上的说明进行操作: https://logging.apache.org/log4j/2.x/manual/migration.html 和 https://logging.apache.org/log4j/2.x/manual/compatibility.html
我想继续使用我的 v1 log4j.properties
由于某种原因,程序以默认配置运行。 没有读取我的属性文件,也没有写入输出日志文件
有人成功使用log4j2兼容功能吗?
日志说:
没有配置 Root 记录器,创建默认的 ERROR 级别 Root 带有控制台附加程序的记录器
附在完整的跟踪下面:
"C:\Program Files\Java\jdk1.8.0_231\bin\java.exe" -Dlog4j1.compatibility=true -Dlog4j2.debug=true -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=trace -Dlog4j.configurationFile=file:///C:/Logging/src/main/resources/log4j.properties "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.4\lib\idea_rt.jar=54099:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_231\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\rt.jar;C:\playground\HelloLogging\target\classes;C:\Users\maryb\.m2\repository\org\apache\logging\log4j\log4j-1.2-api\2.13.0\log4j-1.2-api-2.13.0.jar;C:\Users\maryb\.m2\repository\org\apache\logging\log4j\log4j-api\2.13.0\log4j-api-2.13.0.jar;C:\Users\maryb\.m2\repository\org\apache\logging\log4j\log4j-core\2.13.0\log4j-core-2.13.0.jar" com.mkyong.HelloExample
DEBUG StatusLogger Using ShutdownCallbackRegistry class org.apache.logging.log4j.core.util.DefaultShutdownCallbackRegistry
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger AsyncLogger.ThreadNameStrategy=UNCACHED (user specified null, default is UNCACHED)
TRACE StatusLogger Using default SystemClock for timestamps.
DEBUG StatusLogger org.apache.logging.log4j.core.util.SystemClock does not support precise timestamps.
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Took 0.121267 seconds to load 238 plugins from sun.misc.Launcher$AppClassLoader@18b4aac2
DEBUG StatusLogger PluginManager 'Converter' found 46 plugins
DEBUG StatusLogger Starting OutputStreamManager SYSTEM_OUT.false.false-1
DEBUG StatusLogger Starting LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@49993335]...
DEBUG StatusLogger Reconfiguration started for context[name=18b4aac2] at URI null (org.apache.logging.log4j.core.LoggerContext@49993335) with optional ClassLoader: null
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger PluginManager 'ConfigurationFactory' found 6 plugins
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Missing dependencies for Yaml support, ConfigurationFactory org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory is inactive
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Missing dependencies for Json support, ConfigurationFactory org.apache.logging.log4j.core.config.json.JsonConfigurationFactory is inactive
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@5e853265
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Apache Log4j Core 2.13.0 initializing configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@59e5ddf
DEBUG StatusLogger Installed 1 script engine
DEBUG StatusLogger Oracle Nashorn version: 1.8.0_231, language: ECMAScript, threading: Not Thread Safe, compile: true, names: [nashorn, Nashorn, js, JS, javascript, javascript, ECMAScript, ecmascript], factory class: jdk.nashorn.api.scripting.NashornScriptEngineFactory
DEBUG StatusLogger PluginManager 'Core' found 123 plugins
DEBUG StatusLogger PluginManager 'Level' found 0 plugins
DEBUG StatusLogger PluginManager 'Lookup' found 15 plugins
DEBUG StatusLogger Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
DEBUG StatusLogger createLoggers(=)
DEBUG StatusLogger Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
DEBUG StatusLogger createAppenders(=)
WARN StatusLogger No Root logger was configured, creating default ERROR-level Root logger with Console appender
DEBUG StatusLogger PluginManager 'Converter' found 46 plugins
DEBUG StatusLogger Starting OutputStreamManager SYSTEM_OUT.false.false-2
DEBUG StatusLogger Configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@59e5ddf initialized
DEBUG StatusLogger Starting configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@59e5ddf
DEBUG StatusLogger Started configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@59e5ddf OK.
TRACE StatusLogger Stopping org.apache.logging.log4j.core.config.DefaultConfiguration@4361bd48...
TRACE StatusLogger DefaultConfiguration notified 1 ReliabilityStrategies that config will be stopped.
TRACE StatusLogger DefaultConfiguration stopping root LoggerConfig.
TRACE StatusLogger DefaultConfiguration notifying ReliabilityStrategies that appenders will be stopped.
TRACE StatusLogger DefaultConfiguration stopping remaining Appenders.
DEBUG StatusLogger Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
DEBUG StatusLogger Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
DEBUG StatusLogger Appender DefaultConsole-1 stopped with status true
TRACE StatusLogger DefaultConfiguration stopped 1 remaining Appenders.
TRACE StatusLogger DefaultConfiguration cleaning Appenders from 1 LoggerConfigs.
DEBUG StatusLogger Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@4361bd48 OK
TRACE StatusLogger Reregistering MBeans after reconfigure. Selector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector@6302bbb1
TRACE StatusLogger Reregistering context (1/1): '18b4aac2' org.apache.logging.log4j.core.LoggerContext@49993335
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2'
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=StatusLogger'
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=ContextSelector'
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=*'
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=*'
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncAppenders,name=*'
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncLoggerRingBuffer'
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=*,subtype=RingBuffer'
DEBUG StatusLogger Registering MBean org.apache.logging.log4j2:type=18b4aac2
DEBUG StatusLogger Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=StatusLogger
DEBUG StatusLogger Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=ContextSelector
DEBUG StatusLogger Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=DefaultConsole-2
TRACE StatusLogger Using default SystemClock for timestamps.
DEBUG StatusLogger org.apache.logging.log4j.core.util.SystemClock does not support precise timestamps.
TRACE StatusLogger Using DummyNanoClock for nanosecond timestamps.
DEBUG StatusLogger Reconfiguration complete for context[name=18b4aac2] at URI C:\playground\HelloLogging\src\main\resources\log4j.properties (org.apache.logging.log4j.core.LoggerContext@49993335) with optional ClassLoader: null
DEBUG StatusLogger Shutdown hook enabled. Registering a new one.
DEBUG StatusLogger LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@49993335] started OK.
09:37:08.310 [main] ERROR com.mkyong.HelloExample - This is error : mkyong
09:37:08.315 [main] FATAL com.mkyong.HelloExample - This is fatal : mkyong
DEBUG StatusLogger Stopping LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@49993335]
DEBUG StatusLogger Stopping LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@49993335]...
TRACE StatusLogger Unregistering 1 MBeans: [org.apache.logging.log4j2:type=18b4aac2]
TRACE StatusLogger Unregistering 1 MBeans: [org.apache.logging.log4j2:type=18b4aac2,component=StatusLogger]
TRACE StatusLogger Unregistering 1 MBeans: [org.apache.logging.log4j2:type=18b4aac2,component=ContextSelector]
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=*'
TRACE StatusLogger Unregistering 1 MBeans: [org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=DefaultConsole-2]
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncAppenders,name=*'
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=AsyncLoggerRingBuffer'
TRACE StatusLogger Unregistering but no MBeans found matching 'org.apache.logging.log4j2:type=18b4aac2,component=Loggers,name=*,subtype=RingBuffer'
TRACE StatusLogger Stopping org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@59e5ddf...
TRACE StatusLogger PropertiesConfiguration notified 1 ReliabilityStrategies that config will be stopped.
TRACE StatusLogger PropertiesConfiguration stopping root LoggerConfig.
TRACE StatusLogger PropertiesConfiguration notifying ReliabilityStrategies that appenders will be stopped.
TRACE StatusLogger PropertiesConfiguration stopping remaining Appenders.
DEBUG StatusLogger Shutting down OutputStreamManager SYSTEM_OUT.false.false-2
DEBUG StatusLogger Shut down OutputStreamManager SYSTEM_OUT.false.false-2, all resources released: true
DEBUG StatusLogger Appender DefaultConsole-2 stopped with status true
TRACE StatusLogger PropertiesConfiguration stopped 1 remaining Appenders.
TRACE StatusLogger PropertiesConfiguration cleaning Appenders from 1 LoggerConfigs.
DEBUG StatusLogger Stopped org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@59e5ddf OK
DEBUG StatusLogger Stopped LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@49993335] with status true
Process finished with exit code 0
enter code here
【问题讨论】:
由于记录器通常是静态字段,Log4j 可能会在您在代码中设置这些系统属性之前初始化。您需要使用 JVM-Dproperty=value
命令参数设置这些属性。
我在 vm 选项中添加了属性,并将属性文件路径添加到项目的类路径中。我已经从程序内部验证了它,设置了属性。 code CLASSPATH=%CLASSPATH%\;C:/myProj/src/main/resources/log4j.properties code 看起来属性文件正在被读取,但无法识别。我收到以下错误:code WARN StatusLogger 未配置根记录器,使用控制台附加程序创建默认错误级根记录器 code 知道如何克服这个问题吗?
您能否将状态记录器日志添加到您的问题中(编辑您的问题)?问题也可能在您的log4j.properties
文件中。
谢谢,我已经添加了。版本 1.2.17 使用了相同的 log4j.properties 我想在版本 2x 中使用它
【参考方案1】:
由于Log4j 2.x也有properties配置格式,要区分这两种格式,需要使用:
log4j.configurationFile
属性指定严格的 Log4j 2.x 配置文件列表,
log4j.configuration
属性用于指定单个 Log4j 1.2 配置文件。
在您的情况下,将 log4j.configurationFile
替换为 log4j.configuration
。
【讨论】:
谢谢!这看起来更好。现在它确实读取了调试并且在标准输出和文件上失败了。这是日志:DEBUG StatusLogger Parsing for [root] with value=[DEBUG, stdout, file]。 DEBUG StatusLogger 级别标记为 [DEBUG]。 DEBUG StatusLogger Logger 根级别设置为 DEBUG DEBUG StatusLogger Parsing appender,名为“stdout”。 ERROR StatusLogger Unable to create Appender org.apache.logging.log4j.ConsoleAppender due to ClassNotFoundException:org.apache.logging.log4j.ConsoleAppender 我发现了问题:当我将 org.apache.log4j 搜索/替换为 org.apache.logging.log4j 时,我也替换了 log4j.properties 文件。但我需要将 log4j.properties 文件保留为旧的 log4j1 格式。因此,在还原此更改后,它开始工作。谢谢以上是关于在使用 log4j1 迁移到 log4j2 默认配置后的主要内容,如果未能解决你的问题,请参考以下文章