java 如何关闭wildfly服务器日志并启用log4j2

kyxcudwk  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(170)

我做了一个使用jakarta和wildfly服务器的web应用程序。我的问题是我在某些情况下放置了记录器,当我调用这些方法时,我无法在文件中看到它们。例如,我有这个方法:

@Override
    public PropertyOwnerDto updateEmail(int id, String email) {
        try {
            PropertyOwner propertyOwner = propertyOwnerRepository.read(id);
            propertyOwner.setEmail(email);
            propertyOwnerRepository.create(propertyOwner);
            logger.info("Changing e-mail of owner with id: " + id + "to :" + email);
            return new PropertyOwnerDto(propertyOwner);
        } catch (Exception e) {
            logger.error("Not found");
        }
        return null;
    }

在其中你可以看到我有2个记录器。当我打电话给 Postman 更改电子邮件,电子邮件将更改,但我看不到日志通知我。我有正确的导入:

  • 导入日志管理器;导入日志记录器;*

以及正确的依赖关系:

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

我也做了一个log4j2.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <File name="MyFile" fileName="logs/mylogfile.log">
      <PatternLayout pattern="%d %p %C{1.} [%t] %m%n"/>
    </File>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="MyFile"/>
    </Root>
  </Loggers>
</Configuration>

我也做了一个jboss-logging.properties来关闭wildfly服务器的日志:
# Root logger option logger.level=OFF
在“mylogfile.log”中我能看到的唯一日志是当我构建项目时,我有一些方法从csv文件中读取一些属性。这些日志在我的文件中可见:

2023-01-25 14:43:14,605 INFO c.m.w.s.i.IoServiceImpl [main] The csv file property.csv has been read 
2023-01-25 14:43:14,610 INFO c.m.w.s.i.IoServiceImpl [main] The csv file owners.csv has been read 
2023-01-25 14:43:14,611 INFO c.m.w.s.i.IoServiceImpl [main] The csv file repairs.csv has been read

其中一种方法是:

@Override
    public List<String[]> readCsvFile(String fileName) {
        List<String[]> list = new ArrayList<>();
        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(fileName));
            String str;
            while ((str = br.readLine()) != null) {
                list.add(str.split(","));
            }
            logger.info("The csv file {} has been read ", fileName);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return null;
        }
        return list;
    }

我的问题是我如何保存我在这个文件中的每一个日志?例如,如果我在应用程序运行时发出http请求,以更新电子邮件作为上述方法,我如何保存这个日志,其中有信息或错误的文件中的水平?
我还在META-INF中创建了一个名为jboss-deployment-structure.xml的xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="org.apache.commons.logging" />
            <module name="org.apache.log4j" />
            <module name="org.jboss.logging" />
            <module name="org.jboss.logging.jul-to-slf4j-stub" />
            <module name="org.jboss.logmanager" />
            <module name="org.jboss.logmanager.log4j" />
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

我试图禁用wildflyserver我说与jboss-logging.properties和我添加的jboss-deployment-structure.xml不工作'

y4ekin9u

y4ekin9u1#

您还需要排除org.apache.logging.log4j.api,或者只是排除logging子系统。

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

这将跳过所有日志依赖项,而不添加到部署中。

相关问题