0
votes

I'm trying to change from log4j1 to log4j2. What I did:

  • deleted old configuration file log4j.xml
  • created new config file log4j2.xml
  • created jboss-deployment-structure.xml
  • edited pom

Environment: Wildfly 10 1 EAR 4 WAR The web.xml isn't being used in the file. The previous log4j config log was in the EAR (only)

Log4j2 config file

 <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="MyFile" fileName="all.log" immediateFlush="false" append="false">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>

Jboss deployment structure

    <?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <!--Use log4j.xml file instaead of using the loggin subsystem configuration -->
        <exclusions>
            <module name="org.apache.log4j" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

new maven dependencies

 <!-- Apache Log4j API -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>

        <!-- Apache Log4j SLF4J Binding -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.8.2</version>
        </dependency>

        <!-- Apache Log4j Core -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>

        </dependency>

Has I said earlier I was using log4j1 with SLF4j has facade. These are the changes I've made (I'm trying to start with a simple configuration file I've found online). The file is not even created, but the console log is working just fine.

Thanks,

3
Where are you expecting the file? That looks like a relative path, so maybe the application doesn't have the permissions to write there.tima
I'm running in the localhost. I don't think I'm expecting the file. How do you suggest I should do? Something like this in the fileName: ${jboss.server.log.dir}/log/all.log . ??Ricardo
You should try to change all.log to an absolute path where everyone has permissions like /Users/yourname/Desktop/all.log on Mac or C:\users\yourname\Desktop\all.log on Windows. I'm not 100% on the windows path being correct, but you can double check.tima
time I've just done your suggestion on my mac and still didn't work :(Ricardo

3 Answers

1
votes

I got it to work with a webapp in JBoss 7.1.1 using the below config.

/src/main/resources/log4j2.xml

... above unchanged ...
<File name="MyFile" fileName="/Users/tima/Desktop/all.log" immediateFlush="true" append="true">
... below unchanged ...

WEB-INF/jboss-deployment-structure.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">
    <deployment>
        <exclusions>
            <module name="org.apache.log4j" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

The pom file is the same as yours.

Test controller

@Controller
@RequestMapping("/test1")
public class HelloWorldController {

    private static final Logger logger = LogManager.getLogger(HelloWorldController.class);

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Map<String, Object>> printHello() {
        logger.info("entered printHello()");
        System.out.println("entered printHello() 2");

        Map<String, Object> resultMap = new HashMap<String, Object>();

        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }
}

Results

$: cat ~/Desktop/all.log
2017-05-11 15:13:35.918 [http--0.0.0.0-8080-2] INFO  com.tima.controller.HelloWorldController - entered printHello()

Also in the server.log I can see the following:

15:13:35,937 INFO  [stdout] (http--0.0.0.0-8080-2) entered printHello() 2

EDIT

If you want to use the org.slf4j.Logger logger, you have to add the lines below to the jboss-deployment-structure.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">
    <deployment>
        <exclusions>
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

EDIT 2

If you are using an EAR file, you can set up logging by using a common JAR that is shared across other modules / WARs in the EAR file.

In the common JAR have:

  1. The log4j-api, log4j-core, log4j-slf4j-impl as dependencies in the pom.xml.
  2. The log4j2.xml under src/main/resources

Have the common jar as a dependency in the WAR files. And you can remove the jboss-deployment-structure.xml file from all WAR files.

The jboss-deployment-structure.xml file will now be located in the EAR file under src/main/application/META-INF/, and contain:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">
    <sub-deployment name="war-1.war">
        <exclusions>
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
        </exclusions>
    </sub-deployment>
    <sub-deployment name="war-2.war">
        <exclusions>
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
        </exclusions>
    </sub-deployment>
    ...other wars here...
</jboss-deployment-structure>

The above configuration will print from all WAR files to the same file. But you can configure the log4j2.xml file to print to different files based on the package name. That approach is explained in this question

0
votes
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
    <Property name="basePath">C:\\logs</Property>
</Properties>

<Appenders>
    <RollingFile name="fileLogger" fileName="${basePath}/app-info.log" filePattern="${basePath}/app-info-%d{yyyy-MM-dd}.log">
        <PatternLayout>
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true" />
        </Policies>
    </RollingFile>

    <Console name="console" target="SYSTEM_OUT">
        <PatternLayout   pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
    </Console>
</Appenders>
<Loggers>
    <Logger name="com.howtodoinjava" level="debug" additivity="true">
        <appender-ref ref="fileLogger" level="debug" />
    </Logger>
    <Root level="debug" additivity="false">
        <appender-ref ref="console" />
    </Root>
</Loggers>

for morw you can follow this link : -

http://howtodoinjava.com/log4j2/log4j-2-xml-configuration-example/

0
votes

You'll probably fair better to exclude the logging subsystem in your jboss-deployment-structure.xml. I would have done this as a comment, but pasting the xml wouldn't have been pretty.

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