The Java SDK comes with Logger

Source: Internet
Author: User
Tags string format system log

Java's own log, although not as powerful as log4j, but to meet the daily use is more than enough, the following is a brief introduction:

There are 3 more important concepts in Logger, namely the recorder (Logger), the processor (Handler), and the Formatter (Formatter) to perform the following functions:

      • Logger: Logging, setting log levels, and so on.
      • Handler: Determine the output location and so on.
      • Fomatter: Format the information in the record according to your wishes.

To Create a logger object :

Static Logger GetLogger (String name)
Static Logger GetLogger (string name, String resourcebundlename)


Note: Name is logger, and when the name is the same, the logger of the same name is created only one, and if you create a name, it is not created, Just return the existing logger with the same name. So here's actually a bit of a singleton pattern of that flavor, and the back Resourcebundlename is the name of the specified resource bundle,

Level of Logger

More detailed than the log4j level, all defined in Java.util.logging.Level.
The levels are sorted in descending order as follows:
SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
Finer
FINEST (lowest value)
In addition, there is a level off that can be used to turn off logging and use level all to enable logging for all messages.

Logger The default level is the default level of Info,logger is defined in the JRE installation directory under LIB.
# Limit The message that is printed on the console to INFO and above.
Java.util.logging.ConsoleHandler.level = INFO

Logs that are lower than info will not be displayed, and if you want to display a lower than info, you need to modify the configuration: By default, the file exists in Jre/lib/logging.properties

For example: To see the fine level of messages on the console, make the following settings:

Java.util.logging.consolehandler.level=fine


Simple example

1 Importjava.io.IOException;2 ImportJava.util.logging.ConsoleHandler;3 ImportJava.util.logging.FileHandler;4 ImportJava.util.logging.Formatter;5 ImportJava.util.logging.Level;6 ImportJava.util.logging.LogRecord;7 ImportJava.util.logging.Logger;8 9  Public classLogmain {Ten      Public Static voidMain (string[] args)throwsIOException { OneLogger log = Logger.getlogger ("Lavasoft");  A Log.setlevel (level.info); -Logger log1 = Logger.getlogger ("Lavasoft");  -System.out.println (log = = Log1);//true theLogger log2 = Logger.getlogger ("Lavasoft.blog");  - Log2.setlevel (level.warning); -  -Consolehandler Consolehandler =NewConsolehandler (); + Consolehandler.setlevel (level.all); - Log.addhandler (consolehandler); +Filehandler Filehandler =NewFilehandler ("E://lll.log");  A Filehandler.setlevel (level.info); atFilehandler.setformatter (NewMyloghander ());  - Log.addhandler (filehandler); -  -Log.info ("AAA");  -Log1.info ("CC"); -         //log2.info ("BBB"); inLog2.warning ("BBB"); -      }  to }  +  - classMyloghanderextendsFormatter { the @Override *    PublicString format (LogRecord record) { $         returnRecord.getlevel () + ":" + record.getmessage () + "\ n"; Panax Notoginseng   }  -}

The output is as follows:

3:59:37, 3:59:3712, 2016 3:59:37, 3:59:37, 3:59:37. pm Logmain Main warning: BBB

The log inside the D drive is shown below:

INFO:aaaINFO:ccWARNING:bbb

Here handle decided how to deal with these logs,Myloghander decided the format, no handle, the default console output, the above code equals to add a new two output mode, one is the console is output to text, So now there are three ways of handling, two of which are consoles, which is why the console prints two times. The formatting section can be no, but the format may be ugly. Handle and formatter, see below for details.

Logger's Handler

The 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 it can send it to a blog service or forward it to the operating system log.

You can disable Handler by executing setLevel (Level.off) and re-enable it 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.

Logger's Formatter
Formatter provides support for formatted 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 headers and trailing 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 framework and a single log Handler.
LogRecord (level, String msg)
Constructs a logrecord with a given level and message value.

Article reference: http://sunyuqian.iteye.com/blog/2233655

http://lavasoft.blog.51cto.com/62575/184492

The Java SDK comes with Logger

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.