log4j配置和使用

log4j的配置如下:

#配置日志的等级,日志有好多级别,本文等级为DEBUG,输出地址为CONSOLE。
log4j.rootLogger=DEBUG,CONSOLE,A
#配置是否在屏幕中显示,默认为true,
log4j.addivity.org.apache=false
#配置在控制台输出
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
#设置等级为debug
log4j.appender.CONSOLE.Threshold=DEBUG
#设置日期的输出格式
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p  %x - %m%n
#日志输出到系统标准输出设备上,默认就是System.out
log4j.appender.CONSOLE.Target=System.out
#设置编码为UTF-8
log4j.appender.CONSOLE.Encoding=UTF-8
#配置布局格式
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout


log4j.appender.A=org.apache.log4j.DailyRollingFileAppender  
log4j.appender.A.File=${catalina.home}/logs/FH_log/PurePro_
log4j.appender.A.DatePattern=yyyy-MM-dd'.log'
log4j.appender.A.layout=org.apache.log4j.PatternLayout  
log4j.appender.A.layout.ConversionPattern=[FH_sys]  %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}\:%L \: %m%n

构建日志类:

package com.winning.dm.pathway;

public class Logger {

    private org.apache.log4j.Logger logger;

    /**
     * 构造方法,初始化Log4j的日志对象
     */
    private Logger(org.apache.log4j.Logger log4jLogger) {
        logger = log4jLogger;
    }

    /**
     * 获取构造器,根据类初始化Logger对象
     * 
     * @param Class
     *            Class对象
     * @return Logger对象
     */
    public static Logger getLogger(Class classObject) {
        return new Logger(org.apache.log4j.Logger.getLogger(classObject));
    }

    /**
     * 获取构造器,根据类名初始化Logger对象
     * 
     * @param String
     *            类名字符串
     * @return Logger对象
     */
    public static Logger getLogger(String loggerName) {
        return new Logger(org.apache.log4j.Logger.getLogger(loggerName));
    }

    public void debug(Object object) {
        logger.debug(object);
    }

    public void debug(Object object, Throwable e) {
        logger.debug(object, e);
    }

    public void info(Object object) {
        logger.info(object);
    }

    public void info(Object object, Throwable e) {
        logger.info(object, e);
    }

    public void warn(Object object) {
        logger.warn(object);
    }

    public void warn(Object object, Throwable e) {
        logger.warn(object, e);
    }

    public void error(Object object) {
        logger.error(object);
    }

    public void error(Object object, Throwable e) {
        logger.error(object, e);
    }

    public void fatal(Object object) {
        logger.fatal(object);
    }

    public String getName() {
        return logger.getName();
    }

    public org.apache.log4j.Logger getLog4jLogger() {
        return logger;
    }

    public boolean equals(Logger newLogger) {
        return logger.equals(newLogger.getLog4jLogger());
    }
}

测试使用日志:

package loglearning;

public class LogTest {

    private static Logger logger = Logger.getLogger(LogTest.class);

    public void addLogger() {
        logger.debug("Hello world");
    }

    public static void main(String[] args) {
        System.out.println("first log learning");
        logger.debug("hello world");
    }
}

实验结果如下:

first log learning
2016-12-06 09:33:34 -0    [main] DEBUG   - hello world
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页