ESP-IDF Logging: How great is ‘ESP_LOGx’ vs ‘printf’

MarkEye
5 min readApr 14, 2022

--

Logs are a very useful tool every developer very well knows it. Whenever the program does not run as expected, our next step will be to put logs that print out information, which will help us in debugging the code. Using it we get a resolution of our problem and the reason for the problem.

In ESP-IDF we have two methods for logging one is ‘printf’ and the second one is ‘ESP_LOGx’, Between ‘printf’ vs ‘ESP_LOGx’ my choice is ESP_LOGx. Because it has many rich features like “setting log level”, “Tag for identification” and “different log type with different colours” which makes ESP_LOGx my choice.

Feature 1: Set log level

ESP_LOGx has a total of five macros which are shown below.

  • ESP_LOGE - error (lowest)
  • ESP_LOGW - warning
  • ESP_LOGI - info
  • ESP_LOGD - debug
  • ESP_LOGV - verbose (highest)

In ESP_LOGx ‘x’ is used for E, W, I, D, and V which represent the above five macros. ESP-IDF has a total of six log levels, the sixth one is No output which disables all the logs that print using ESP_LOGx macro. So using log level we can enable or disable logs and can control some specific logs, like when the log level is lowest only the Error log will print. One step above that level Error and Warning logs will print and if we set the log level highest all the logs will print out.

How to set the log level

Type idf.py menuconfig and go to Component config -> Log output -> Default log verbosity .

ESP-IDF Logging: set log level at configuration screenshot
ESP-IDF Logging: set log level at configuration

Default log verbosity is set all the log levels at the selected level. For testing what happened at different log level writes the below code.

#include "esp_log.h"static const char *TAG = "MAIN";void app_main(void) {ESP_LOGE(TAG, "This is error log");ESP_LOGW(TAG, "This is warning log");ESP_LOGI(TAG, "This is info log");ESP_LOGD(TAG, "This is debug log");ESP_LOGV(TAG, "This is verbos log");}

esp_log.h component has ESP_LOGx macros so for use first include it and write some sample logs in the main function as above. Now idf.py build -> idf.py flash -> idf.py monitor and check out the output. Below is the output when Default log verbosity set at Error.

ESP-IDF Logging: set log level at Error console output screenshot
ESP-IDF Logging: set log level at Error console output screenshot

Below is the output when Default log verbosity set at Verbos.

ESP-IDF Logging: set log level at Verbos console output screenshot
ESP-IDF Logging: set log level at Verbos console output screenshot

Below is the output when Default log verbosity set at No output.

ESP-IDF Logging: set log level at No output console output screenshot
ESP-IDF Logging: set log level at No output console output screenshot

Feature 2: Tag every log

The bigger project has many files and sometimes we can’t able identify in which file the printed log is put. So we put identity with logs that tell us this log is from this file eg. printf("xyz-file: some text")when this syntax executes, it prints with a file name that makes it easy to identify from which this is executed. And sometimes we put crazy logs like eg. printf("Inside xyz function") , printf("Inside if") and printf("Inside else") . The purpose of this log is to know whether our code is actually going inside a ‘xyz function’ or not, and what condition is executing ‘if’ or ‘else’.

So for the trick, that we used with pritnf,ESP_LOGx has a TAG mechanism. All the logs which are printed using ESP_LOGx micro have ‘TAG’. ‘TAG’ could be any string, so using this ‘TAG’ we can identify the log, like from which file or function this log is printed. As we use MAIN as the tag above so all the log is printed beside MAIN.

Feature 3: Different colours for different logs type

ESP-IDF Logging: sample logs using ESP_LOG screenshot
ESP-IDF Logging: sample logs using ESP_LOG

As we can see here, the log is printed in different colours. Different macro will print in different colours. ‘Error’ macro printed in red indicates serious problems that easily draw our attention and give information about it. So any NULL checking or error type log we can print using ‘ESP-LOGE’ macro.
Below ‘Error’ macro, ‘Warning’ macro is printed in orange, it shows less problematic information that we can ignore. That type of logs we can print using ‘ESP_LOGW’ macro
The ‘Info’ macro is printed in green. The green colour indicates the normal conditions. So any logs like variable values and some text or information can be printed using the ‘ESP-LOGI’ macro. Same to same we can use Debug and Verbos macros which are printed in white colour.

Note:

In production firmware, we can not put logs, because anyone can see these logs and could get confidential information which makes our product vulnerable. So for removing logs we have two ways by commenting on them or by removing them from code. But at repairing time it will create issues. Because we are not able to get any information by logs. So it creates difficulty in knowing device conditions. But if we put all the logs using ESP_LOGx macro in code, then we can disable all the logs using setting the log level to the No output. And at maintenance time can enable all the logs using the setting log level.

I hope this article will add value to your knowledge and make your next project fancier with coloured and tagged logs. If you like this and want more please follow.

Thank you for reading.

--

--