操作读取日志文件,
1、使用默认的日志文件,并验证默认级别
public void originalConfig()
{
Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
logger.setLevel(Level.FINE);//add level limit
logger.info("enter my info :");
try
{
int t =9/0;
}
catch(Exception e)
{
logger.info("devide is '0',which is prohibit!" + e);
logger.fine("This is a fine level message!");
}
}
2、构建自己的日志文件,并使用控制台打印(ConsoleHandler)
public void bypassConfig()
{
Logger mylogger = Logger.getLogger("com.lv.logger");
mylogger.setLevel(Level.FINE);
mylogger.setUseParentHandlers(false);
Handler handler = new ConsoleHandler();
handler.setLevel(Level.FINE);
mylogger.addHandler(handler); mylogger.info("my own logger and handler!");
mylogger.fine("this is a fine message!");
}
3、构建自己的日志文件,使用输出文件方式(FileHandler),指定路径
public void myFileHandler()
{
Logger mylogger = Logger.getLogger("huihui.logger");
mylogger.setLevel(Level.INFO);
mylogger.setUseParentHandlers(false);
FileHandler handler;
try {
handler = new FileHandler("../logs/mylogger%u.log");
mylogger.addHandler(handler);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mylogger.info("this is a file log!");
}