当前位置: 首页 > 知识库问答 >
问题:

如何在Spring Boot中使用Log4J2复合配置

毛峻
2023-03-14
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
    </dependency>
log4j.configurationFile=log4j2-base.xml,log4j2-env.xml

资源目录还包含log4j2-base.xml。文件log4j2-env.xml是特定于环境的JAR文件的一部分,已经放在类路径中。

日志:

DEBUG StatusLogger Configuration XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/lib/spring-boot-2.0.1.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml] initialized
DEBUG StatusLogger Starting configuration XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/lib/spring-boot-2.0.1.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml]
DEBUG StatusLogger Started configuration XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/lib/spring-boot-2.0.1.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml] OK.
TRACE StatusLogger Stopping org.apache.logging.log4j.core.config.composite.CompositeConfiguration@40575bb9 [configurations=[XmlConfiguration[location=/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/classes/log4j2-base.xml], XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/lib/myProject.jar!/log4j2-env.xml]], mergeStrategy=org.apache.logging.log4j.core.config.composite.DefaultMergeStrategy@583fc0cb, rootNode=null, listeners=[], pluginPackages=[], pluginManager=org.apache.logging.log4j.core.config.plugins.util.PluginManager@278307b, isShutdownHookEnabled=true, shutdownTimeoutMillis=0, scriptManager=org.apache.logging.log4j.core.script.ScriptManager@185463aa]...
TRACE StatusLogger CompositeConfiguration notified 8 ReliabilityStrategies that config will be stopped.
TRACE StatusLogger CompositeConfiguration stopping 7 LoggerConfigs.
TRACE StatusLogger CompositeConfiguration stopping root LoggerConfig.
TRACE StatusLogger CompositeConfiguration notifying ReliabilityStrategies that appenders will be stopped.
TRACE StatusLogger CompositeConfiguration stopping remaining Appenders.
DEBUG StatusLogger Shutting down RollingFileManager null
DEBUG StatusLogger Shutting down RollingFileManager {}null
DEBUG StatusLogger All asynchronous threads have terminated
DEBUG StatusLogger RollingFileManager shutdown completed with status true
DEBUG StatusLogger Shut down RollingFileManager null, all resources released: true
DEBUG StatusLogger Appender RollingFile stopped with status true
TRACE StatusLogger CompositeConfiguration stopped 1 remaining Appenders.
TRACE StatusLogger CompositeConfiguration cleaning Appenders from 8 LoggerConfigs.
DEBUG StatusLogger Stopped org.apache.logging.log4j.core.config.composite.CompositeConfiguration@40575bb9 [configurations=[XmlConfiguration[location=/usr/mware/jwsAppDetails/app1/webapps/myProject/WEB-INF/classes/log4j2-base.xml], XmlConfiguration[location=jar:file:/usr/mware/jwsAppDetails/app1/lib/myProject.jar!/log4j2-env.xml]], mergeStrategy=org.apache.logging.log4j.core.config.composite.DefaultMergeStrategy@583fc0cb, rootNode=null, listeners=[], pluginPackages=[], pluginManager=org.apache.logging.log4j.core.config.plugins.util.PluginManager@278307b, isShutdownHookEnabled=true, shutdownTimeoutMillis=0, scriptManager=org.apache.logging.log4j.core.script.ScriptManager@185463aa] OK
TRACE StatusLogger Reregistering MBeans after reconfigure. Selector=org.apache.logging.log4j.core.selector.ClassLoaderContextSelector@77aa3299
TRACE StatusLogger Reregistering context (1/1): '2190fd23' org.apache.logging.log4j.core.LoggerContext@64150381

共有1个答案

璩慎之
2023-03-14

我们尝试了以下方法,结果奏效了:

>

  • 将文件log4j-base.xml重命名为log4j2.xml
  • 在log4j2.component.properties中,将log4j-base.xml重命名为log4j2.xml,如下所示:

    log4j.configurationfile=log4j2.xml,log4j2-env.xml

  •  类似资料:
    • 我在$CATALINA_HOME/lib下有log4j2jar: log4j-api-2.10.0.jar log4j-core-2.10.0.jar log4j-jul-2.10.0.jar export JAVA_OPTS=“${JAVA_OPTS}-djava.util.logging.manager=org.apache.logging.log4j.jul.logmanager” 我已经删

    • 我在学习Spring Boot时遇到了一些编码问题;我想添加一个像Spring3.x那样的CharacterEncodingFilter。就像这样:

    • 需要从log4j迁移到log4j2的帮助。我有一个滚动文件追加器。我需要这个appender来使用org中的“statisticsvlayout”。perf4j。以前在log4j1中。x版本我们使用log4j中的以下标记直接引用来自任何附录的统计数据。xml。 我们无法从log4j2确认这一点。xml,因为我们无法直接使用外部布局。请告诉我如何在log4j2中配置它。xml。

    • 我为这个特殊的问题找了很多,但我没有找到任何具体的解决办法。我在一个表中有一个复合主键,这个复合主键的一个字段是另一个表的复合主键的一部分。您可以说这个特定的字段是第二个表中的外键,但是在表定义中没有定义任何独占外键约束。对于第一个表中的每个rec,第二个表中可能有多条记录。我试图使用SPringBoot-JPA-Hibernate实现这一点,但无法实现。有人能帮我吗。以下是德泰:- 我有一个US

    • 我正在尝试将基于时间的触发策略与log4j-2.13一起使用。2,它与spring boot 2.3一起嵌入。0.0释放(spring-boot-started-log4j2)。预期的功能是每天创建一个日志文件。 我的配置文件如下所示: 但是,在启动应用程序时,会引发以下异常: 如果我改变文件模式: 不再引发异常,但文件名为“log-%d{yyyyMMdd}.log”,而不是“log-202005

    • 我最近切换到ApacheLog4J2,仍然找不到使用log4j2配置hibernate日志的方法。xml。 因为我找不到解决这个问题的方法,我仍然使用log4j.properties文件显式Hibernate。这不是最好的解决方案,因为我的log4j2.xml使用JPA appender(将日志写入数据库)。我不想为Hibernate编写单独的逻辑。 有没有办法使用log4j2配置Hibernat