Chinaunix首页 | 论坛 | 博客
  • 博客访问: 408771
  • 博文数量: 114
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1395
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-05 17:54
文章分类

全部博文(114)

文章存档

2011年(2)

2009年(1)

2008年(111)

我的朋友

分类: Java

2008-08-19 19:50:30

1:首先在工程中导入log4j-1.3alpha-3和log4j-chainsaw-1.3alpha-3两个包
2:在任何需要使用log4j的地方,类代码中必须描述!
     private static Logger log = Logger.getLogger(Tes5t.class);
  
   如:
 

package com.viita;

import org.apache.log4j.*;
import org.w3c.dom.Text;

public class Logtest {
    
    private static Logger log = Logger.getLogger(Tex5t.class);
    public void dox() {
        // TODO Auto-generated method stub

        //BasicConfigurator.configure();

        log.debug("这是调试信息");
        log.error("这是错误信息");
        log.fatal("这是致命错误");
        log.info("这是显示信息");
        log.warn("这是警告");
        
    }
    
    public static void main(String[] args) {
        Logtest lg = new Logtest();
        lg.dox();
        
    }
    
    

}

 

3:必须对log4j进行配置,我们采用两种方法进行配置:xml和资源文件!前提是:两者的名字是固定的,log4j.xml和log4j.properties  都要放置在工程的classes根目录下!

先看xml的配置代码(文件保存形式):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="">
        
  <appender name="FileAppender" class="org.apache.log4j.FileAppender">
  <param name="File" value="c:/1.txt"/>
  <param name="Append" value="true"/>
    <layout class="org.apache.log4j.TTCCLayout">
        <param name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/>
    </layout>
  </appender>
  <root>
    <priority value ="error" />
    <appender-ref ref="FileAppender"/>
  </root>
</log4j:configuration>

 

再看xml的控制台输出形式:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="">
        
  <appender name="FileAppender" class="org.apache.log4j.ConsoleAppender">
 
    <layout class="org.apache.log4j.TTCCLayout">
        <param name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/>
    </layout>
  </appender>
  <root>
    <priority value ="error" />
    <appender-ref ref="FileAppender"/>
  </root>
</log4j:configuration>

 

其实在现在的项目中,用得最多的是资源文件的配置!

log4j.rootLogger=DEBUG,FILE #这块进行显示优先级别和输出位置的更改 #DEBUG,CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE log4j.addivity.org.apache=true ################### # Console Appender ################### log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=DEBUG log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.TTCCLayout log4j.appender.CONSOLE.layout.dateFormat=yyyy-MM-dd HH:mm:ss #log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n ##################### # File Appender ##################### log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=file.log log4j.appender.FILE.Append=false log4j.appender.FILE.layout=org.apache.log4j.TTCCLayout log4j.appender.CONSOLE.layout.dateFormat=yyyy-MM-dd HH:mm:ss #log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n # Use this layout for LogFactor 5 analysis ######################## # Rolling File ######################## log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender log4j.appender.ROLLING_FILE.Threshold=ERROR log4j.appender.ROLLING_FILE.File=rolling.log log4j.appender.ROLLING_FILE.Append=true log4j.appender.ROLLING_FILE.MaxFileSize=10KB log4j.appender.ROLLING_FILE.MaxBackupIndex=1 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n #################### # Socket Appender #################### log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender log4j.appender.SOCKET.RemoteHost=localhost log4j.appender.SOCKET.Port=5001 log4j.appender.SOCKET.LocationInfo=true # Set up for Log Facter 5 log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n ######################## # Log Factor 5 Appender ######################## log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000 ######################## # SMTP Appender ####################### log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender log4j.appender.MAIL.Threshold=FATAL log4j.appender.MAIL.BufferSize=10 log4j.appender.MAIL.From=chenyl@hollycrm.com log4j.appender.MAIL.SMTPHost=mail.hollycrm.com log4j.appender.MAIL.Subject=Log4J Message log4j.appender.MAIL.To=chenyl@hollycrm.com log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n ######################## # JDBC Appender ####################### log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver log4j.appender.DATABASE.user=root log4j.appender.DATABASE.password= log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n') log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File=SampleMessages.log4j log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j' log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout ################### #DIY Appender ################### log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender log4j.appender.im.host = mail.cybercorlin.net log4j.appender.im.username = username log4j.appender.im.password = password log4j.appender.im.recipient = corlin@cybercorlin.net log4j.appender.im.layout=org.apache.log4j.PatternLayout log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n

 

这样我们就可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码更加细致地控制日志的生成过程

阅读(602) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~