📄 e385. the quintessential logging program.txt
字号:
To log a message, you first need to obtain a Logger object and then use it to log the message. Loggers have names that resemble a fully qualified class name. Typically, log messages generated from a class would use a Logger with the same name as the class.
The logger contains one or more handlers that are responsible for processing the log records. A handler could write to a file or print to a console. Typically, the code that generates log records should not concern itself with where those log records end up. It only needs to decide the name of the logger to use.
This example logs a few messages to a logger. For examples of controlling the destination of the log messages, see e391 Writing Log Records to a Log File and e392 Writing Log Records to Standard Error.
import java.io.*;
import java.util.logging.*;
package com.mycompany;
public class BasicLogging {
public static void main(String[] args) {
// Get a logger; the logger is automatically created if
// it doesn't already exist
Logger logger = Logger.getLogger("com.mycompany.BasicLogging");
// Log a few message at different severity levels
logger.severe("my severe message");
logger.warning("my warning message");
logger.info("my info message");
logger.config("my config message");
logger.fine("my fine message");
logger.finer("my finer message");
logger.finest("my finest message");
}
}
Related Examples
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -