Java logger Introduction

Source: Internet
Author: User

 

An example of log output using Java. util. Logging. logger is as follows:

Package COM. sample; </P> <p> Import Java. io. file; <br/> Import Java. util. logging. filehandler; <br/> Import Java. util. logging. handler; <br/> Import Java. util. logging. level; <br/> Import Java. util. logging. logger; <br/> Import Java. util. logging. simpleformatter; </P> <p> public class loggersample {</P> <p> protected logger log = logger. getlogger (this. getclass (). getname (); </P> <p> Public loggersample () {<br/>}</P> <p> Pu BLIC void sethandler (handler) {<br/> This. log. addhandler (handler); <br/>}</P> <p> Public int add (int A, int B) {<br/> int result = A + B; <br/> string L = a + "+" + B + "=" + result; <br/> log. log (level. info, L); // * 1 <br/> // log.info (l); <br/> system. out. print (this. getclass (). getname (); <br/> return result; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> try {<br/> logger logg = logger. getlog GER ("com. sample. loggersample "); // * 2 <br/> logg. setlevel (level. info); // * 3 </P> <p> file F = new file ("C:/logger/log. log "); <br/> If (! F. exists () {<br/> New file ("C:/logger "). mkdir (); <br/>}</P> <p> filehandler FH = new filehandler (F. tostring (); // * 4 <br/> FH. setformatter (New simpleformatter (); </P> <p> // lelehandler CH = new consolehandler (); <br/> // ch. setlevel (level. all); </P> <p> loggersample ls = new loggersample (); <br/> ls. sethandler (FH); <br/> ls. add (1, 2); </P> <p> FH. close (); <br/>}catch (exception e) {<br/> system. out. println ("exception thrown:" + E); <br/> E. printstacktrace (); <br/>}</P> <p>}

 

* 3: Set the logger output level

The levels are listed in descending order as follows:
Severe (maximum value)
Warning
Info
Config
Fine
Finer
Finest (minimum value)
In addition, there is a level off that can be used to Disable Logging and enable logging for all messages with level all.
If the set level is higher than the predefined output level, the log at the lower level (for example, * 1) will not be output.

 

* 1:

There are two logger output methods:
① Log. Log (level. info, "message ")
② Log.info ("mesage ")
Output at other levels is similar.

 

* 2:

Obtain the logger object of the COM. sample. loggersample class.
You can set its level so that the loggersample will output logs based on the level pre-defined in loggersample.

 

* 4:

You can add handler to logger for different forms of output:
① Filehandler: output to file
② Consolehandler: output to the console
③ Sockethandler: output to Network

 

 

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.