Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

May the Logs Be With You!

DZone 's Guide to

May the Logs Be With You!

Join the not-so-dark side of programming and learn more about how logging can help you and others decipher your code.

· DevOps Zone ·
Free Resource

A very important practice that most developers don't take under consideration is logging. But once they find themselves spending hours debugging others' code or even their own old code, they come to realize the importance of logging and how useful it is.

What is Logging?

Logging is the additional minutes you spend while coding to generate messages that track the events in your code. The logs (messages) may be oriented to future developers responsible for the maintenance and evolutions, or to yourself to help you understand the behavior of the code quickly and easily, especially when an error occurs.

Surely most programming languages come with their stack trace, but counting on that alone is not enough.

What Do You Need to Log?

Logs can be classified into four big types:

  • Diagnostic logs: This type of log is the most important one, as the main purpose behind using logs is to help developers identify easily the root cause of an error once it occurs. So, as you may have already deduced, you need to log the errors and describe them clearly. 

  • Audit logs: The previous type helps in locating and solving the errors in your code. This one helps with understanding it. These logs describe the main steps of your code, and they mention the functional steps related to the business of your application. For example, you can have a log message indicating that a transaction is accepted at an e-commerce website.

  • Profiling logs: these logs indicate the start and the end of your operations. For example, the start of the execution of a particular function and its end. These logs may be helpful to track the execution branches in your code and to eliminate some parts of the code from the analysis to make once an error occurs as you know that particular part has ended successfully.

  • Statistics logs:  The idea is to log messages describing the performance of your code like execution time or the memory consumed by your process. I consider these logs as optional, but in some cases, they may be very helpful to evaluate your code.

Tips to Make Quality Logs

The following recommendations are based on my own experience and they are not the only ones you can get.

  • Make sure to use understandable messages in your logs and especially meaningful for the ones they are oriented to: a database administrator needs the different logs of a regular developer, and they all have to make sense.

  • "Meaningful messages" doesn't mean writing long phrases; keep it simple but clear.

  • This tip I got from the Python PEP 8. It was designated for comments, but I find it also suitable for logs. Use English. You can write in your native language only if you are sure that no one who does not speak it will have to read your logs.

  • Do not print your logs on the console output or write them using IO operation in a file. Use a framework or a module designated to logs. Many programming languages offer logging modules in their standard libraries. For Pythonfor example, it's called logging and it can be imported directly without further installation.

  •  Use structured logs: structured logs are logs with a fixed format easily parsed by a machine. This offers you flexibility when you have to write code to analyze your logs to send emails to users, for example, with the description of an error once it occurs.

  • Classify your logs depending on their severity: For example, by default, the Python logging module offers five levels of severity: Trace, Debug, Info, Error, Fatal.

Logs are worth spending time on; remember you get what you give, so do not hesitate to use them!

Have a nice day :)  

 

Topics:
open source ,best practices ,logging ,python ,devops

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}