Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23482
  • 博文数量: 2
  • 博客积分: 1468
  • 博客等级: 上尉
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-28 11:33
文章分类
文章存档

2010年(1)

2008年(1)

我的朋友
最近访客

分类: Java

2008-08-15 10:17:09

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * Logs messages into File and DB.
 *


 * Debug < Info < Warn < Error
 *
 *
 */
public class LoggerManager {


    public static final String PROFILE = "Log4jFile.properties";

    /**
     * Holds singleton instance
     */

    private static LoggerManager impl;
    static {
        impl = new LoggerManager();
    }

    private static Logger log4j;

    /**
     * prevents instantiation
     */

    private LoggerManager() {
        log4j = LogManager.getLogger(LoggerManager.class);
        try {
            Properties pro = new Properties();
            InputStream is = getClass().getResourceAsStream(PROFILE);
            pro.load(is);
            PropertyConfigurator.configure(pro);
        } catch (IOException e) {
            BasicConfigurator.configure();
            log4j.info("should change properties file...");
            log4j.error(e.toString());
        }
    }

    /**
     * Singleton Pattern -- use getLog() instead
     */

    private static LoggerManager getInstance() {
        return impl;
    }

    /**
     * Logging: Debug < Info < Warn < Error.
     *


     * Singleton Pattern

     * Usage:
     *
     *


     * static final Logger log = LoggerManager.getLog();
     *
     * log.debug("debug, note: ...");
     * log.info("info msg");
     * log.warn("Warning message");
     * log.error("Error, dead");
     *

     *
     * @return log4j For logging call debug(msg), info(msg), warn(msg), and
     * error(msg)
     */
    public static Logger getLog() {
        return log4j;
    }

    public static boolean shutdownDBLog() {

        return false;
    }
    
    /**
     * Replace single quotes (') with double quotes (").
     * @param queryString
     * @return queryString w/o single quotes (')
     */

    public static String unquote(String queryString) {
        return queryString.replace('\'', '"');
    }

}

附:log4jFile.properties


log4j.addivity.org.apache=true
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %l%n%p\: %m%n%n
log4j.appender.Chainsaw=org.apache.log4j.net.SocketAppender
log4j.appender.Chainsaw.layout=org.apache.log4j.PatternLayout
log4j.appender.Chainsaw.layout.ConversionPattern=%d{DATE} [%-5p] %c {%F\:%L} - %m%n
log4j.appender.Chainsaw.locationInfo=true
log4j.appender.Chainsaw.port=4445
log4j.appender.Chainsaw.remoteHost=127.0.0.1
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.Append=true
log4j.appender.FILE.DatePattern=yyyyMMdd'.log'
log4j.appender.FILE.File=logfile
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %l%n%p\: %m%n
log4j.rootLogger=ERROR, CONSOLE,FILE


阅读(1605) | 评论(0) | 转发(0) |
0

上一篇:让自己老婆成为世界上最幸福的女人

下一篇:没有了

给主人留下些什么吧!~~