我应该在手术之前或之后登录吗?

编程入门 行业动态 更新时间:2024-10-24 18:28:23
本文介绍了我应该在手术之前或之后登录吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在考虑在某个操作周围将日志记录写入何处.这是两种不同的样式.第一个,在操作之前写日志.

I'm thinking about where to write the log record around an operation. Here are two different styles. The first one, write log before the operation.

之前:

log.info("Perform operation XXX") operation()

这是另一种样式,请在操作后写日志.

And here is a different style, write the log after the operation.

之后:

operation() log.info("Operation XXX is done.")

使用before样式时,日志记录会说出现在要做什么.这种风格的优点是,当出现问题时,开发人员可以很容易地检测到它,因为他们知道程序现在正在做什么.但是缺点是您不确定操作是否正确完成,例如,如果操作内部出现错误,例如,函数调用被阻塞在那里并且永远不会返回,则您永远无法通过读取日志记录来了解它.使用After样式,您可以确定操作已完成.

With the before-style, the logging records say what is going to do now. The pro of this style is that when something goes wrong, developer can detect it easily, because they know what is the program doing now. But the con is that you are not sure is the operation finished correctly, if something wrong is inside the operation, for example, a function call gets blocked there and never return, you can't never know it by reading the logging records. With the after-style, you are sure the operation is done.

当然,我们可以将这两种样式混合在一起

Of course, we can mix those two style together

两者:

log.info("Perform operation XXX") operation() log.info("Operation XXX is done.")

但是我觉得这有点冗长,它使记录重复.所以,这是我的问题-什么是好的日志记录样式?我想知道你的想法.

But I feel that is kinda verbose, it makes double logging records. So, here is my question - what is the good logging style? I would like to know how do you think.

推荐答案

我通常会使用两个不同的日志级别.

I'd typically use two different log levels.

我把第一个放在调试"级别,第二个放在信息"级别.这样,典型的生产机器将只记录正在执行的操作,但是我可以打开调试日志记录,并查看它在出错之前会尝试执行的操作.

The first one I put on a "debug" level, and the second one on an "info" level. That way typical production machines would only log what's being done, but I can turn on the debug logging and see what it tries to do before it errors out.

更多推荐

我应该在手术之前或之后登录吗?

本文发布于:2023-10-27 11:39:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:手术

发布评论

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

>www.elefans.com

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