Introducing Log4J loggers with Spring

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 have declared this bean

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

      

inside services-context.xml (this is the blazeds / spring project).

I inject it this way into the UserDAO bean:

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

      

This is the 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 was hoping that the registration in the UserDAO class would write to the FILE application, but it doesn't. File created but empty. ROOT appender is working correctly. What am I doing wrong?

+2


source to share


2 answers


In the services-context.xml file, try setting the "logName" property of the "myLog" bean to "com.bla.database". This must match the logger name defined in the log4j.xml configuration file.



+3


source


Another approach would be to look at registration as a cross-cutting concern and implement it in an aspect. You can use the standard Spring interceptors declaratively.



+5


source







All Articles