gpt4 book ai didi

spring - 使用 Spring 注入(inject) Log4J 记录器

转载 作者:行者123 更新时间:2023-12-01 14:38:13 26 4
gpt4 key购买 nike

我有一个带有以下 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

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

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

我以这种方式将它注入(inject)到 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 工作正常。我做错了什么?

最佳答案

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

关于spring - 使用 Spring 注入(inject) Log4J 记录器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1509757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com