实际开发者修改Logger配置的方法

默认情况下,JDK的LogManager会在JRE目录下的"lib/logging.properties"这个文件中读取配置。

在开发中往往需要自定义配置,因此可以通过两种方式来修改默认配置:

1、通过java命令行的方式修改

除此之外,LogManager还可以根据两个系统属性来允许用户控制日志的配置:

  • "java.util.logging.config.class"
  • "java.util.logging.config.file"

其中,class这个属性优先有效,如果设置,会忽略file这个属性。

 

Java中设置属性也有两种方法:

  • Preferences API
  • 启动的时候,命令行参数

关于Preferences API,请参考JDK Documentation。

命令行参数是指,启动的时候用 -D<name>=<value>的方式指定属性,具体到这里,我们就可以用

 

java -Djava.util.logging.config.file="abc.properties"

 

指定使用"abc.properties"这个文件作为配置文件


2、自定义子日志类,在代码里重新读取配置信息

package com.proj.log;

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
import java.util.logging.Logger;

/**
 * 日志{@link java.util.logging.Logger}获取器
 */
public class Logging {
    private static Logger logger = null;
    private Logging(){}
     
    public static Logger getLogger(){
        if (null == logger) {
            InputStream is  = Logging.class.getClass().getResourceAsStream("/logger.properties");
            try {
                LogManager.getLogManager().readConfiguration(is);
            } catch (Exception e) {
                logging.warning("input properties file is error.\n" + e.toString());
            }finally{
                try {
                    is.close();
                } catch (IOException e) {
                    logging.warning("close FileInputStream a case.\n" + e.toString());
                }
            }
             
            logger = Logger.getLogger("LOGGER");
        }
        return logger;
    }
     
    private static Logger logging = Logger.getLogger(Logging.class.getName());
}
默认在src目录下添加文件logger.properties:

handlers = java.util.logging.ConsoleHandler,java.util.logging.FileHandler
 
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level = INFO
 
java.util.logging.FileHandler.pattern = c:/my.log%g.log
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.limit = 104857600
java.util.logging.FileHandler.count = 3
java.util.logging.FileHandler.append = true
java.util.logging.FileHandler.level = INFO
 
LOGGER.level = FINEST

测试用例:

public class LoggerTest extends TestCase{
    @Test
    public void testLogger() throws Exception {
        Logger logger = Logging.getLogger();
        logger.finest("finest");
        logger.finer("finer");
        logger.fine("fine");
        logger.info("info");
        logger.config("config");
        logger.warning("warning");
        logger.severe("severe");
         
    }
}

修改日期格式:

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class LogTest
{
	public static void main(String[] args) throws IOException
	{
		Logger log = Logger.getLogger(LogTest.class.getName());
		log.setLevel(Level.INFO);
		
		FileHandler fileHandler = new FileHandler("E:/testlog%g.log", true);
		fileHandler.setLevel(Level.SEVERE);
		fileHandler.setFormatter(new Formatter()
								 {
									public String format(LogRecord record)
									{
								        SimpleDateFormat sd = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");  
								        String d = sd.format(new Date());  
										return d + record.getLevel() + ":" + record.getMessage() + "/n";
									}
								 });
		log.addHandler(fileHandler);
		try
		{
			throw new Exception("ldddddddd");
		}
		catch (Exception e)
		{
			StringWriter sw = new StringWriter();
			e.printStackTrace(new PrintWriter(sw));
			log.severe(sw.toString());
		}
		log.info("aaa");
	}
}
格式:2014-10-29 17:39:11


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0X码上链

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值