DEV Community

Cover image for How to make logs useful?
Ismaël Maurice
Ismaël Maurice

Posted on

How to make logs useful?

How to make logs useful?

As developers, logs are the expressive part of our application. They allow us to capture the activity of an application.

In computer programming, a developer can decide whether to display logs in his application, which can be a traceability aid in the event of errors.

The problem now is to know what to display and where, hence the purpose of this article.

Logs in a single function

By definition, a function receives one or more parameters as input, then performs operations and returns a result.

Function architecture

In a simple function, the useful information's to be displayed in the logs are:

  • The parameter(s) the function receives.
  • Result after operations (optional, useful when you have a sequence of function calls).

For example, for a function that takes no parameters and returns the message 'Hello' we would have in Python:

Log of a function without parameters

Suppose the function took a name as parameter and returned “Hello username”, we'd have:

Log function with parameters

If we want to display the result of the function in the logs we would have:

Log function with parameters and result

When parameters contain confidential information such as email and password, these should not be displayed in the logs and should be replaced by **.

Logs in a sequence of functions

A computer program is a sequence of functions and methods that call each other. Let's assume a program consisting of 3 functions: function A, function B and function C, with the following stack:

Multi-function call architecture

When you have several functions, the logs should look like this:

  • The calling function must display its parameters when called.
  • After executing another function, the calling function must display the result obtained.
  • The calling function can display the returned result.

Let's say a main function calls a getName function and, with the name entered by the user, calls a hello function to display the message 'Hello username'.

If we want to apply the logs in this sequence of functions, we would have:

Log of a sequence of functions

Execution would give :

Log d'une suite de fonctions exécution

In the previous examples, we used the print function to display logs. In your projects, you should use log modules from your own language. In Python, you can use Logging.

The techniques described above can be adapted to suit your needs.

When parameters contain confidential information such as email and password, these should not be displayed in the logs and should be replaced by **.

Top comments (0)