1

Not sure what I am missing, trying to implement Log4J logging in my JBoss Spring application as defined here: Hibernate logging in Spring application on JBoss 7.1

But it doesn't seem to be picking up the configurations defined in my webapp/WEB-INF/classes/log4j.properties

pom.xml:

     <properties>
         <org.slf4j-version>1.7.5</org.slf4j-version>
    </properties>
     <!-- Logging -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>

    <!-- Spring -->
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
         </dependency>

META-INF/jboss-deployment-structure.xml:

<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="org.apache.log4j" />
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
        </exclusions>
    </deployment>
</jboss-deployment-structure> 

WEB-INF/classes/log4j.properties:

log4j.rootLogger=INFO, A1, R

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.Target=System.out
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate=DEBUG
log4j.logger.com.bagel=INFO

TestController.java:

package com.bagel.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
public class TestController {

private final static Logger LOGGER = LoggerFactory.getLogger(TestController.class.getName());

  @RequestMapping("/test")
  public void test(){
    LOGGER.info("Test Logging");
  }

}

And I am definitely sure that my Controller method test is being invoked, however it does not log the required "Test Logging"

Community
  • 1
  • 1
Shivam Sinha
  • 4,924
  • 7
  • 43
  • 65
  • Is there a reason to use log4j if you're using slf4j? – James R. Perkins May 19 '14 at 15:37
  • Hi James SLF4J is simply an interface. It requires an underlying implementation at run time e.g: java.util.logging, logback, log4j etc. This allows to you switch you actual implementation if required. – Shivam Sinha May 23 '14 at 12:17
  • Yes, which is why I'm asking the question :) The server will handle the logging configuration for you so there is no reason to use an additional log manager like log4j. – James R. Perkins May 23 '14 at 19:52
  • Well the server logs weren't displaying anything even after adding the logger category to standalone.xml ..Hence I thought I would need to explicitly define the implementation. – Shivam Sinha May 24 '14 at 07:28
  • Not sure what that could be, but I have heard of the issue with Spring before. I have no idea what Spring does to configure logging, but I think it attempts to do something. – James R. Perkins May 27 '14 at 21:40

1 Answers1

0

In my case I have followed below steps and it worked for me:

  1. Configure log4j in JBOSS EAP7.x module like below screen. enter image description here

module.xml

<module name="log4j" xmlns="urn:jboss:module:1.5">
    <properties>
        <property name="jboss.api" value="unsupported"/>
    </properties>

    <resources>
        <resource-root path="log4j-1.2.17.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
        <module name="javax.servlet.api" optional="true"/>
    </dependencies>
</module>
  1. Place jboss-deployment-structure.xml file under WEB-INF

  1. Place the log4j.properties file under src-->main-->resources

  2. Build the project and deploy it again it should work like a charm!

Elias
  • 664
  • 2
  • 11
  • 23