e389. minimizing the impact of logging code.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 12 行

TXT
12
字号
It is good to add logging code to an application, but the logging code should minimize its impact on the application, especially if the logging is not enabled. In particular, if the message to be potentially logged needs to be constructed, the method call should be wrapped in a cheaper check. For example, the method call 
    int count = 123;
    Logger logger = Logger.getLogger("com.mycompany.MyClass");
    logger.finest("count: "+count);

will cause the count to be converted to a string and then concatenated to another string. This is a lot of wasted work if the message will not be logged. To avoid this overhead, use Logger.isLoggable() to check if the message would be logged before calling the logging method. For example, 
    
    if (logger.isLoggable(Level.FINEST)) {
        logger.finest("count: "+count);
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?