JDK1.4 provides a logging package: java. util. logging, which can be used to control the log records in the program. For example, you can specify the Log Level and log location (console, file, socket, memory buffer), or create a sub-recorder, you can use the program control method to specify the content you want to record, or you can use the configuration file to specify the content without modifying the program. Compared with Log4j, Log4j is simpler and lighter. It is undoubtedly a better choice when the requirements for log output are not very complex.
First, we obtain an instance of the LogManager class:
LogManager lMgr = LogManager. getLogManager ();
Then we create a recorder and add it to the current Manager:
String thisName = "Logpkg ";
Logger log = Logger. getLogger (thisName );
LMgr. addLogger (log );
If the location of the log file is not specified, the log information is displayed on the console according to the content specified in the logging. properties file under the jre/lib directory. The default value is lelehandler.
In the program, we can publish log information by level. There are 7 levels: SERVER (maximum value), WARNING, INFO, CONFIG, FINE, FINER, FINEST (minimum value ), and OFF (not recorded ).
Log. server ("error ");
We can set the record level of the recorder to ignore messages lower than the WARNING level. Only information recorded by the server and warning can be output.
Log. setLevel (Level. WARNING );
Log.info ("This message is info"); // This information will be ignored and not output
Log. warning ("This message is warning"); // This information will be output