Log4J configuration not displaying Spring Transact

2019-05-21 03:11发布

问题:

Following is my log4j.properties:

log4j.rootLogger=ALL, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %p [%c] - %m%n


log4j.logger.org.springframework=ALL
log4j.logger.app.dev.ems=ALL
log4j.logger.org.springframework.transaction=ALL

I have mentioned the log4jConfigLocation in the web.xml:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/resources/log4j.properties</param-value>
</context-param>

And also defined its listener class:

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

But I am unable to see the Spring Transaction log in console. Also from a class: app.dev.ems.web.wicket.page.home.HomePage when I am doing:

Logger logger = LoggerFactory.getLogger(getClass());

public HomePage() {     
    logger.debug("<<<<<<<<<<<<<<<<<<<JYM>>>>>>>>>>>>>>>>>>");//if logger.info is used then it is showing.       
}

This is also not displaying that log. I am in Wicket Application and I have set:

<init-param>
    <param-name>configuration</param-name>
    <param-value>DEVELOPMENT</param-value>
</init-param>

While defining the WicketServlet.

I am unable to find the problem. Any information will be very helpful to me.

NB: If you want me to post the applicationContext.xml I can do that. Th Transaction is Annotation driven.


I have log4j-1.2.14, slf4j-api-1.6.1, slf4j-log4j12-1.4.2 added. And I am using JBoss AS-7.1.0-Final.

In other SO thread I saw that this is problem with JBoss, and as per the instruction given there I have added jboss-deployment-structure.xml in WEB-INF(also tried with META-INF):

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

And defined a bean in applicationContext.xml which will load the log4j.properties:

<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
        <list>
            <value>classpath:/app/dev/ems/web/log4j.properties</value>
        </list>
    </property>
</bean>

But unfortunately it didn't worked.

回答1:

Using the jboss-deployment-structure.xml exclusion should be working. If it's not, it could be the location of your configuration file. I'm not familiar with Spring so maybe adding the <context-param/> is a typical way to specify the location of the log configuration.

You might want to try to move your log4j.properties to the to the WEB-INF/classes or META-INF directory of your archive. Also you make sure you're including the log4j library in your deployment.

FWIW the jboss-deployment-structure.xml will no longer be needed for logging configurations. It's not in a release yet, but it is in the upstream source.

Since it seems you're looking for a way to turn on debugging you need to do two things. You need to set a handler to allow debug messages to come through. You also need to setup a logger that will allow at least debug if not all messages to come through.

For example in your case you would create a logger like so.

<logger category="app.dev.ems.web.wicket" />

That will create a logger that will log at all levels. Now create a handler, or change a previously defined handler to accept debug messages.

Another example would be creating a file handler for your specific logger that will print your log message into a file.

<file-handler name="myFileHandler>
    <level name="DEBUG"/>
    <file relative-to="jboss.server.log.dir" path="my-log.log"/>
</file-handler>

Now create your logger and assign the handler to it.

<logger category="app.dev.ems.web.wicket">
    <handlers>
        <handler name="myFileHandler/>
    </handlers>
<logger>

Now all log messages passed through a logger that has a category of app.dev.ems.web.wicket.* will print to that file in addition to the handlers attached to the root-logger. If you don't want to see these in the default server.log or on the console you can add the use-parent-handler="false" attribute to the logger.



回答2:

I get problem also to use log4j with Spring and Jboss AS7, EAP 6, but I fix it by excluding commons-logging from spring-context.jar and add other jars. Also I excluded modules from jboss-deployment-structure.xml. It wasn't at all evident to fix it and to know the problem.

Here is what I did:

http://mariemjabloun.blogspot.com/2014/05/resolved-logging-in-jboos-eap-6-spring.html



回答3:

I have found a solution. My aim was to see the transaction logs. And now I can see that. What I did, I have modified the standalone.xml file resides in standalone/configuration. I changed some parts of the Logging subsystem:

The console-handler:

<console-handler name="CONSOLE">
   <level name="INFO"/>
   <formatter>
       <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
   </formatter>
 </console-handler>

changed to

<console-handler name="CONSOLE">
   <level name="DEBUG"/>
   <formatter>
       <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
   </formatter>
 </console-handler>

And the root-logger

<root-logger>
   <level name="INFO"/>
   <handlers>
      <handler name="CONSOLE"/>
      <handler name="FILE"/>
   </handlers>
</root-logger>

changed to

<root-logger>
   <level name="DEBUG"/>
   <handlers>
      <handler name="CONSOLE"/>
      <handler name="FILE"/>
   </handlers>
</root-logger>

And that works. Now I can see the DEBUG log in console. :)