Announcement Announcement Module
No announcement yet.
Logging using Spring Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Logging using Spring


    Am trying to use before, after, and after throws advisors to log my application.
    I have found some tutorials but they either foxed on explaining AOP or wasn't very useful.

    can any one help find a good solution as am not able to find any good tutorial...

    Thanks in advance

  • #2
    I am posting detail steps that you need to follow in order to enable logging in your project using spring's AOP feature. I have used Log4J. The steps are not very fine tuned or written perfectly but they should give you a good idea of how to build logging framework for your project.NJOY.

    Steps followed to enable Logging using log4j:
    1)Downloaded log4j-1.2.15.jar file from apache.
    2)Included the jar file in the project’s classpath.
    3)Kept log4j.dtd under “Your_Web_Project/Java Resources” folder(You can keep it somewhere else also. Doesnt really matter as long as your app is able to figure it out.)
    4)Made a log4j.xml file in the same folder.
    5)Made logging-service.xml Spring bean’s file that contains various advices/advisor beans for our application. For more detail on advices/advisors please refer Spring AOP. This step is detailed later on in the same posting.
    6)Made various Java classes that contains logic describing what to do when logging framework is invoked.(I have not posted the Java classes as it would be good for you to try it out on your own. Still if you find any problem, you can reply back and i can post the java classes.)

    Lets look at the important steps in a little more detail. The first 3 steps are implicit and requires no discussion. So lets start with the next step:

    Creating a log4j.xml file:
    A log4j.xml configuration file contains <log4j:configuration> as the root tab. Within this root tab we can define several appender tags. Appenders are responsible for controlling the output of the Log operations.
    Let us see a sample appender pulled out from the main log4j.xml file

    <appender name="error-file" class="org.apache.log4j.RollingFileAppender">
    <param name="file" value="Projecterror.log" />
    <param name="append" value="true" />
    <param name="maxFileSize" value="2MB" />
    <param name="maxBackupIndex" value="15" />
    <param name="threshold" value="ERROR" />
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern"
    value="%d [%t] %p - %m%n" />

    The appender name is error-file and it uses a RollingFileAppender class of the log4j utility. Note that we have used various param names to define our custom param values for having control over the logging framework.
    We have used the RollingFileAppender class so that files are backed up when they reach the limit defined by the param “maxFileSize”.Note that we also control how many backup files can be created by using the MaxBackupIndex parameter.
    Param name file tells us the name of the log file.
    Param name append tells us that keep appending the log data in this file instead of overwriting it.
    Param name threshold defines what type of log statements should be looged. In this case only ERRORs and FATAL are logged.
    Log4j has a basic rule defined which is the core of everything.
    It assumes that levels are ordered. For the standard levels, we have
    So if you set the threshold to INFO, all logging for INFO, WARN, ERROR and FATAL will take place, but any logging request for a DEBUG will not be logged into the log file. See log4j documentation for more details.

    We have also defined a layout with a PatternLayout class that specifies a conversion pattern. The conversion pattern takes a value specified as a regular expression. Here we have specified how our log statement should be formatted for better reading.
    %d specifies Time and date
    %t specifies which thread
    %p specifies what is the logging level
    %m%n specifies the actual message.

    So a log statement in our log file will look somewhat like this:

    2008-06-25 15:48:00,982 [main] DEBUG - Loading schema mappings from [META-INF/spring.schemas]

    We can also define loggers in our log4j.xml file.
    A log4j.xml file should have a root tag which denotes the root logger.
    A logger looks like this:
    <logger name="TESTLOG">
    <level value="INFO" />
    <appender-ref ref="trace-file" />
    <appender-ref ref="error-file" />

    Here the name of the logger is TESTLOG(note names are case-sensitive).This logger will log all logging request with a log level of INFO or above. And it will write the log statements using the appenders defined as appender-ref tags.
    We are through with our Log4J stuff for the time being. We will now concentrate on how to create various Advices and advisors using Spring’s AOP framework. Understanding of this is of utmost importance in order to understand the complete framework and also to understand how Spring’s AOP framework works in practical.

    The hierarchy is as follows:
    We define ADVISOR beans. ADVISOR beans contain POINTCUT and ADVICE. ADVICE is the actual implementation of our aspect. Thus it contains logic of what to do when somebody calls for this ADVICE using the ADVISOR. POINTCUT defines at what points advice should be applied.
    Lets see an example straight from our logging-service.xml file:

    <bean id="entryLoggerAdvisor"
    class=" dPointcutAdvisor">
    <property name="advice">
    <ref local="theBeforeLoggerAdvice"/>
    <property name="patterns">

    Here we have defined an ADVISOR – entryLoggerAdvisor.
    Its class is RegexpMethodPointcutAdvisor. This class tell us that we can apply ADVICE to the methods of the class that is using this ADVISOR, using regular expressions.
    This ADVISOR also contains a property named “advice” that refers to a bean “theBeforeLoggerAdvice”. We will look at it a little later.
    Another property that it contains is “patterns”. This property has a value of .* It is a regular expression, where . means “Matches any single character” and * means “Matching the preceding character zero or more times.” In simple terms it will apply the advice to all the methods of the class that is calling this ADVISOR.
    Now coming back to the advice property, lets look how “theBeforeLoggerAdvice” bean looks like.
    <bean id="theBeforeLoggerAdvice" class="org.oclcpica.infra.framework.spring.aspects .MethodEntry">
    <ref bean="loggingService"/>

    This bean refers to class MethodEntry which implements the Spring specific MethodBeforeAdvice interface. Thus it tells the Spring container that this advice should be applied at the beginning of all the methods of the class calling the advice through the advisor. Phew!! Long sentence. But that sumarises almost every thing for us.
    This bean also takes loggingService bean as a constructor-arg. MethodEntry class internally calls a method on LoggingService to carry out the logging tasks. But that’s our choice. We are not forced to do this.

    So in essence, you have created an ADVISOR, an ADVICE and a logging bean that will help the framework to carry out that advice. Here loggingService is a java class that defines all the logging methods like debug,info etc.

    Hope this helps you. If you still have any questions, please revert back.


    • #3
      Thanx for the help I managed finally to do it


      • #4
        You are welcome