1 public class SimpleLog { 2 3 4 public static void main(String[] args) throws SecurityException, IOException { 5 Logger log=Logger.getLogger("Test"); 6 //添加log处理,默认是console 7 ConsoleHandler console=new ConsoleHandler(); 8 console.setLevel(Level.ALL); 9 log.addHandler(console); 10 FileHandler file=new FileHandler("log.log"); 11 file.setLevel(Level.WARNING); 12 log.addHandler(file); 13 //设置日志输出格式 14 file.setFormatter(new Formatter() { 15 16 @Override 17 public String format(LogRecord record) { 18 // TODO Auto-generated method stub 19 return new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date(record.getMillis())) +"------"+record.getLevel()+"-------"+record.getClass()+"-------"+record.getLoggerName()+"----"+record.getMessage()+"\t\n"; 20 } 21 }); 22 console.setFormatter(new Formatter() { 23 24 @Override 25 public String format(LogRecord record) { 26 return new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date(record.getMillis())) +"------"+record.getLevel()+"-------"+record.getClass()+"---"+record.getSourceMethodName()+"-------"+record.getLoggerName()+"----"+record.getMessage()+"\t\n"; 27 } 28 }); 29 //日志控制 30 log.log(Level.SEVERE, "测试日志"); 31 log.log(Level.INFO, "测试日志"); 32 log.log(Level.WARNING, "测试日志"); 33 log.log(Level.OFF, "测试日志"); 34 35 }
循序渐进看Java web日志跟踪(2)-Java日志API认识
原文:http://www.cnblogs.com/Seanit/p/5126019.html