标签:
日志系统(Log System)是将信息输出到一个或者多个目标上的一种机制。一个日志器(Logger)有下面几个组件。
使用日志系统有以下两个主要目的:
在本节,我们将学习如何使用java.util.logging包提供的类来将一个日志系统增加到并发应用程序中。
1. 创建一个名为MyFormatter的类,继承java.util.logging.Formatter类。然后,实现抽象format()方法。它以LogRecord对象为参数,返回一个带有日志消息的String对象。
import java.util.Date; import java.util.logging.Formatter; import java.util.logging.LogRecord; public class MyFormatter extends Formatter { @Override public String format(LogRecord record) { StringBuilder sb = new StringBuilder(); sb.append("["+record.getLevel()+"] - "); sb.append(new Date(record.getMillis())+" : "); sb.append(record.getSourceClassName()+"."+record.getSourceMethodName()+" : "); sb.append(record.getMessage()+"\n"); return sb.toString(); } }
2. 创建一个名为MyLogger的类。
import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; public class MyLogger { private static Handler handler; public static Logger getLogger(String name){ Logger logger = Logger.getLogger(name); logger.setLevel(Level.ALL); try { if(handler==null){ handler = new FileHandler("D:\\recipe8.log"); Formatter format = new MyFormatter(); handler.setFormatter(format); } if(logger.getHandlers().length==0){ logger.addHandler(handler); } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return logger; } }
3. 创建一个名为Task的类,实现Runnable接口。它是用来测试Logger对象的任务。
import java.util.concurrent.TimeUnit; import java.util.logging.Logger; public class Task implements Runnable { @Override public void run() { try { Logger logger = MyLogger.getLogger(this.getClass().getName()); logger.entering(Thread.currentThread().getName(), "run()"); TimeUnit.SECONDS.sleep(2); logger.exiting(Thread.currentThread().getName(), "exit()", Thread.currentThread()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
4. 实现范例的主类Main,并实现main()方法。
import java.util.logging.Level; import java.util.logging.Logger; public class Main { public static void main(String[] args) { Logger logger = MyLogger.getLogger("Core"); logger.entering("Core", "main()",args); Thread threads[] = new Thread[5]; for(int i=0;i<threads.length;i++){ logger.log(Level.INFO, "Launching thread: ", +i); Task task = new Task(); threads[i] = new Thread(task); logger.log(Level.INFO, "Thread created: "+threads[i].getName()); threads[i].start(); } logger.log(Level.INFO, "Five Threads created. Waiting for its finalization."); try { for(int i=0;i<threads.length;i++){ threads[i].join(); logger.log(Level.INFO, "Thread has finished its execution", threads[i]); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } logger.exiting("Core", "main()"); } }
5. 控制台输出结果如下
十月 28, 2015 7:40:16 下午 Main main 信息: Launching thread: 十月 28, 2015 7:40:16 下午 Main main 信息: Thread created: Thread-1 十月 28, 2015 7:40:16 下午 Main main 信息: Launching thread: 十月 28, 2015 7:40:16 下午 Main main 信息: Thread created: Thread-2 十月 28, 2015 7:40:16 下午 Main main 信息: Launching thread: 十月 28, 2015 7:40:16 下午 Main main 信息: Thread created: Thread-3 十月 28, 2015 7:40:16 下午 Main main 信息: Launching thread: 十月 28, 2015 7:40:16 下午 Main main 信息: Thread created: Thread-4 十月 28, 2015 7:40:16 下午 Main main 信息: Launching thread: 十月 28, 2015 7:40:16 下午 Main main 信息: Thread created: Thread-5 十月 28, 2015 7:40:16 下午 Main main 信息: Five Threads created. Waiting for its finalization. 十月 28, 2015 7:40:18 下午 Main main 信息: Thread has finished its execution 十月 28, 2015 7:40:18 下午 Main main 信息: Thread has finished its execution 十月 28, 2015 7:40:18 下午 Main main 信息: Thread has finished its execution 十月 28, 2015 7:40:18 下午 Main main 信息: Thread has finished its execution 十月 28, 2015 7:40:18 下午 Main main 信息: Thread has finished its execution
6. 日志recipe8.log内容如下:
[FINER] - Wed Oct 28 19:40:16 CST 2015 : Core.main() : ENTRY [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Launching thread: [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Thread created: Thread-1 [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Launching thread: [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Thread created: Thread-2 [FINER] - Wed Oct 28 19:40:16 CST 2015 : Thread-1.run() : ENTRY [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Launching thread: [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Thread created: Thread-3 [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Launching thread: [FINER] - Wed Oct 28 19:40:16 CST 2015 : Thread-3.run() : ENTRY [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Thread created: Thread-4 [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Launching thread: [FINER] - Wed Oct 28 19:40:16 CST 2015 : Thread-2.run() : ENTRY [FINER] - Wed Oct 28 19:40:16 CST 2015 : Thread-4.run() : ENTRY [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Thread created: Thread-5 [INFO] - Wed Oct 28 19:40:16 CST 2015 : Main.main : Five Threads created. Waiting for its finalization. [FINER] - Wed Oct 28 19:40:16 CST 2015 : Thread-5.run() : ENTRY [FINER] - Wed Oct 28 19:40:18 CST 2015 : Thread-1.exit() : RETURN {0} [INFO] - Wed Oct 28 19:40:18 CST 2015 : Main.main : Thread has finished its execution [FINER] - Wed Oct 28 19:40:18 CST 2015 : Thread-3.exit() : RETURN {0} [FINER] - Wed Oct 28 19:40:18 CST 2015 : Thread-4.exit() : RETURN {0} [FINER] - Wed Oct 28 19:40:18 CST 2015 : Thread-2.exit() : RETURN {0} [INFO] - Wed Oct 28 19:40:18 CST 2015 : Main.main : Thread has finished its execution [INFO] - Wed Oct 28 19:40:18 CST 2015 : Main.main : Thread has finished its execution [INFO] - Wed Oct 28 19:40:18 CST 2015 : Main.main : Thread has finished its execution [FINER] - Wed Oct 28 19:40:18 CST 2015 : Thread-5.exit() : RETURN {0} [INFO] - Wed Oct 28 19:40:18 CST 2015 : Main.main : Thread has finished its execution [FINER] - Wed Oct 28 19:40:18 CST 2015 : Core.main() : RETURN
在上面的实现类中,已使用LogRecord类的下列方法来获取日志消息的信息。
在主程序中,使用了下列方法。
也有其他类库比java.util.logging包提供了更完全的日志系统,如Log4j或slf4j类库。但是java.util.logging包是Java API的一部分,并且它的所有方法都是安全的,所以用在并发应用程序中没有问题。
标签:
原文地址:http://www.cnblogs.com/gaopeng527/p/4918093.html