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);
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值