问题
I have a spring 2.5 webapp with the following web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Spring BlazeDS Integration Samples</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>ServerBlaze</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*-context.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<servlet>
<servlet-name>serverBlaze</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>serverBlaze</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
and I declared this bean
<bean id="mylog"
class="org.springframework.beans.factory.config.CommonsLogFactoryBean">
<property name="logName" value="mylog" />
</bean>
inside services-context.xml (it's a blazeds/spring project).
I inject it this way into UserDAO bean :
<bean id="user" class="com.acotel.msp.database.UserDAO" >
<property name="mylog" ref="mylog" />
<property name="jsonClient" ref="jsonClient" />
</bean>
This is log4j.xml config file :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}\\logs\\serverBlaze.log" />
<param name="datePattern" value="'.'yyyy-MM" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
...cut...
</layout>
</appender>
<appender name="ROOT" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}\\logs\\serverBlazeRoot.log" />
<param name="datePattern" value="'.'yyyy-MM" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n" />
</layout>
</appender>
<logger name="com.bla.database">
<level value="info" />
<appender-ref ref="FILE" />
</logger>
<root>
<priority value="info" />
<appender-ref ref="ROOT" />
</root>
</log4j:configuration>
In my class I have this :
package com.bla.database;
import java.util.ArrayList;
import json.Client;
import org.apache.commons.logging.Log;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.bla.MessageReceiver;
import com.bla.PropertiesManager;
import com.bla.interfaces.Users;
import com.bla.vo.User;
public class UserDAO {
private Log mylog;
private Client jsonClient;
public User getUser(String username, String password) {
User result = null;
try {
Users users = jsonClient.openProxy("userDAO", Users.class);
result = users.getUser(username, password);
mylog.info("Esito invio bean ["+result+"]");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return result;
}}
I hoped that logging in class UserDAO would log to the FILE appender, but It does not. The file is created but is empty. ROOT appender works correctly. What am I doing wrong?
回答1:
In your services-context.xml file, try setting the "logName" property of the "myLog" bean to "com.bla.database". This should match up with the name of the logger defined in your log4j.xml configuration file.
回答2:
Another approach would be to treat logging as a cross cutting concern and do it with aspects. You can apply standard Spring interceptors declaratively.
来源:https://stackoverflow.com/questions/1509757/injecting-log4j-loggers-with-spring