j2ee log4j

1.下载log4j的jar包,拷贝到工程中。

2.编写log4j.properties或者log4j.xml。

3.在web.xml中添加

  <!-- Log4j 配置 -->
  <context-param> 
        <param-name>log4jConfigLocation</param-name> 
        <param-value>/WEB-INF/log4j.xml</param-value> 
    </context-param> 
    <context-param> 
        <param-name>log4jRefreshInterval</param-name> 
        <param-value>1800000</param-value> 
    </context-param> 
    <listener> 
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 

4.使用log4j.properties-----将web.xml中的log4j.xml替换为log4j.properties即可。

一般会将log4j.properties放在src目录下,那么在web.xml中需要这么写:<param-value>classpath:log4j.properties</param-value>

#--------------------------------
#
log4j.rootLogger=DEBUG, stdout, fileout
#log4j.logger.test=info
#log4j.logger.org.apache.jasper = DEBUG
#log4j.logger.org.apache.catalina.startup.TldConfig = DEBUG
#log4j.logger.org.apache.catalina.session.ManagerBase = DEBUG

log4j.logger.com.fiscal = DEBUG
log4j.logger.com.system = DEBUG

log4j.logger.com.ibatis = DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner = DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG
log4j.logger.java.sql.Connection = DEBUG
log4j.logger.java.sql.Statement = DEBUG
log4j.logger.java.sql.PreparedStatement = DEBUG
log4j.logger.java.sql.ResultSet = DEBUG

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=C\:\\ibatis.log
log4j.appender.fileout.MaxFileSize=30KB

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} \:%m%n
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH:mm:ss} :%m%n

#log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

# log4j.logger.org=info
#--------------------------------

 

5.使用log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="Console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{yyyy/MM/dd-HH:mm:ss} >> %5p >> %t >> %l >> %m%n" />
        </layout>
    </appender>

 

    <appender name="myaction"
        class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="c:\\myaction.log" />
        <param name="MaxFileSize" value="10KB" />
        <param name="MaxBackupIndex" value="3" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d >> %5p >> %t >>  %l >> %m%n" />
        </layout>
    </appender>


    <logger name="com.struts.action">
    <level value="INFO" />
        <appender-ref ref="myaction" />
    </logger>
   
    <root>
        <priority value="info" />
        <appender-ref ref="Console" />
    </root>

</log4j:configuration>

 

6.使用logger

在类里添加 private static Logger logger = Logger.getLogger("com.struts.action"); 会使用myaction哪个记录器。

 

getLogger里面也可以用类名等,默认或者找不到对应名称的logger都会使用rootlogger

你可能感兴趣的:(log4j)