What to write into log file?

后端 未结 7 1436
面向向阳花
面向向阳花 2020-12-28 09:28

My question is simple: what to write into a log. Are there any conventions? What do I have to put in?

Since my app has to be released, I\'d like to have friendly log

7条回答
  •  轮回少年
    2020-12-28 09:50

    Here are some suggestions for content:

    • timestamp
    • message
    • log message type (such as error, warning, trace, debug)
    • thread id ( so you can make sense of the log file from a multi threaded application)

    Best practices for implementation:

    • Put a mutex around the write method so that you can be sure that each write is thread safe and will make sense.
    • Send 1 message at at a time to the log file, and specify the type of log message each time. Then you can set what type of logging you want to take on program startup.
    • Use no buffering on the file, or flush often in case there is a program crash.

    Edit: I just noticed the question was tagged with Python, so please see S. Lott's answer before mine. It may be enough for your needs.

提交回复
热议问题