php editor Zimo brings you an article about logrus output. During the development process, we often need to output logs to help us troubleshoot problems and track the code execution process. Logrus is a powerful logging library that can output logs in different formats, such as JSON, text or custom formats. This article will introduce the different formats of logrus output logs, help developers choose the appropriate output format according to their needs, and improve the readability and usability of logs.
My program uses logrus
in a basic way, without any configuration:
<code> logrus.Info("...") </code>
But in different places, it is output in different formats, some places such as:
INFO[0016] pushed
There are also some places, such as:
time="2023-11-30T05:26:39Z" level=info msg=pushed
I don’t know what’s the mystery behind it?
Let me answer this question myself. I looked at the logrus
code over the weekend and noticed something tricky.
logrus
There is a mechanism to detect whether the current terminal has color. If so, it will be output in the format of INFO[0000] Pushed
, otherwise it will be output in the format of time= "2023-11-30T05:26:39Z" level= output information in the format msg=pushed
.
So if you want to always output in the first format (shorter) then you just set the forced color:
logrus.SetFormatter(&logrus.TextFormatter{ ForceColors: true, })
If you want the second format, just force disable the colors:
logrus.SetFormatter(&logrus.TextFormatter{ DisableColors: true, })
You can also configure the time format.
The above is the detailed content of Logrus output in different formats. For more information, please follow other related articles on the PHP Chinese website!