zoukankan      html  css  js  c++  java
  • Java Logging: Basic Usage

     

    The most common way of using the Java Logging API is to create a Logger in each class that needs to log. This instance is typically made static and final, meaning all instances of that class use the same Logger instance. Here is an example:

    public class LoggingExamples {
    
        private static final Logger logger =
            Logger.getLogger(LoggingExamples.class.getName());
    
    }
    

      

    As you can see from this example, it is common practice to use the class name including package name as name for the Logger. The name of the Logger to create is passed as string parameter to the Logger.getLogger()method.

    Once instantiated, you can call the various logging methods on the Logger. All this is explained in the text on theLogger.

    There are several different places in your code that you may log from. It all depends on what you want to log. For instance, ordinary debug trace logging calls to entering() and exiting() would be called at the beginning and end of a method call. If, on the other hand, you want to log an exception that occurred, you might want to log from inside a catch clause. Here are a few examples of logging:

    public class LoggingExamples {
    
        private static final Logger logger =
            Logger.getLogger(LoggingExamples.class.getName());
    
    
        public void doIt() {
            logger.entering(getClass().getName(), "doIt");
    
            try{
                //... something that can throw an exception
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Error doing XYZ", e);
            }
    
            logger.exiting(getClass().getName(), "doIt");
        }
    
    }
    

      

  • 相关阅读:
    bzoj4555
    bzoj4516
    树莓派/Debian 挂载硬盘
    树莓派/Debian Apache2 安装腾讯云 SSL 证书
    2019-2020-2《网络对抗技术》 Exp2 后门原理与实践
    kali 开启 SSH 服务
    Docker 入门 7 构建镜像
    Docker 入门 6 获取、加速镜像.md
    Docker 入门 5 数据管理
    Docker 入门 4 容器端口映射 和 Nginx 演示部署
  • 原文地址:https://www.cnblogs.com/hephec/p/4579594.html
Copyright © 2011-2022 走看看