log.debug是否会降低性能

编程入门 行业动态 更新时间:2024-10-28 16:22:16
本文介绍了log.debug是否会降低性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在调试日志中写一些日志,这些日志在具有信息日志级别的生产日志中将不可用.那么,这些额外的调试日志将如何影响性能?我的意思是,如果我们将日志级别设置为INFO,则记录器必须检查日志级别是什么,并发现log.debug需要忽略.

I want to write some logs at the debug log which will not be available in the production logs which has info log level. So how will this extra debug logs affect the performance? I mean if we set the log level at INFO, the logger has to check what the log level is and find that the log.debug needto be ignored.

那么,这种额外的日志级别检查是否会影响性能?

So does this extra log level checking affect performance?

在部署时是否有任何自动删除log.debug()语句的方法?我的意思是在开发期间,log.debug将在那里,我们可以进行调试.但是在生产部署期间,自动魔术机制将删除所有log.debug()消息.我不确定这些是否可行.

Is there any automagical way of removing the log.debug() statements while deployment? I mean during development time the log.debug will be there and we can debug. But during production deployment time the automagical mechanism will remove all log.debug() messages. I am not sure whether these are possible.

推荐答案

那么,这些额外的调试日志将如何影响性能?

So how will this extra debug logs affect the performance?

由于记录器是磁盘I/O调用(假设您正在写入文件系统),因此它会影响应用程序的性能,并且在生产环境中严格建议不要使用 DEBUG日志级别. >

It affects the performance of the application as loggers are disc I/O calls (assuming you are writing to file system) and DEBUG log level is strictly NOT recommended for production environments.

是否有任何自动方法删除log.debug()语句 部署期间?

Is there any automagical way of removing the log.debug() statements while deployment?

否,没有神奇的方法来删除log.debug()语句,但是,当您将日志记录级别设置为INFO时,只要将参数传递给debug()方法时您不做大量计算,它应该没事.例如,如果您将记录器级别设置为INFO,并假设您的代码中包含以下两个记录器,则:

No, there is no magical way of removing the log.debug() statements, BUT when you set the logging level to INFO, then as long as you are NOT doing heavy computations while passing the parameters to the debug() method, it should be fine. For example, if you have logger level set to INFO and assume you have got the below two loggers in your code:

logger.debug(" Entry:: "); //this logger is fine, no calculations //Below logger, you are doing computations to print i.e., calling to String methods logger.debug(" Entry : product:"+product+" dept:"+dept);//overhead toString() calls

我建议使用 slf4j ,以便通过使用{}(使用 MessageFormatter 替换为实际值),如下所示:

I recommend to use slf4j so that you can avoid the second logger computations overhead by using {} (which replaces with actual values using it's MessageFormatter) as shown below:

//Below logger product and dept toString() NOT invoked logger.debug(" Entry : product:{} dept{}", product, dept);

更重要的一点是,使用 slf4j只是一个抽象,您可以在任何日志记录框架之间切换,您可以在下面的文本中找到,这些文本取自此处.

One more important point is that with slf4j is just an abstraction and you can switch between any logging frameworks, you can look below text taken from here.

Java的简单日志记录外观(SLF4J)作为简单外观 或各种日志框架的抽象(例如java.util.logging, logback,log4j)允许最终用户插入所需的日志记录 框架在部署时.

The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.

更多推荐

log.debug是否会降低性能

本文发布于:2023-07-22 12:31:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1187535.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否会   性能   log   debug

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!