Home >Backend Development >Golang >Golang: Importance of planning how to display logs in high-use applications

Golang: Importance of planning how to display logs in high-use applications

DDD
DDDOriginal
2024-10-11 10:09:30785browse

Most of the time we don't spend the time necessary to correctly plan our application logs, which later causes us to rework, often unnecessary.

The well-planned and applied use of logs can, in addition to improving future analysis of a problem, significantly impact the performance of the application, especially for intense processes.

Thinking about exemplifying and demonstrating a little of this impact, here are two examples with their benchmark, respectively.

The first of a common way to "log" information in the code, but little information value, becomes just data:

benchmark_logs_test.go
Golang: Importância de planejar como exibir logs em aplicações de uso intenso

Benchmark:
Golang: Importância de planejar como exibir logs em aplicações de uso intenso

And the second with the structured log, only showing at the end the start and end times and the message.

benchmark_logs_enhancement_test.go
Golang: Importância de planejar como exibir logs em aplicações de uso intenso

Benchmark:
Golang: Importância de planejar como exibir logs em aplicações de uso intenso

Both examples have a loop of one hundred thousand indexes, which using goroutines, increments a variable.
When starting, the current time is captured, and when finished, the time it took is printed.
I'm using the logrus log library, as it has numerous interesting properties and functionality, such as structured logging.

You can clearly see the difference that a log that is not thought out correctly can cause in the performance of applications.

The above is the detailed content of Golang: Importance of planning how to display logs in high-use applications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn