advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Looking into the JDK 1.4 Logging API :

Log Levels

Every logger and every message has an associated logging Level. If a logger receives a message for logging and the message has a level lower than what the logger is set up to log, then the logger discards the message. The following levels are pre-defined: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST.

Handlers also have levels, and they're used in the same way as for loggers.

The Logger hierarchy

Loggers are placed in a hierarchy. Given are two loggers: the root logger (with no name) and the global logger, which has the root logger as its parent. If you define two loggers:

Logger logger1 = Logger.getLogger("abc.def");
    Logger logger2 = Logger.getLogger("abc.def.Game");

then you'll get this hierarchy:

The consequences of having this hierarchy are:

  • If a logger has no defined level, it searches up the tree until it finds a defined level, and uses it. The root logger has as default a level of "INFO".
  • When a logger has completed its own logging (through its handlers) it sends the message to its parents' handlers--all the way up the tree.

There are some rules regarding "sending messages up the tree" that should be observed:

  • First of all you'll only see a message logged if its level is higher or equal to the level of the logger you use.
  • The parent loggers' levels has no significance. It's the loggers' handlers and their levels that are used.
  • A handler is only used if the message has a level that is higher or equal to the level of the handler.
  • If a logger has no handler defined then you simply continue up the tree.
  • All handlers, up to and including the root handler, are checked.

If, for example, no handlers are explicitly specified for "abc.def.Game" and "abc.def" then it's only the handler for the root logger that will produce any output. A logger may however choose to break this chain by calling its setUseParentHandlers method with the value false.

Filters

I'll not use time on Filters, but just mention that a handler also checks it's filter (if one is defined) to see if logging should be done. A filter is a class that can inspect all the data that goes into the message and then determine if the message should be logged. See the resources section if you'd like to read more about filters.

Configuring the Loggers

The Logging API allows you to define the logging configuration in detail. The default logging configuration comes from the logging.properties file, which you find in the JDK's jre/lib directory. In this file you define the handlers for the root logger:

One handler:

handlers= java.util.logging.ConsoleHandler

Two handlers:

handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler

You may also define the default properties for the handlers, e.g.:

java.util.logging.ConsoleHandler.level = INFO
    java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

These default properties apply to any logger that uses these handlers.

Finally you may define the level for any named logger like this:

The root logger:

.level= INFO

Other loggers:

com.xyz.foo.level = SEVERE

If you want to use for example FileHandlers with different properties, then the logging.properties file can't be used, but the logging API has plenty of methods that you may use to accomplish the tasks. A pair of examples:

A logger with a ConsoleHandler with low priority level:

Logger logger1 = Logger.getLogger("abc.def"); 
logger1.setLevel(Level.FINEST);
ConsoleHandler c1 = new ConsoleHandler();
c1.setLevel(Level.FINEST);
logger1.addHandler(c1);

A logger with a FileHandler using a home made formatter:

Logger logger2 = Logger.getLogger("abc.def.ghi"); 
logger2.setLevel(Level.CONFIG);
FileHandler f2 = new FileHandler("%h/mylog.txt", 10000, 2, false);
f2.setFormatter(new MyFormatter());
f2.setLevel(Level.CONFIG);
logger2.addHandler(f2);

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs