错误 StatusLogger 找不到 log4j2 配置文件.在更新 Struts 2.5.12 版本时

2022-01-16 00:00:00 logging migration java log4j2 struts2

根据要求和安全目的,我正在更新 Struts 项目.之前的 Struts 版本是 2.3.24,现在我将它更新到 2.5.12.我已经下载了 Struts 所需的所有 jar 文件并应用于项目,但出现以下错误

As per the requirement and for security purpose I am updating Struts project. Before the Struts version is 2.3.24 and now I am updating it to 2.5.12. I have downloaded all the required jar files of Struts and applied to project but I am getting the below error

ERROR StatusLogger 找不到 log4j2 配置文件.使用默认值配置:仅将错误记录到控制台.设置系统属性'org.apache.logging.log4j.simplelog.StatusLogger.level' 到 TRACE 到显示 Log4j2 内部初始化日志记录.

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.

但我没有在我的项目中使用任何记录器.我已经添加了所有依赖 jar 文件,我没有使用 Maven,而是在 lib 文件夹中添加了相关库.请有任何建议.

But I am not using any logger in my project. I have added all the dependency jar files and I am not using Maven, but added related libraries in lib folder. Any suggestions please.

推荐答案

Struts 框架正在使用日志框架 log4j 第二版.

Struts framework is using a logging framework log4j second version.

请注意,该框架现在使用 Log4j2 作为主要的日志记录层,现有的旧日志记录层已被弃用,并将很快被删除.Log4j2 支持许多不同的日志记录实现,请查看文档了解更多详细信息.

Struts 2.3 to 2.5 migration

Please be aware that the framework is using Log4j2 now as a main logging layer, the existing old logging layer is deprecated and will be removed soon. Log4j2 supports many different logging implementations, please check documentations for more details.

log4j2.xml 文件是引导日志框架所必需的.但是类路径中缺少它.Struts 框架也缺少它.

The file log4j2.xml is required to bootstrap the logging framework. But it's missing from the classpath. It's also missing from the Struts framework.

您应该找到一些 log4j2.xml,即来自 struts-showcase 应用程序或阅读本教程中的第 4 步 如何创建一个 Struts 2 Web 应用程序

You should find some log4j2.xml, i.e. from the struts-showcase application or read a step 4 in this tutorial How To Create A Struts 2 Web Application

要了解幕后发生的事情,本教程的示例应用程序使用 log4j2.您需要将 log4j2 的依赖节点添加到 pom:

Step 4 - Add Logging

To see what’s happening under the hood, the example application for this tutorial uses log4j2. You’ll need to add a dependency node for log4j2 to the pom:

pom.xml log4j 依赖节点

pom.xml log4j dependency node

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.8.2</version>
</dependency>

同时使用 log4j-core 和 log4j-api 允许使用最新版本的 Log4j2 而不会与框架提供的版本冲突.在 src/main/resources 文件夹中设置 log4j2.xml 配置,其中包含以下内容

Using both log4j-core and log4j-api allows to use the latest version of Log4j2 without a clash with version provided by the framework. Setup a log4j2.xml configuration in the src/main/resources folder which contains the following

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.opensymphony.xwork2" level="debug"/>
        <Logger name="org.apache.struts2" level="debug"/>
        <Root level="warn">
            <AppenderRef ref="STDOUT"/>
        </Root>
    </Loggers>
</Configuration>

注意上面的log4j2配置将控制台指定为日志目标.

Note the above log4j2 configuration specifies the console as the log target.

相关文章