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

如何使Log4j2在Wildfly 8.2中工作

卞轶
2023-03-14

我正在通过log4j2(版本2.5)为部署到Wildfly 8.2服务器的ear应用程序(包括ejb和web应用程序)使用slf4j。

这就是父POM的外观:

<dependencyManagement>
    <dependencies>
        <!-- SLF4J -->
        <dependency> 
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-bom</artifactId>
            <version>${log4j2-version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        ...
    </dependencies>
</dependencyManagement>

模块的pom.xml是:

<dependencies>
    <dependency> 
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>
    ...
</dependencies>

然后我放置了log4j2。xml到ear的META-INF文件夹中

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">  
    <Appenders>  
       <File name="FileLogger" fileName="app-simple.log">  
          <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{3} - %m%n"/>  
       </File>  
    </Appenders>  
    <Loggers>  
       <Root level="DEBUG">  
          <AppenderRef ref="FileLogger"/>  
       </Root>  
    </Loggers>  
</Configuration>

在所有这些和应用程序部署之后,什么都没有发生,没有错误,也没有生成日志文件,

我认为Wildfly日志子系统可能会干扰它。所以我添加了jboss部署结构。META-INF文件夹中的xml,以排除Wildfly的日志记录

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="logging"/>
        </exclude-subsystems>
    </deployment>
</jboss-deployment-structure>

然后我得到了以下错误。

15:07:20,648 ERROR [stderr] (MSC service thread 1-5) ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

我确实发现这篇帖子讨论了相同的错误,根据答案,我认为log4j2版本可能有问题,所以我将版本降级到2.0.1,然后它没有抱怨配置文件,但出现了这个新错误:

16:57:38,456 ERROR [stderr] (MSC service thread 1-5) ERROR StatusLogger Could not search jar file 'C:\Servers\wildfly-8.2.0.Final\standalone\deployments\test-ear.ear\lib\log4j-core.jar\org\apache\logging\log4j\core' for classes matching criteria: annotated with @Plugin file not found java.io.FileNotFoundException: C:\Servers\wildfly-8.2.0.Final\standalone\deployments\test-ear.ear\lib\log4j-core.jar\org\apache\logging\log4j\core (The system cannot find the path specified)

同样根据这篇文章,Wildfly中的log4j2可能有问题,所以我试图调整log4j2的版本和配置,但运气不好。

那么如何在Wildfly8中设置log4j2,有人能帮忙吗。非常感谢。

共有1个答案

卢健
2023-03-14

从所有的文档中,我可以发现EAR的META-INF文件夹不在类路径中,所以Log4j 2不会在那里找到配置文件。我能找到的唯一文档提到必须创建一个模块,在其中可以放置公共配置文件

 类似资料:
  • 我试图使用appclient部署我的jar,但我得到了这个错误。 这是我的部署命令 从该错误中可以清楚地看到“java.lang.ClassNotFoundException:com.serverapi.ServiceManagerBeanRemote From[Module”deployment.socket-gateway.jar:main“From Service Module Loader

  • 使用log4j2,我想替换日志消息中的一些字符串(比如用bar替换foo)。在xml配置中,我可以使用以下配置,它可以正常工作。 但我不得不在项目中使用属性文件,而不是使用XML,所以我尝试了以下方法 我得到了这样的错误: 如何在属性文件和log4j2中表示replace,是否可以将xml配置中的所有内容转换为属性配置?我使用的是log4j-api-2.14.0。jar和log4j-core-2.

  • 我正在尝试在WebLogic 12.2.1下运行的Web服务中添加log4j日志记录,但不知何故日志记录不起作用。 这是我的WAR文件WEB-INF\classes中的log4j2.xml: 这是我的Web服务代码的片段: 在我的WAR文件的WEB-INF\lib\中,我有: 我可以使用客户端程序成功调用web服务。但我根本没有看到日志文件被创建。有什么问题? 提前感谢。

  • 如前一个StackOverflow问题所述,我正在学习如何使用JAX-WS(用于XML Web服务的Java API)。我在以前的可执行Java程序中使用过Log4j2,我想用它来记录web服务请求。在本JAX-WS教程中,如何将Log4j2添加到基本代码中?

  • 资源目录还包含。文件是特定于环境的JAR文件的一部分,已经放在类路径中。 日志:

  • 我一直在尝试新的Log4j2。从有关迁移的文档来看,XML模式/DTD规范似乎已被废除。 这似乎是倒退了一步。当然,应该可以将XML Schema或DTD与我的相关联,以帮助编写它并进行验证。我在留档中找不到任何有用的东西,也没有找到XML Schema或DTD本身。 那么:在Log4j2中,我应该如何将XML模式与Log4j2.XML相关联?