简体   繁体   中英

How to turn off wildfly server logs and enable log4j2

I have made a web application using jakarta and wildfly server. My problem is that i have put loggers in some cases and when i call these methods i can't see them in a file. For example i have this method:

@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;
    }

In which you can see i have 2 loggers. When i make a call with postman to change the email, the email will change but i cant see a log to inform me. I have the correct imports:

import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;

and the correct dependencies:

  <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>

Also i made a log4j2.xml file:

<?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>

and i made also a jboss-logging.properties to turn off the logs from wildfly server:

# Root logger option logger.level=OFF

The only logs i can see in "mylogfile.log" is when i build the project, i have some methods in which i read some properties from csv files. These logs are visible in my file:

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

One method from these is this:

 @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;
    }

My question is how i can save every log i have in this file? For example if i make a http request when the application is running to update the email as the method above, how i can save this log which has the level of INFO or ERROR in the file?

I have created also in META-INF a xml which is called jboss-deployment-structure.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>

i tried to disable wildflyserver i said with jboss-logging.properties and i added the jboss-deployment-structure.xml which does not work`

You also need to exclude the org.apache.logging.log4j.api or simply just exclude the logging subsystem.

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

This would skip all the logging dependencies from being added to your deployment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM