Java Notes-12(Preferences API, The Logging API)

來源:互聯網
上載者:User

標籤:logging   java   

Summary:Preferences API, The Logging API

- The Preferences API is like a portable version of the Windows registry, a mini-database in which you can keep small amounts of information, accessible to all applications

-Preferences are stored logically in a tree. 

Preferences prefs = Preferences.userRoot().node("oreilly/learningjava");prefs.put("author", "Niemeyer");prefs.putInt("edition", 4);String author = prefs.get("author", "unknown");int edition = prefs.getInt("edition", -1);

-Preferences are stored in two separate trees: system preferences and user preferences.System preferences are shared by all users of the Java installation. But user preferences
are maintained separately for each user; 

-The  node() method accepts either a relative or an absolute path. 

Preferences prefs =Preferences.userRoot().node("oreilly").node("learningjava");

-Often your application should be notified if changes are made to the preferences while it’s running. You can get updates on preference changes using the  PreferenceChange
Listener and  NodeChangeListener interfaces. 

Preferences prefs =Preferences.userRoot().node("/oreilly/learningjava");prefs.addPreferenceChangeListener( new PreferenceChangeListener() {public void preferenceChange(PreferenceChangeEvent e) {System.out.println("Value: " + e.getKey()+ " changed to "+ e.getNewValue() );}} );

-The  java.util.logging package provides a highly flexible and easy-to-use logging framework for system information, error messages, and fine-grained tracing (debugging) output.

 -The heart of the logging framework is the logger, an instance of  java.util.logging.Logger . 

package com.oreilly.learnjava;public class Book {static Logger log = Logger.getLogger("com.oreilly.learnjava.Book");


-The logger provides a wide range of methods to log messages; some take very detailed information, and some convenience methods take only a string for ease of use

log.warning("Disk 90% full.");log.info("New user joined chat room.");

-a logger for the name “global” is provided in the static field  Logger.global . You can use it as an alternative to the old standby  System.out.println() 

Logger.global.info("Doing foo...")

-Before a logger hands off a message to its handlers or its parent’s handlers, it first checks whether the logging level is sufficient to proceed. If the message doesn’t meet the required level, it is discarded at the source. 

Level                          Meaning
SEVERE Application failure
WARNIN Notification of potential problem
INFO  Messages of general interest to end users
CONFIG Detailed system configuration information for administrators
FINE Successively more detailed application tracing information for developers
FINER  
FINEST  


Java Notes-12(Preferences API, The Logging API)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.