java 中的Logging

java.util.logging

 

1.Logging   Basic Logging

 

Logger.global.info("File->Open menu item selected");

 

Logger.global.setLevel(Level.OFF);    at an appropriate place (such as the beginning of main), then all logging is suppressed.


这样日志就不会输出了。

 

2.Advanced Logging

Logger myLogger = Logger.getLogger("com.mycompany.myapp");

创建自己的日志对象,和包名一样,日志对象名是有层次结构,并且层次结构更强。if you set the log level on the logger "com.mycompany", then the child loggers inherit that level.

There are seven logging levels:

  • SEVERE (highest value)

  • WARNING

  • INFO

  • CONFIG

  • FINE

  • FINER

  • FINEST (lowest value)
    Now all levels of FINE and higher are logged. 这样一来,任何层次>= FINE都会被记录。
    Level.ALL 所有的,Level.OFF

    By default, the top three levels are actually logged. You can set a different level, for example,
    logger.setLevel(Level.FINE);

    There are logging methods for all levels, such as

    logger.warning(message);
    logger.fine(message);
    


     

    and so on. Alternatively, you can use the log method and supply the level, such as

    logger.log(Level.FINE, message);
    

 



 

Logging Cookbook

  1. For a simple application, choose a single logger. It is a good idea to give the logger the same name as your main application package, such as com.mycompany.myprog. You can always get the logger by calling

    Logger logger = Logger.getLogger("com.mycompany.myprog");
    

    For convenience, you may want to add static fields

    
    private static final Logger logger = Logger.getLogger("com
    .mycompany.myprog");
    

    to classes with a lot of logging activity.

  2. The default logging configuration logs all messages of level INFO or higher to the console. Users can override the default configuration, but as you have seen, the process is a bit involved. Therefore, it is a good idea to install a more reasonable default in your application.

    The following code ensures that all messages are logged to an application-specific file. Place the code into the main method of your application.

    
    if (System.getProperty("java.util.logging.config.class") == null
          && System.getProperty("java.util.logging.config.file") == null)
    {
       try
       {
          Logger.getLogger("").setLevel(Level.ALL);
          final int LOG_ROTATION_COUNT = 10;
          Handler handler = new FileHandler("%h/myapp.log", 0, 
     LOG_ROTATION_COUNT);
          Logger.getLogger("").addHandler(handler);
       }
       catch (IOException e)
       {
          logger.log(Level.SEVERE, "Can't create log file handler", e);
       }
    }
    

  3. Now you are ready to log to your heart's content. Keep in mind that all messages with level INFO, WARNING, and SEVERE show up on the console. Therefore, reserve these levels for messages that are meaningful to the users of your program. The level FINE is a good choice for logging messages that are intended for programmers.

    Whenever you are tempted to call System.out.println, emit a log message instead:

    logger.fine("File open dialog canceled");
    

    It is also a good idea to log unexpected exceptions, for example,

    try
    {
       . . .
    }
    catch (SomeException e)
    {
       logger.log(Level.FINE, "explanation", e);
    

你可能感兴趣的:(java,UP,idea)