2

I have done some searches looking for information about how to do logging with the Spring Framework.

We currently have an application that has no logging in it except for system.out statements (very bad way).

What I would like to do, is add logging, but also want to be able to control the logging at run time, with say JMX.

We are using Rad 7.0 / WebSphere 6.1

I am interesting to find out what is the best way(s) to accomplish this (I figure there may be several).

Update: Thoughts on the following Spring AOP Logging Good ideal or not. This is in reference to a question posted here on logging: Conditional Logging. Does this improve things or just makes it more difficult in the area of logging?

4

3 回答 3

5

I would use Commons Logging and Log4j. This is not really a question for Spring, however the Springframework source does uses Commons Logging as well. If you create a log4j logger and appender in log4j, you can enable logging within the Springframework classes too. There are a few ways to control logging at runtime. The Log4j sandbox has a JSP, that you can drop into your webapp, that will allow you to control the log levels of all of the loggers within your application.

于 2008-09-23T02:26:37.557 回答
2

See the other answers for log4j. But also consider JAMon for application monitoring. It's very easy to add to a spring application, e.g.:

    <bean id="performanceMonitor" class="org.springframework.aop.interceptor.JamonPerformanceMonitorInterceptor">
        <property name="useDynamicLogger" value="false"/>
        <property name="trackAllInvocations" value="true"/>
    </bean>

   <bean id="txRequired" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
            <property name="transactionManager" ref="transactionManager"/>
            <property name="transactionAttributes" >
                <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props>
            </property>
        <property name="preInterceptors">
           <list>
              <ref bean="performanceMonitor"/>      
           </list>
        </property>
   </bean>
于 2008-09-23T08:13:05.680 回答
1

here's a sample file for configuring log4j for a console and file logger. if this file is on the classpath it will get read by log4j automatically. However, since you're inside an app server, there may be another preferred way of configuring logging. I remember inside JBoss there was an xml file you had to modify. Not sure about websphere configuration. But if you want to configure it for a simple test app, this will get you going.

# Set root logger level to WARN and appenders to A1 & F1.
log4j.rootLogger=WARN, A1, F1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# logging to console only INFO
log4j.appender.A1.Threshold=INFO
# F1 is a file appender
log4j.appender.F1=org.apache.log4j.RollingFileAppender

# Tell Spring to be quiet
log4j.logger.org.springframework=WARN
# debug logging for my classes
log4j.logger.com.yourcorp=DEBUG
log4j.logger.org.hibernate=INFO

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r : %d{HH:mm:ss,SSS} [%t] %-5p %c{1} %x - %m%n

log4j.appender.F1.File=./log/mylogfile.log
log4j.appender.F1.MaxFileSize=10MB
log4j.appender.F1.MaxBackupIndex=5
log4j.appender.F1.layout=org.apache.log4j.PatternLayout
log4j.appender.F1.layout.ConversionPattern=%-4r : %d{HH:mm:ss,SSS} [%t] %-5p %c{1} %x - %m%n
于 2008-09-23T04:14:40.747 回答