Java.util.logging.Logger

Source: Internet
Author: User
Tags string format system log log4j
Java.util.logging.Logger Use detailed   Java.util.logging.Logger is not something new, 1.4, but because of the existence of log4j, this logger has been silent, in fact, in some of the test code, the JDK's own logger than log4j more convenient. First, create the Logger object static Logger GetLogger (String name)
Finds or creates a logger for the specified subsystem.
Static Logger GetLogger (string name, String resourcebundlename)
Finds or creates a logger for the specified subsystem.
Note: Name is logger, and when the name is the same, the logger of the same name is created only one. Second, the level of logger than the log4j level of detail, all defined in the java.util.logging.Level inside.
Levels in descending order are as follows: SEVERE (highest) WARNING INFO CONFIG FINE finer FINEST (lowest value) In addition, there is a level off that you can use to turn off logging and enable logging of all messages using level all. Logger The default level is info, and logs that are lower than info are not displayed. The default level definition for logger is under Lib of the JRE installation directory. # LIMIT The message this are printed on the console to INFO and above.
Java.util.logging.ConsoleHandler.level = INFO Three, simple instance 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
Info: AAA

Process finished with exit code 0 when commenting out Log2.setlevel (level.warning);
Output Result: True
2009-7-28 20:02:02 Testlogger Main
Info: AAA
2009-7-28 20:02:02 Testlogger Main
Info: BBB

Process finished with exit code 0 you can see from here that logger's name has a hierarchical relationship.   This is exactly the same as the Log4j control method. The following is the original version of the API document: You typically use a dot-delimited hierarchical namespace to name Logger. Logger names can be arbitrary strings, but they should generally be based on the package name or class name of the component being recorded, such as java.net or javax.swing. In addition, you can create an "anonymous" Logger whose name is not stored in the Logger namespace. The Logger object can be obtained by invoking a GetLogger factory method.   These methods either create a new Logger or return an appropriate existing Logger. The Logger Handler Handler object obtains the log information from the Logger and exports the information. For example, it can write this information to a console or file, or you can send that information to the blog service or forward it to the operating system log.
Handler can be disabled by executing setlevel (Level.off) and can be re-enabled by performing the appropriate level of setlevel.
The Handler class typically uses the Logmanager property to set the default values for the Filter, Formatter, and level of the Handler.
Java.util.logging.Handler
Java.util.logging.MemoryHandler
Java.util.logging.StreamHandler
Java.util.logging.ConsoleHandler
Java.util.logging.FileHandler
Java.util.logging.SocketHandler Example: 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 ("C:/testlog%g.log");
Filehandler.setlevel (Level.info);
Log.addhandler (Filehandler);
Log.info ("AAA");
Log2.info ("BBB");
Log2.fine ("fine");
}
Output Result: True
2009-7-28 20:36:14 Testlogger Main
Info: AAA
2009-7-28 20:36:14 Testlogger Main
Info: AAA
2009-7-28 20:36:14 Testlogger Main
Info: BBB
2009-7-28 20:36:14 Testlogger Main
Info: BBB

Process finished with exit Code 0 view C Disk: Visible, the default log mode is XML format, it sucks. So it's best to customize the logger format.   Need to be defined with formatter. V. Logger's Formatter Formatter provides support for formatting logrecords.
In general, each log record Handler has an associated Formatter. Formatter accepts the LogRecord and converts it to a string.
Some formatter, such as xmlformatter, need to wrap the header and tail strings around a set of formatted records.   You can use the GetHeader and GetTail methods to get these strings. The LogRecord object is used to pass log requests between the log frame and the individual log Handler.
LogRecord (Level level, String msg)
Constructs LogRecord with the given level and message value. Java.util.logging.Formatter
Java.util.logging.SimpleFormatter
Java.util.logging.XMLFormatter look at an example to understand: 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 ("C:/testlog%g.log");
Filehandler.setlevel (Level.info);
Filehandler.setformatter (New Myloghander ());
Log.addhandler (Filehandler);

Log.info ("AAA");
Log2.info ("BBB");
Log2.fine ("fine");
}
}

Class Myloghander extends Formatter {
@Override
Public String format (LogRecord record) {
return Record.getlevel () + ":" + record.getmessage () + "\ n";
}
Output: In the control and C-disk output of the file as shown here, Java logger content is gone. The feeling format is very unpleasant. The custom XML format sucks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.