2

我有一个带有以下 web.xml 的 spring 2.5 webapp

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

我宣布了这个豆子

    <bean id="mylog"
    class="org.springframework.beans.factory.config.CommonsLogFactoryBean">
    <property name="logName" value="mylog" />
</bean>

在 services-context.xml 中(这是一个 blazeds/spring 项目)。

我以这种方式将它注入到 UserDAO bean 中:

    <bean id="user" class="com.acotel.msp.database.UserDAO" >
    <property name="mylog" ref="mylog" />
    <property name="jsonClient" ref="jsonClient" />
</bean>

这是 log4j.xml 配置文件:

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

在我的课堂上,我有这个:

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

我希望登录类 UserDAO 会登录到 FILE appender,但它没有。该文件已创建但为空。ROOT appender 工作正常。我究竟做错了什么?

4

2 回答 2

5

另一种方法是将日志记录视为一个横切关注点并使用方面进行。您可以以声明方式应用标准 Spring 拦截器。

于 2009-10-03T14:33:01.307 回答
3

在您的 services-context.xml 文件中,尝试将“myLog”bean 的“logName”属性设置为“com.bla.database”。这应该与 log4j.xml 配置文件中定义的记录器的名称相匹配。

于 2009-10-02T18:33:00.673 回答