Java Log System Learning--java.util.log.level

Source: Internet
Author: User

Log Level Java.util.log.Level

Java will log is a relatively simple object, only three properties, Name,value,resourcebundlename, representing the name of the log, value and resource file name, the log is divided into the following 6 levels, the six levels in the level of static constants in the way ( Note that the constructor for this class is protected, meaning that only the subclass can use new to construct the object, resourcebundlename the default read sun.util.logging.resources.logging)

Level.off = new levels ("Off", integer.max_value,defaultbundle)--This log level is special to close the log

Level.severe = new Level ("SEVERE", 1000,defaultbundle)--the system's more serious error levels, typically unrecoverable bugs

level.warning = new Level ("WARNING", 900,defaultbundle)--Warning prompt

Level.info = new Level ("INFO", 800,defaultbundle)--General information output

Level.config = new Level ("CONFIG", 700,defaultbundle)--static configuration information output, such as Cpu,mem

Level.fine = new Level ("FINE", 600,defaultbundle)--

Level.finer = new Level ("finer", 500,defaultbundle)--

Level.finest = new Level ("FINEST", 400,defaultbundle)--fine,finer,finest three log levels are used to track log information, and the level of detail of the output is incremented sequentially

Level.all = new Level (' All ', integer.min_value,defaultbundle)--All log information is output

Learn something.

1) The object is often quantified

There are not many log levels in the system, all the objects are made into static variables, as long as the initialization, at any time can call the

2 prevent external programs from flooding call new to generate log-level objects

You can see that the constructor of the level class is declared as protected type in order for the external caller to refer to the static constant directly instead of using new to generate its own object, while preserving the extensible nature of the class, the subclass can still extend the constructor of the parent class to generate the object

3 increment/decrement of log level

You can see that the name of a log object is just a text description of that level, and the value of the level follows the rule of ascending from small to large, from Integer.max_value to Integer.min_value, the output log information is more and more detailed, The benefits of this design can be in the program through a very simple integer size judgment to determine which level of the log output, if (Level<=level.all) {}else if (levels <=level.finest) {} ...

4 Small Tip There is a static method in the--level class that converts a string into a level object, such as passing in "INFO" to return Level.info, or passing in "400" and returning to Level.finest

public static synchronized level parse (String name) throws IllegalArgumentException {

Name.length (); --An alternative way to check for null pointers


First through the name check, such as incoming is "INFO" case, if found the direct return of the

Look for a known level with the given non-localized name.

for (int i = 0; i < known.size (); i++) {

Level L = (level) known.get (i);

if (Name.equals (L.name)) {

return l;

}

}


If it's not a format like "INFO", check if it's like "400"

try {

int x = integer.parseint (name);

for (int i = 0; i < known.size (); i++) {

Level L = (level) known.get (i);

if (L.value = = x) {

return l;

}

}

Create a new level.

Return to new level (name, x);

catch (NumberFormatException ex) {

Not an integer.

Drop through.

}


That is not "INFO" or "400" format, check the resource file

for (int i = 0; i < known.size (); i++) {

Level L = (level) known.get (i);

if (Name.equals (L.getlocalizedname ())) {

return l;

}

}


It's not an error.

throw new IllegalArgumentException ("Bad Level/" "+ Name +"/");

}

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.