EspLogger provides ESP32-specific logging functionality using ESP-IDFโs esp_log system.
It implements the BaseLogger interface with support for both ESP-IDF Log V1 and Log V2 systems,
offering comprehensive logging capabilities with performance monitoring and multi-output support.
Features
ESP-IDF Integration - Full support for ESP-IDF Log V1 and Log V2 systems
Multi-Output Support - Console, file, and network logging
Performance Monitoring - Built-in statistics and performance tracking
Thread Safety - Mutex-protected operations for multi-threaded access
classEspLogger:publicBaseLogger{public:// Constructor with configurationexplicitEspLogger(consthf_logger_config_t&config)noexcept;// Destructor with proper cleanup~EspLogger()noexceptoverride;// BaseLogger interface implementationboolInitialize()noexceptoverride;boolDeinitialize()noexceptoverride;// Logging operationshf_logger_err_tLog(hf_log_level_tlevel,constchar*tag,constchar*format,...)noexceptoverride;hf_logger_err_tLogV(hf_log_level_tlevel,constchar*tag,constchar*format,va_listargs)noexceptoverride;// Level managementhf_logger_err_tSetLevel(hf_log_level_tlevel)noexceptoverride;hf_log_level_tGetLevel()constnoexceptoverride;// Tag managementhf_logger_err_tSetTagLevel(constchar*tag,hf_log_level_tlevel)noexceptoverride;hf_log_level_tGetTagLevel(constchar*tag)constnoexceptoverride;// Output managementhf_logger_err_tAddOutput(hf_logger_output_toutput_type,constchar*output_config)noexceptoverride;hf_logger_err_tRemoveOutput(hf_logger_output_toutput_type)noexceptoverride;// Performance and diagnosticshf_logger_err_tGetStatistics(hf_logger_statistics_t&statistics)noexceptoverride;hf_logger_err_tGetDiagnostics(hf_logger_diagnostics_t&diagnostics)noexceptoverride;hf_logger_err_tResetStatistics()noexceptoverride;// ESP-IDF specific featureshf_logger_err_tSetEspLogLevel(esp_log_level_tlevel)noexcept;esp_log_level_tGetEspLogLevel()constnoexcept;hf_logger_err_tEnableEspLogV2(boolenable)noexcept;boolIsEspLogV2Enabled()constnoexcept;};
#include"mcu/esp32/EspLogger.h"// Configure loggerhf_logger_config_tconfig={};config.default_level=hf_log_level_t::LOG_INFO;config.enable_esp_log_v2=true;config.output_types=hf_logger_output_t::OUTPUT_CONSOLE;// Create and initialize loggerEspLoggerlogger(config);if(!logger.EnsureInitialized()){printf("Failed to initialize logger\n");return;}// Basic logginglogger.Log(hf_log_level_t::LOG_INFO,"APP","Application started");logger.Log(hf_log_level_t::LOG_ERROR,"APP","Error occurred: %d",error_code);logger.Log(hf_log_level_t::LOG_DEBUG,"APP","Debug info: %s",debug_string);
Tag-Based Logging
1
2
3
4
5
6
7
8
9
10
11
12
EspLoggerlogger(config);logger.EnsureInitialized();// Set different levels for different tagslogger.SetTagLevel("APP",hf_log_level_t::LOG_INFO);logger.SetTagLevel("SENSOR",hf_log_level_t::LOG_DEBUG);logger.SetTagLevel("NETWORK",hf_log_level_t::LOG_WARN);// Log with different tagslogger.Log(hf_log_level_t::LOG_INFO,"APP","Application initialized");logger.Log(hf_log_level_t::LOG_DEBUG,"SENSOR","Sensor reading: %.2f",sensor_value);logger.Log(hf_log_level_t::LOG_WARN,"NETWORK","Network timeout");
Multi-Output Logging
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Configure multi-output loggerhf_logger_config_tconfig={};config.default_level=hf_log_level_t::LOG_INFO;config.output_types=hf_logger_output_t::OUTPUT_CONSOLE|hf_logger_output_t::OUTPUT_FILE;strcpy(config.file_config,"/spiffs/logs/app.log");EspLoggerlogger(config);logger.EnsureInitialized();// Add network outputlogger.AddOutput(hf_logger_output_t::OUTPUT_NETWORK,"udp://192.168.1.100:514");// Log to all outputslogger.Log(hf_log_level_t::LOG_INFO,"APP","Multi-output logging active");
EspLoggerlogger(config);logger.EnsureInitialized();// Set global levellogger.SetLevel(hf_log_level_t::LOG_WARN);// Set tag-specific levelslogger.SetTagLevel("CRITICAL",hf_log_level_t::LOG_ERROR);logger.SetTagLevel("DEBUG",hf_log_level_t::LOG_DEBUG);// Log with different levelslogger.Log(hf_log_level_t::LOG_INFO,"APP","This won't be logged (level too low)");logger.Log(hf_log_level_t::LOG_WARN,"APP","This will be logged");logger.Log(hf_log_level_t::LOG_ERROR,"CRITICAL","Critical error");logger.Log(hf_log_level_t::LOG_DEBUG,"DEBUG","Debug info");// Get current levelshf_log_level_tglobal_level=logger.GetLevel();hf_log_level_tcritical_level=logger.GetTagLevel("CRITICAL");printf("Global level: %d, Critical level: %d\n",global_level,critical_level);
Log Levels
Level
Value
Description
|โโ-|โโ-|โโโโ-|
LOG_NONE
0
No logging
LOG_ERROR
1
Error messages only
LOG_WARN
2
Warning and error messages
LOG_INFO
3
Informational, warning, and error messages
LOG_DEBUG
4
Debug, info, warning, and error messages
LOG_VERBOSE
5
All messages including verbose
Output Types
Output Type
Description
|โโโโ-|โโโโ-|
OUTPUT_CONSOLE
Standard console output
OUTPUT_FILE
File-based logging
OUTPUT_NETWORK
Network-based logging (UDP/TCP)
Performance Characteristics
Log Latency: <10ยตs per log entry
Memory Usage: ~2KB base + 1KB per output
Throughput: >10,000 logs/second
Thread Safety: Full mutex protection
ESP-IDF Integration: Native performance
Error Handling
The EspLogger class provides comprehensive error reporting through the hf_logger_err_t
enumeration:
LOGGER_SUCCESS - Operation completed successfully
LOGGER_ERR_NOT_INITIALIZED - Logger not initialized