`
- 浏览:
12777 次
- 性别:
- 来自:
北京
-
Log4j之logger.isInfoEnabled()和logger.isDebugEnabled
1.看下apache的官方的document,在Performance下那块(From: http://logging.apache.org/log4j/1.2/manual.html)
For example, for some logger cat, writing,
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
incurs the cost of constructing the message parameter, i.e. converting both integer i and entry[i] to a String, and concatenating intermediate strings, regardless of whether the message will be logged or not. This cost of parameter construction can be quite high and it depends on the size of the parameters involved.
To avoid the parameter construction cost write:
if(logger.isDebugEnabled() { logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i])); }
This will not incur the cost of parameter construction if debugging is disabled. On the other hand, if the logger is debug-enabled, it will incur twice the cost of evaluating whether the logger is enabled or not: once in debugEnabled and once in debug. This is an insignificant overhead because evaluating a logger takes about 1% of the time it takes to actually log.
In log4j, logging requests are made to instances of the Logger class. Logger is a class and not an interface. This measurably reduces the cost of method invocation at the cost of some flexibility.
Certain users resort to preprocessing or compile-time techniques to compile out all log statements. This leads to perfect performance efficiency with respect to logging. However, since the resulting application binary does not contain any log statements, logging cannot be turned on for that binary. In my opinion this is a disproportionate price to pay in exchange for a small performance gain.
2.简单来说,就是用isDebugEnabled方法判断下是能提升性能的。(From: http://blog.sina.com.cn/s/blog_616b57310100f36s.html )
if(logger.isInfoEnabled()) { logger.info("User " + userId + " is using app " +appId); }
为什么要加上logger.isInfoEnabled()?原因有两点。
1).直接使用logger.info("User " + userId + " is using app" + appId)来输出log,也能够达到log级别为INFO或在INFO以下时才输出:("User " + userId + "is using app " +appId),因为logger.info方法内部有判断输出级别的代码。但是在进入logger.info函数之前,("User " +userId + " is using app " + appId)这个表达式已经通过运算拼接成了一个字符串;而如果事先使用 if(logger.isInfoEnabled())进行判断,那么当log级别在INFO以上时,就能省去上述的字符串操作,在高并发和复杂log信息拼接的情况下,使用这种标准的方法输出log能够省去不小的系统开销。另外,如果构造log信息的过程需要大量字符串操作,建议使用StringBuilder来完成字符串拼接。
2).ERROR及其以上级别的log信息是一定会被输出的,所以只有logger.isDebugEnabled和logger.isInfoEnabled方法,而没有logger.isErrorEnabled方法。
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Log4j是Apache软件基金会下的一个开源项目,它提供了一种强大且灵活的方式来管理和控制应用程序的日志记录。相较于简单的打印语句,Log4j提供了更为高级的功能,比如能够根据不同的级别过滤日志消息、支持多种输出...
- `log4j.rootLogger` 表示根日志记录器的日志级别和 Appender 名称。 - `log4j.appender.Console` 指定了控制台 Appender 的类。 - `log4j.appender.Console.layout` 和 `log4j.appender.Console.layout....
本文将详细介绍 Log4j 的配置方法,帮助读者更好地理解和使用 Log4j。 #### 二、配置文件格式 Log4j 支持多种配置文件格式,包括 XML、Properties 和 JSON 等。其中最常用的两种是 XML 和 Properties 格式。本文...
3. Logger工作原理:在`org.apache.log4j.Logger`类中,每个logger都有一个优先级,通过`isDebugEnabled()`、`isInfoEnabled()`等方法检查当前日志级别是否允许输出特定级别的日志。 4. 配置解析:`org.apache.log4...
- **日志级别检查**: 提供了一系列布尔属性(如 `isDebugEnabled`、`isInfoEnabled`等),用于检查当前Logger的日志级别设置。 **3.1.2 日志级别** Log4net定义了五个基本的日志级别:`DEBUG`, `INFO`, `WARN`, `...
这个框架由Apache软件基金会开发,是Apache log4j在Java平台上的.NET版本。Log4Net 文件日志主要涉及以下几个核心知识点: 1. **配置与使用**: - **配置文件**:Log4Net 的配置通常通过XML文件(如log4net.config...
log4net是.NET框架下的一款成熟且强大的日志记录组件,它基于Apache的log4j设计,为开发者提供了一套全面的日志记录API。在软件开发过程中,良好的日志记录机制对于系统监控、错误排查和性能分析具有不可替代的作用...
在***开发中,使用日志记录工具log4net是一种常见且高效的方法,用于记录应用程序的运行状态和出现的问题。本文将详细介绍在***项目中如何配置和使用log4net,包括配置log4net的步骤、如何在代码中实现日志记录,...