ESP32 Tutorial – ESP_LOGx


Introduction

In ESP-IDF of ESP32, esp_logx is a function used to record LOG information. The ESP_LOGx function is a macro used to simplify the debugging of LOG records and provide different levels of LOG output.

ESP_LOGx Levels

ESP_LOGE: records error information, usually indicating that the program has encountered some problems that need to be dealt with immediately.
ESP_LOGW: Log warning information, indicating that the program may have potential problems, but it will not immediately affect functionality.
ESP_LOGI: records common information, indicating the normal operating status or status information of the program.
ESP_LOGG: records debugging information, usually used in the development phase, including more detailed running details.
ESP_LOGE: Logs very detailed debugging information, usually used for the most granular debugging.

How to use ESP_LOGx

ESP_LOGx is a macro that actually calls different functions based on different LOG levels. Its basic syntax is as follows...
ESP_LOGx(level, tag, format, ...)
level: LOG level, such as ESP_LOG_INFO.
tag: tag used to identify the source of the LOG.
format: format string, similar to printf format.
...: The variable part in the format string.

Setting ESP_LOGx levels

Static Configuration

In the sdkconfig file you can set the global LOG level. For example.
CONFIG_LOG_DEFAULT_LEVEL_INFO=y
Dynamic Configuration

In the program, you can also use the esp_log_level_set function to dynamically set the LOG level, for example...
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include "esp_log.h"

// Define the log tag for this component
static const char *TAG = "MY_TAG";

void app_main() {
    // Set the global log level to INFO
    esp_log_level_set("*", ESP_LOG_INFO);

    // This DEBUG message will not be shown because global level is INFO
    ESP_LOGD(TAG, "This DEBUG message will not be shown.");

    // This INFO message will be shown
    ESP_LOGI(TAG, "This INFO message will be shown.");
}
Notice

esp_log_level_set() cannot set the LOG level higher than the level set by CONFIG_LOG_MAXIMUM_LEVEL. To increase the LOG level of a specific file above this maximum level at compile time, use the LOG_LOCAL_LEVEL macro.

Install VSCode & ESP-IDF VSCode Extension

Make sure you have installed and configured the ESP-IDF development environment. You can also refer to the article the ESP32 Tutorial – ESP-IDF With VSCode.

Create A New ESP32 Project

We can use VSCode's IDF plug-in to create a new ESP32 project. Please refer to ESP32 Tutorial – How To Create An ESP32 Project With VSCode.

ESP_LOGx Example

#include "esp_log.h"

static const char *TAG = "APP_TAG";

void app_main() {
    ESP_LOGE(TAG, "This is an error message: %s", "Something went wrong");
    ESP_LOGW(TAG, "This is a warning message");
    ESP_LOGI(TAG, "This is an info message");
    ESP_LOGD(TAG, "This is a debug message with value: %d", 100);
    ESP_LOGV(TAG, "This is a verbose message with string: %s", "Verbose info");
}

Compile, Flash & Monitor

Find the "ESP32-IDF: Build, Flash and Monitor" ICON in VSCode to execute and burn.

Results

We can view the printing results on the terminal, as follows...
I (1000) APP_TAG: This is an info message
D (1001) APP_TAG: This is a debug message with value: 100
V (1002) APP_TAG: This is a verbose message with string: Verbose info
E (1003) APP_TAG: This is an error message: Something went wrong
W (1004) APP_TAG: This is a warning message

Conclusion

ESP_LOGx provides flexible LOG recording function, allowing you to control LOG output according to LOG level and label. By configuring LOG levels and using different LOG macros, you can get the information you need during development and dynamically adjust LOG output at runtime.