Commons-logging + Log4j

一、Commons-logging能帮我们做什么?

1、提供一个统一的日志接口,简单了操作,同时避免项目与某个日志实现系统紧密耦合

2、自动选择适当的日志实现系统

  a.classpath下查找commons-logging.properties

  b.classpath下查找Log4j的包

  c.使用JDK自身的日志实现类(注:JDK1.4以后才有日志实现类)

  d.使用commons-logging自身日志实现类SimpleLog

3、它甚至不需要配置

 

完整的Java类的代码:

package com.nadim.framework.monitor;



import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;



public class Run {

    private static final Log log = LogFactory.getLog(Run.class);

    public static void main(String[] args) {

        //输出4种不同级别的日志

        log.debug("111");

        log.info("222");

        log.warn("333");

        log.error("444");

    }

}

注:要正确地应用Log4j输出日志信息,需要添加log4j.properties文件。

 

单独使用Log4j的Java类的代码:

package com.nadim.framework.monitor;



import org.apache.log4j.Logger;

import org.apache.log4j.PropertyConfigurator;



public class Run {

    static Logger logger = Logger.getLogger(TestLog4j.class); // First step



    public static void main(String[] args) {

        PropertyConfigurator.configure("log4j.properties");

        

        logger.debug("Here is some DEBUG");

        logger.info("Here is some INFO");

        logger.warn("Here is some WARN");

        logger.error("Here is some ERROR");

        logger.fatal("Here is some FATAL");



    }

}

 

二、log4j.properties的一点介绍

  除去以#开头的注释以及空行,第一行有用的内容是:  log4j.rootLogger = DEBUG, CONSOLE,A1

  1、log4j.rootLogger是最最重要的一个属性了,它定义日志信息的“输出级别”和“输出目的地”。

  2、第一个逗号之前的是“输出级别”;后面为“输出目的地”。可以同时指定多个“输出目的地”,以逗号隔开。

    输出级别:DEBUG、INFO、WARN、ERROR、FATAL ,它们是由Log4j系统定义的。

    输出目的地是自己定义的: 

        # 应用于控制台
      
        log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
      
      log4j.appender.Threshold=DEBUG
      
      log4j.appender.CONSOLE.Target=System.out
      
      log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
      
      log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
      
      #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
      
      
      #应用于文件
      
      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.PatternLayout
      
      log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
      
      # Use this layout for LogFactor 5 analysis
      
      
      # 应用于文件回滚
      
      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
      
      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
      
      
      # 发送日志给邮件
      
      log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
      
      log4j.appender.MAIL.Threshold=FATAL
      
      log4j.appender.MAIL.BufferSize=10
      
      [email protected]
      
      log4j.appender.MAIL.SMTPHost=www.wusetu.com
      
      log4j.appender.MAIL.Subject=Log4J Message
      
      [email protected]
      
      log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
      
      log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
      
      
      # 用于数据库
      
      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
      
      
      #自定义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 = [email protected]
      
      log4j.appender.im.layout=org.apache.log4j.PatternLayout
      
      log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 

 

 

你可能感兴趣的:(commons)