java - log4j2.xml 已加载但未应用 [JVM 参数]

标签 java xml spring spring-boot spring-batch

我正在尝试使用 Spring Batch、Spring MVC 和 Spring Boot 创建批处理。我使用 log4j2 进行日志记录。

由于像这样的 JVM 参数,我的目标是加载外部 log4j2.xml 配置文件

-Dlog4j.configurationFile=file://C:\{path}\Workspace\demo-indexeur\config\log4j2.xml

Spring boot 检测我的文件(我在控制台中没有错误)并加载它,但不应用我放入文件中的配置。

这是我的配置文件加载的调试:

2015-05-06 15:36:53,279 DEBUG Starting configuration XmlConfiguration[location=C:\{path}\Workspace\demo-indexeur\config\log4j2.xml]
2015-05-06 15:36:53,279 DEBUG PluginManager 'Core' found 69 plugins
2015-05-06 15:36:53,279 DEBUG PluginManager 'Level' found 0 plugins
2015-05-06 15:36:53,279 DEBUG PluginManager 'Lookup' found 10 plugins
2015-05-06 15:36:53,279 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout]. Searching for builder factory method...
2015-05-06 15:36:53,279 DEBUG Found builder factory method [newBuilder]: public static org.apache.logging.log4j.core.layout.PatternLayout$Builder org.apache.logging.log4j.core.layout.PatternLayout.newBuilder().
2015-05-06 15:36:53,289 DEBUG TypeConverterRegistry initializing.
2015-05-06 15:36:53,289 DEBUG PluginManager 'TypeConverter' found 21 plugins
2015-05-06 15:36:53,299 DEBUG Calling build() on class class org.apache.logging.log4j.core.layout.PatternLayout$Builder for element PatternLayout with params(pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", Configuration(C:\{path}\Workspace\demo-indexeur\config\log4j2.xml), null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null")
2015-05-06 15:36:53,299 DEBUG PluginManager 'Converter' found 33 plugins
2015-05-06 15:36:53,299 DEBUG Built Plugin[name=layout] OK from builder factory method.
2015-05-06 15:36:53,299 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender]. Searching for builder factory method...
2015-05-06 15:36:53,299 INFO Log4j appears to be running in a Servlet environment, but there's no log4j-web module available. If you want better web container support, please add the log4j-web JAR to your web archive or server lib directory.
2015-05-06 15:36:53,299 DEBUG PluginManager 'Converter' found 33 plugins
2015-05-06 15:36:53,299 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2015-05-06 15:36:53,299 DEBUG Found builder factory method [newBuilder]: public static org.apache.logging.log4j.core.appender.ConsoleAppender$Builder org.apache.logging.log4j.core.appender.ConsoleAppender.newBuilder().
2015-05-06 15:36:53,299 DEBUG No PluginVisitorStrategy found on annotation [interface org.apache.logging.log4j.core.config.plugins.validation.constraints.Required]. Ignoring.
2015-05-06 15:36:53,299 DEBUG Encountered type [org.apache.logging.log4j.core.layout.PatternLayout] which can only be checked for null.
2015-05-06 15:36:53,309 DEBUG No PluginVisitorStrategy found on annotation [interface org.apache.logging.log4j.core.config.plugins.validation.constraints.Required]. Ignoring.
2015-05-06 15:36:53,309 DEBUG Encountered type [org.apache.logging.log4j.core.appender.ConsoleAppender$Target] which can only be checked for null.
2015-05-06 15:36:53,309 DEBUG No PluginVisitorStrategy found on annotation [interface org.apache.logging.log4j.core.config.plugins.validation.constraints.Required]. Ignoring.
2015-05-06 15:36:53,309 DEBUG Calling build() on class class org.apache.logging.log4j.core.appender.ConsoleAppender$Builder for element Console with params(PatternLayout(%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n), null, target="SYSTEM_OUT", name="Console", follow="null", ignoreExceptions="null")
2015-05-06 15:36:53,309 DEBUG Jansi is not installed, cannot find org.fusesource.jansi.WindowsAnsiOutputStream
2015-05-06 15:36:53,309 DEBUG Built Plugin[name=appender] OK from builder factory method.
2015-05-06 15:36:53,309 DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin]. Searching for builder factory method...
2015-05-06 15:36:53,309 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.AppendersPlugin. Going to try finding a factory method instead.
2015-05-06 15:36:53,309 DEBUG Still building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin]. Searching for factory method...
2015-05-06 15:36:53,309 DEBUG Found factory method [createAppenders]: public static java.util.concurrent.ConcurrentMap org.apache.logging.log4j.core.config.AppendersPlugin.createAppenders(org.apache.logging.log4j.core.Appender[]).
2015-05-06 15:36:53,309 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.AppendersPlugin for element Appenders with params(={Console})
2015-05-06 15:36:53,309 DEBUG Built Plugin[name=appenders] OK from factory method.
2015-05-06 15:36:53,309 DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for builder factory method...
2015-05-06 15:36:53,309 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.AppenderRef. Going to try finding a factory method instead.
2015-05-06 15:36:53,309 DEBUG Still building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef]. Searching for factory method...
2015-05-06 15:36:53,309 DEBUG Found factory method [createAppenderRef]: public static org.apache.logging.log4j.core.config.AppenderRef org.apache.logging.log4j.core.config.AppenderRef.createAppenderRef(java.lang.String,org.apache.logging.log4j.Level,org.apache.logging.log4j.core.Filter).
2015-05-06 15:36:53,309 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.AppenderRef for element AppenderRef with params(ref="Console", level="null", null)
2015-05-06 15:36:53,309 DEBUG Built Plugin[name=AppenderRef] OK from factory method.
2015-05-06 15:36:53,309 DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger]. Searching for builder factory method...
2015-05-06 15:36:53,309 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger. Going to try finding a factory method instead.
2015-05-06 15:36:53,309 DEBUG Still building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger]. Searching for factory method...
2015-05-06 15:36:53,309 DEBUG Found factory method [createLogger]: public static org.apache.logging.log4j.core.config.LoggerConfig org.apache.logging.log4j.core.config.LoggerConfig$RootLogger.createLogger(java.lang.String,org.apache.logging.log4j.Level,java.lang.String,org.apache.logging.log4j.core.config.AppenderRef[],org.apache.logging.log4j.core.config.Property[],org.apache.logging.log4j.core.config.Configuration,org.apache.logging.log4j.core.Filter).
2015-05-06 15:36:53,319 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element Root with params(additivity="null", level="ERROR", includeLocation="null", ={Console}, ={}, Configuration(C:\{path}\Workspace\demo-indexeur\config\log4j2.xml), null)
2015-05-06 15:36:53,319 DEBUG Built Plugin[name=root] OK from factory method.
2015-05-06 15:36:53,319 DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin]. Searching for builder factory method...
2015-05-06 15:36:53,319 DEBUG No builder factory method found in class org.apache.logging.log4j.core.config.LoggersPlugin. Going to try finding a factory method instead.
2015-05-06 15:36:53,319 DEBUG Still building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin]. Searching for factory method...
2015-05-06 15:36:53,319 DEBUG Found factory method [createLoggers]: public static org.apache.logging.log4j.core.config.Loggers org.apache.logging.log4j.core.config.LoggersPlugin.createLoggers(org.apache.logging.log4j.core.config.LoggerConfig[]).
2015-05-06 15:36:53,319 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.LoggersPlugin for element Loggers with params(={root})
2015-05-06 15:36:53,319 DEBUG Built Plugin[name=loggers] OK from factory method.
2015-05-06 15:36:53,319 DEBUG Started configuration XmlConfiguration[location=C:\{path}\Workspace\demo-indexeur\config\log4j2.xml] OK.
2015-05-06 15:36:53,319 TRACE Stopping org.apache.logging.log4j.core.config.DefaultConfiguration@5b87edb3...
2015-05-06 15:36:53,319 TRACE AbstractConfiguration stopped 0 AsyncLoggerConfigs.
2015-05-06 15:36:53,319 TRACE AbstractConfiguration stopped 0 AsyncAppenders.
2015-05-06 15:36:53,319 TRACE AbstractConfiguration stopped 1 Appenders.
2015-05-06 15:36:53,319 TRACE AbstractConfiguration stopped 0 Loggers.
2015-05-06 15:36:53,319 DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@5bc79274 OK
2015-05-06 15:36:53,319 DEBUG Registering MBean org.apache.logging.log4j2:type=sun.misc.Launcher$AppClassLoader@c387f44
2015-05-06 15:36:53,329 DEBUG Registering MBean org.apache.logging.log4j2:type=sun.misc.Launcher$AppClassLoader@c387f44,component=StatusLogger
2015-05-06 15:36:53,329 DEBUG Registering MBean org.apache.logging.log4j2:type=sun.misc.Launcher$AppClassLoader@c387f44,component=ContextSelector
2015-05-06 15:36:53,329 DEBUG Registering MBean org.apache.logging.log4j2:type=sun.misc.Launcher$AppClassLoader@c387f44,component=Loggers,name=
2015-05-06 15:36:53,329 DEBUG Registering MBean org.apache.logging.log4j2:type=sun.misc.Launcher$AppClassLoader@c387f44,component=Appenders,name=Console
2015-05-06 15:36:53,329 DEBUG Reconfiguration complete for context[name=sun.misc.Launcher$AppClassLoader@c387f44] at null (org.apache.logging.log4j.core.LoggerContext@72b6cbeb) with optional ClassLoader: null
2015-05-06 15:36:53,329 DEBUG Shutdown hook enabled. Registering a new one.
2015-05-06 15:36:53,329 DEBUG LoggerContext[name=sun.misc.Launcher$AppClassLoader@c387f44, org.apache.logging.log4j.core.LoggerContext@7fac633a] started OK.
2015-05-06 15:36:53,489 DEBUG Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@6973bf95
2015-05-06 15:36:53,489 INFO Log4j appears to be running in a Servlet environment, but there's no log4j-web module available. If you want better web container support, please add the log4j-web JAR to your web archive or server lib directory.

