标签:
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
三、简单实例
public class TestLogger { public static void main(String[] args) { Logger log = Logger.getLogger("lavasoft"); log.setLevel(Level.INFO); Logger log1 = Logger.getLogger("lavasoft"); System.out.println(log==log1); //true Logger log2 = Logger.getLogger("lavasoft.blog"); log2.setLevel(Level.WARNING); log.info("aaa"); log2.info("bbb"); log2.fine("fine"); } }
输出结果:
true 2009-7-28 20:00:30 TestLogger main 信息: aaa Process finished with exit code 0
输出结果: true 2009-7-28 20:02:02 TestLogger main 信息: aaa 2009-7-28 20:02:02 TestLogger main 信息: bbb
java.util.logging.Handler
java.util.logging.MemoryHandler
java.util.logging.StreamHandler
java.util.logging.ConsoleHandler
java.util.logging.FileHandler
java.util.logging.SocketHandler
public class TestLogger { public static void main(String[] args) throws IOException { Logger log = Logger.getLogger("lavasoft"); log.setLevel(Level.INFO); Logger log1 = Logger.getLogger("lavasoft"); System.out.println(log==log1); //true Logger log2 = Logger.getLogger("lavasoft.blog"); // log2.setLevel(Level.WARNING); //控制台控制器 ConsoleHandler consoleHandler =new ConsoleHandler(); consoleHandler.setLevel(Level.ALL); log.addHandler(consoleHandler);
//文件控制器 FileHandler fileHandler = new FileHandler("F:/testDir/8888g.log"); fileHandler.setLevel(Level.INFO); log.addHandler(fileHandler); log.info("aaa"); log2.info("bbb"); log2.fine("fine"); } }
输出结果: true 2009-7-28 20:36:14 TestLogger main 信息: aaa 2009-7-28 20:36:14 TestLogger main 信息: aaa 2009-7-28 20:36:14 TestLogger main 信息: bbb 2009-7-28 20:36:14 TestLogger main 信息: bbb
查看F:/testDir/8888g.log:
java.util.logging.Formatter
java.util.logging.SimpleFormatter
java.util.logging.XMLFormatter
public static void main(String[] args) { try { FileHandler fileHandler = new FileHandler("F:/testDir/8888g.log"); fileHandler.setLevel(Level.INFO); fileHandler.setFormatter(new Formatter() {//定义一个匿名类 @Override public String format(LogRecord record) { return record.getLevel() + ":" + record.getMessage() + "\n"; } }); logger.addHandler(fileHandler); logger.info("测试"); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
标签:
原文地址:http://www.cnblogs.com/liaojie970/p/5582147.html