`
gaofen100
  • 浏览: 1189511 次
文章分类
社区版块
存档分类
最新评论

log4j when need to use log4j.isDebugEnabled()

 
阅读更多

from http://huangxx.iteye.com/blog/190693


在使用log4j,common-log这样的log框架时,发现很多代码中这样写
if (log.isDebugEnabled()) {
log.debug( "xxxx ");

}


我很奇怪,为什么要与log.isDebugEnabled()?既然log.debug()在没有指定输出级别为DEBUG时不会有输出,为什么还要在前面加一个isDebugEnabled()的判断?
Java代码 收藏代码
  1. 为了效率,如果上述那个代码那么简单是没有必要的
  2. 但是如果这样
  3. if(log.isDebugEnabled()){
  4. log.debug(buildFullString());
  5. }
  6. 如果这个buildFullString效率不太高,那么如果直接写log.debug(buildFullString());的话
  7. 虽然它不会打印语句,但是buildFullString还是被执行了,这样就白费了功夫
  8. 所以加上isDebugEnabled就可以避免执行buildFullString了
  9. 这个方法一般用在认为buildFullString这个函数效率不太高的情况下

Java代码 收藏代码
  1. 加不加if,效果相同,但是效率不同
  2. 在你的例子里,debug的参数就是一个string,所以没有太本质的差别
  3. 但是假如log.debug("map="+map)的话,在调用debug之前,必然需要调用map.toString(),而这个操作可能导致更多的大量字符串操作,最后,才能得到这个辛辛苦苦拼接出来的结果,作为参数传入debug(),结果,这个参数却不需要(一般在log方法入口就判断输出level)。

分享到:
评论

相关推荐

    erlang日志应用log4erl(非sasl)

    1- In order to use log4erl, you need to first include it in the path. There are 2 ways to do this: a) include the "log4erl" directory in erlang's "lib" directory in the target machine (cp -Rf log4erl ...

    unix power tools

    ..............................................................................................................................................................................4 1.2 Who Listens to What ...

    STG (SNMP Traffic Grapher)

    If you need more than two - try to use more than one instance of program. Network values: Request Timeout (milliseconds): minimal 10ms (0.01 sec) maximal 32000ms (32s) probably shouldn'...

    EurekaLog_7.5.0.0_Enterprise

    4)....Added: Asking e-mail when user switches to "details" from MS Classic without entering e-mail 5)....Fixed: Compatibility issues with older Bugzilla versions (3.x) 6)....Fixed: Passing settings ...

    微软内部资料-SQL性能优化2

    Before we look at how SQL Server uses and manages its memory, we need to ensure a full understanding of the more common memory related terms. The following definitions will help you understand how SQL...

    Pro Unity Game Development with C#

    If you log on online right now, you can immediately visit web sites such as Blender, GIMP, Inkscape, and (of course) Unity to get access to professional-grade game development software completely ...

    BURNINTEST--硬件检测工具

    entry will be added to the normal BurnInTest log. - Changes to trace logging to reduce activity when trace logging is not turned on. - Note: We have seen a report of the Video Playback failing ...

    UE(官方下载)

    This is a convenient feature when you're manually comparing files, when you want to copy/paste between multiple files, or when you simply want to divide up your edit space. Tabbed Child Windows ...

    php.ini-development

    much more verbose when it comes to errors. We recommending using the ; development version only in development environments as errors shown to ; application users can inadvertently leak otherwise ...

    端口查看工具

    whether you want to clear the log, in order to avoid from clearing the log file by mistake. * Version 1.91: o Added 'Beep On New Ports' option. * Version 1.90: o Added 'Tray Balloon On New Ports...

    CakePHP 1.3 Application Development Cookbook.pdf

    Code words in text are shown as follows: "Create a file named query_log.php and place it in your app/controllers/components folder with the following contents:" CakePHP is a rapid development ...

    Git-2.21.0-64-bit.zip

    give the user a chance to edit the log message, even when there is otherwise no need to create a new merge and replace the existing one (i.e. fast-forward instead), but did not. Which has been ...

    drupal 6.12

    uploads, you may need to use the following command from a shell or system prompt (while in the installation directory): chmod o+w uploads * Access the file system path settings in Drupal by ...

    apktool documentation

    After you find a framework file you could pull it via adb pull /path/to/file or use a file manager application. After you have the file locally, pay attention to how Apktool installs it. The number ...

    PHP and MySQL Web Devepopment 4th Edition.pdf

    Before you can really appreciate all the tool's capabilities you need to become familiar with the optional parameters that you can use in conjunction with the DCDIAG command. If you look at Figure B,...

    窗口大小调整软件

    * Enable StickyScreen to make windows always sticky to the screen borders and the taskbar when dragging or resizing (no need to press shift). Fullscreen windows, such as games, will not be dragged or...

    Monyog-3.7.6.0 含注册

    To use the MONyog mail alert functionality to mail you alerts about a MySQL server you will first need to select that feature when registering a server. You can choose to receive mails, not only when ...

    DebuggingWithGDB 6.8-2008

    4 Running Programs Under gdb . . . . . . . . . . . . . 25 4.1 Compiling for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Starting your Program . . . . . . . . . ...

    ICS delphixe10源码版

    Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The...

    微软内部资料-SQL性能优化5

    When the index is traversed to the leaf level, the data itself has been retrieved, not simply pointed to. Uniqueness Is Maintained In Key Values In SQL Server 2000, all clustered indexes are unique. ...

Global site tag (gtag.js) - Google Analytics