/ Published in: Java

Now you are ready to log to your heart’s content. Keep in mind that all messages with level INFO, WARNING, and SEVERE show up on the console. Therefore, reserve these levels for messages that are meaningful to the users of your program. The level FINE is a good choice for logging messages that are intended for programmers. Whenever you are tempted to call System.out.println, emit a log message instead: logger.fine("File open dialog canceled"); It is also a good idea to log unexpected exceptions. For example: try { . . . } catch (SomeException e) { logger.log(Level.FINE, "explanation", e); }
Expand |
Embed | Plain Text
//The following code ensures that all messages are logged to an application-//specific file. Place the code into the main method of your application. { try { Logger.getLogger("").setLevel(Level.ALL); final int LOG_ROTATION_COUNT = 10; Handler handler = new FileHandler("%h/myapp.log", 0, LOG_ROTATION_COUNT); Logger.getLogger("").addHandler(handler); // åŒä¸€ä¸ªloggerå¯ä»¥æ·»åŠ å¤šä¸ªä¸åŒçš„handler } { logger.log(Level.SEVERE, "Can't create log file handler", e); } }
You need to login to post a comment.