Should I log before or after an operation?

Deadly 提交于 2019-12-10 14:08:06

问题


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.

Before:

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

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

After:

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

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

Both:

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.


回答1:


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.




回答2:


It all depends what you want to log. If you're interested in the code getting to the point where it's about to do an operation. If you want to make sure the operation succeeded, do it after. If you want both, do both.




回答3:


Maybe you could use something like a try catch ? Here 's a naive python example :

try :
    operation()
    log.info("Operation XXX is done.")
except Exception:
    log.info("Operation xxx Failed")
    raise Exception() # optional : if you want to propagate failure to another try catch statement and/or crash eventually. 

Operation will be launched. If it doesn't fail (no exception raised) you get a success statement in the logs.

If it fails (by raising an exception. Like disc full or whatever you are trying to do), Exception is caught and you get a failure statement.

Log is more meaning full. You get to keep the verbosity to a oneliner and get to know if operation succeeded. Best of all choices.

Oh and you get a hook point where you can add some code to be executed in case of failure.

I hope it help.




回答4:


There's another style that I've seen used in Linux boot scripts and in strace. It's got the advantages of your combined style with less verbosity, but you've got to make sure that your logging facility isn't doing any buffering. I don't know log.info, so here's a rough example with print:

print "Doing XXX... ",  # Note lack of newline :)
operation()
print "Done."

(Since in most cases print uses buffering, using this example verbatim won't work properly. You won't see "Doing XXX" until you see the "Done". But you get the general idea.)

The other disadvantage of this style is that things can get mixed up if you have multiple threads writing to the same log.



来源:https://stackoverflow.com/questions/4577376/should-i-log-before-or-after-an-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!