Java 记录日志

日志程序

直接调用logWriter方法即可。参考链接:http://blog.csdn.net/luoweifu/article/details/46495045

package com.newtd.log;

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

/**
 * Created by lee on 2017/3/17.
 */
public class LogWriter {
    static String strClassName = LogWriter.class.getName();
    static Logger logger = Logger.getLogger(strClassName);
    static LogManager logManager = LogManager.getLogManager();

    static {
        InputStream inputStream = null;
        try {
            //读取配制文件
            inputStream = LogWriter.class.getClassLoader().getResourceAsStream("log.properties");
            logManager.readConfiguration(inputStream);
            //添加Logger
            logManager.addLogger(logger);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void logWriter(String message) {
        logger.info(message);
    }
}

配置文件

# "handlers" specifies a comma separated list of log Handler
#handlers= java.util.logging.ConsoleHandler
handlers= java.util.logging.FileHandler

# Default logging level.
.level= CONFIG

# default file output is in "E:\Test" directory.
java.util.logging.FileHandler.pattern = ./odps_to_jedis.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

# Limit the message that are printed on the console to CONFIG and above.
java.util.logging.ConsoleHandler.level = CONFIG
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


# Facility specific properties.Provides extra control for each logger.
# For example, set the com.xyz.foo logger to only log SEVERE messages:
com.xyz.foo.level = SEVERE

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部