当我将文件移动到“src/main/resources”内时,它工作得很好,但是当我尝试外部化该文件时,它却不起作用。

这是我的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

这是我的依赖项:

<!--    Spring Batch -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
        <exclusions>
<!--            Suppression de SLF4J -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

<!--    Spring web-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
<!--            Suppression de SLF4J -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

<!--    Utilisation de LOG4J2 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

有人可以告诉我为什么当我外部化文件时它不起作用吗? 感谢您的帮助。

最佳答案

Log4j has the ability to automatically configure itself during initialization. Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration.

这就是为什么我的文件被检测到并正确加载但未被 Spring Boot 使用。

我忘记的是告诉 spring boot 在哪里可以找到日志配置文件,为此,我必须在 spring boot 的 application.properties 中添加:

logging.config= # location of logging config file

之后,一切正常。

关于java - log4j2.xml 已加载但未应用 [JVM 参数],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076778/

相关文章:

c# - 在浏览器中打开大型 aspx 文件时编译错误 CS1647

c# - XPath 未按预期工作

java - 如何处理 Spring Security AuthenticationProvider 抛出的运行时异常?

Java FX 无法加载 Font Awesome 图标

java - 将许可的应用程序限制在服务器上

java - .hasNext() 上的无限循环

java - Jetty - WebSocket.sendMessage(String data) 和 void sendMessage(byte frame,String data) 有什么区别?

xml - serde-xml-rs 反序列化 u8 有效但 u16 无效

spring - 如何从 Spring security 3.1 获取当前用户角色

java - 通过注释和 xml 上下文连接 Spring bean