Log4J 日志记录是一项成本高昂的事务吗?

发布于 2024-09-27 05:06:28 字数 708 浏览 0 评论 0原文

记录器

嗨,

我有一个 Spring MVC Web 应用程序,需要一些服务器端日志记录,因此我在 Spring MVC

<listener>
 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

控制器中配置了 Log4J,我记录了所有用户事务。

@Controller
public class MyController {
 protected final Log logger = LogFactory.getLog(getClass());

 public String setup(){
  MyObject object = new MyObject();

  logger.info("User check in data...." + object.data);
 }
}

但我有一些问题:

  1. Spring MVC Web 应用程序中的登录是否被视为成本高昂的事务?我的意思是这会减慢我的应用程序的速度吗?
  2. 在生产环境中,如果我省略这个logger.info可以吗?我需要添加此内容以实现每笔交易的可追溯性,并且这是一项要求 但我担心这些的影响。

谢谢

Logger

Hi,

I have a Spring MVC webapps that needs some server side logging so I configured Log4J in my Spring MVC

<listener>
 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

In my controller, I logged all user transaction.

@Controller
public class MyController {
 protected final Log logger = LogFactory.getLog(getClass());

 public String setup(){
  MyObject object = new MyObject();

  logger.info("User check in data...." + object.data);
 }
}

I have some questions though:

  1. Are Logging in a Spring MVC Webapps considered a costly transaction? I mean will this slowdown my app?
  2. In a production enviroment, will it be OK if I leave out this logger.info? I need to add this for traceability of each transaction and is a requirement
    but I am worried about the impact of these.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-10-04 05:06:28

Log4j 本身几乎没有开销。它旨在编译到您的代码中并通过配置启用或禁用。

当它被禁用时,速度非常快。您可能遇到的唯一问题是准备日志消息,该消息将不会被使用。您可以使用防护来避免这种情况:

 if (logger.isInfoEnabled()){  // in case object.data.toString() is costly
    logger.info("User check in data...." + object.data);
 }

当启用它时,显然需要一些时间才能将消息写入日志文件/守护程序/数据库。但是,由于您已启用它,因此您需要该日志消息(您说您有可追溯性要求),因此公平的比较是以另一种方式创建日志消息。在这里,log4j 开销(与直接调用日志记录后端相比)是最小的。

因此,记录消息的全部成本将取决于您使用的后端类型(而不是 log4j 本身)。

Log4j has almost no overhead itself. It is designed to be compiled into your code and enabled or disabled by configuration.

When it is disabled, it is very fast. The only problem you may have is in preparing the log message, which will then not be used. You can avoid this with a guard:

 if (logger.isInfoEnabled()){  // in case object.data.toString() is costly
    logger.info("User check in data...." + object.data);
 }

When it is enabled, it obviously takes some time to write the message to the log file/daemon/database. However, since you have enabled it, you need that log message (you say you have a traceability requirement), so the fair comparison would be to creating the log message in another way. And here, the log4j overhead (versus calling the logging backend directly) is minimal.

The full cost of logging the message will thus be determined by what kind of backend you are using (and not by log4j itself).

左秋 2024-10-04 05:06:28

在继续之前,有一个简短的免责声明:到目前为止,我还没有使用 Spring MVC 的经验,但是我已经将 log4j 与基本的 java servlet 一起使用,并且我相信同样的原则也适用。

根据您提供的代码,您可以做两件事来提高日志记录速度:

  • logger 声明为静态:

protected static final Log logger = LogFactory.getLog(MyController.class);

如果创建了对象的多个实例,这可以确保您不会每次都创建新的记录器(因为创建或检索记录器是一项昂贵的操作)。

  • 根据 Thilo 的回答,使用 if 语句来检查是否启用了指定级别的日志记录。您不仅可以避免 toString() 方法的成本(正如他正确指出的那样),还可以避免不必要的字符串连接。

如果您遵循这些建议,禁用日志记录时的日志记录成本将是 log4j 的初始设置成本 + 使用记录器为每个类初始化记录器 + 要评估的单个 if 语句(如果我没记错的话,在 log4j 中,归结为函数调用的开销加上单个整数比较)。

启用日志记录后,您必须考虑您选择使用的附加程序引入的任何成本(例如,记录到数据库将产生一些开销)以及格式化日志信息所需的时间。例如,使用 PatternLayout 时,使用 %F%M%L 模式是一项昂贵的操作,因为 log4j 会生成此信息通过构建完整的堆栈跟踪。

来源:log4j介绍和log4j源代码。

Before you continue, a brief disclaimer: as yet I have no experience with the spring MVC, however I have used log4j with basic java servlets and I believe the same principles apply.

Based on your provided code, you can do two things to improve the logging speed:

  • Declare logger as static:

protected static final Log logger = LogFactory.getLog(MyController.class);

If multiple instances of your object are created, this ensures you aren't creating a new logger each time (as creating or retrieving a logger is an expensive operation).

  • Guard using an if statement to check whether the specified level of logging is enabled, as per Thilo's answer. Not only will you avoid the costs of the toString() method (as he rightly pointed out), but you avoid unnecessary string concatenation.

If you follow these suggestions, the cost of logging when logging is disabled will be the initial setup cost of log4j + the initialization of a logger for each class using loggers + a single if statement to evaluate (and if I recall correctly, in log4j this boils down to the overhead of the function call plus a single integer comparison).

When logging is enabled, you have to account for any costs introduced by the appender you choose to use (for example, logging to a database will incur some overhead) as well as the time required to format the log information. For example, when using the PatternLayout, using the %F, %M, or %L patterns is an expensive operation as log4j generates this information by building a full stack trace.

Source: log4j introduction and log4j source code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文