A logging library for ESP32 that saves logs to LittleFS and provides console output with detailed formatting. Features non-blocking queue-based logging, configurable log levels, and callback support.
- Non-blocking queue-based logging system
- File logging to LittleFS with automatic rotation
- Console output with timestamps and core ID
- Configurable log levels for console and file
- Log counters and queue monitoring
- Callback support for custom log handling
- Conditional compilation flags for production builds
Add to your platformio.ini file:
lib_deps = jijio/AdvancedLoggerSearch for "AdvancedLogger" in the Library Manager.
Download the latest release from the releases page.
Tested on: ESP32S3, ESP-WROVER
#include <AdvancedLogger.h>
void setup() {
Serial.begin(115200);
AdvancedLogger::begin();
LOG_INFO("System started");
LOG_ERROR("Error occurred: %d", 42);
}
void loop() {
LOG_DEBUG("Loop iteration: %lu", millis());
delay(5000);
}Output format:
[2024-03-23T09:44:10.123Z] [1 450 ms] [INFO ] [Core 1] [main.cpp:setup] System started
[2024-03-23T09:44:10.456Z] [1 783 ms] [ERROR ] [Core 1] [main.cpp:setup] Error occurred: 42
Set different log levels for console output and file logging:
AdvancedLogger::setPrintLevel(LogLevel::DEBUG); // Console output
AdvancedLogger::setSaveLevel(LogLevel::WARNING); // File loggingAvailable levels: VERBOSE, DEBUG, INFO, WARNING, ERROR, FATAL
Customize the logging queue before including the header:
#define ADVANCED_LOGGER_ALLOCABLE_HEAP_SIZE (20 * 1024) // 20KB heap
#define ADVANCED_LOGGER_TASK_STACK_SIZE (8 * 1024) // 8KB stack
#define ADVANCED_LOGGER_TASK_PRIORITY 2 // Task priority
#define ADVANCED_LOGGER_MAX_MESSAGE_LENGTH 512 // Max message size
#include "AdvancedLogger.h"Or in platformio.ini:
build_flags =
-DADVANCED_LOGGER_ALLOCABLE_HEAP_SIZE=20480
-DADVANCED_LOGGER_TASK_STACK_SIZE=8192
-DADVANCED_LOGGER_MAX_MESSAGE_LENGTH=512Configure when log files are flushed to ensure data persistence:
#define ADVANCED_LOGGER_FLUSH_INTERVAL_MS 5000 // Flush every 5 seconds (default)
#define ADVANCED_LOGGER_FLUSH_LOG_LEVEL LogLevel::ERROR // Log level that triggers immediate flush (default)
#include "AdvancedLogger.h"The library automatically flushes files periodically and on the specified log level to prevent data loss during power cycles or crashes.
Register a function to handle log entries:
void logHandler(const LogEntry& entry) {
// Send to server, display on screen, etc.
Serial.printf("Callback: %s\n", entry.message);
}
void setup() {
AdvancedLogger::setCallback(logHandler);
AdvancedLogger::begin();
}Check system status:
unsigned long available = AdvancedLogger::getQueueSpacesAvailable();
unsigned long waiting = AdvancedLogger::getQueueMessagesWaiting();
unsigned long dropped = AdvancedLogger::getDroppedCount();
LOG_INFO("Queue: %lu available, %lu waiting, %lu dropped", available, waiting, dropped);// Set max lines before auto-rotation
AdvancedLogger::setMaxLogLines(5000);
// Get current line count
unsigned long lines = AdvancedLogger::getLogLines();
// Clear log (keep 20% of recent entries)
AdvancedLogger::clearLogKeepLatestXPercent(20);
// Dump to Serial
AdvancedLogger::dump(Serial);Disable specific log levels to reduce binary size:
#define ADVANCED_LOGGER_DISABLE_VERBOSE
#define ADVANCED_LOGGER_DISABLE_DEBUG
#define ADVANCED_LOGGER_DISABLE_CONSOLE_LOGGING // Disable all console output
#define ADVANCED_LOGGER_DISABLE_FILE_LOGGING // Disable all file logging
#include "AdvancedLogger.h"LOG_VERBOSE(format, ...)- Most detailed loggingLOG_DEBUG(format, ...)- Debug informationLOG_INFO(format, ...)- General informationLOG_WARNING(format, ...)- Warning messagesLOG_ERROR(format, ...)- Error conditionsLOG_FATAL(format, ...)- Fatal errors
AdvancedLogger::begin(path)- Initialize loggerAdvancedLogger::end()- Clean up resourcesAdvancedLogger::setPrintLevel(level)- Set console log levelAdvancedLogger::setSaveLevel(level)- Set file log levelAdvancedLogger::setCallback(callback)- Register log handler
getVerboseCount(),getDebugCount(),getInfoCount()getWarningCount(),getErrorCount(),getFatalCount()getTotalLogCount(),resetLogCounters()
setMaxLogLines(count)- Set rotation thresholdgetLogLines()- Get current line countclearLog()- Delete all logsclearLogKeepLatestXPercent(percent)- Rotate logsdump(stream)- Output logs to stream
See the examples folder for complete usage examples.
Fork the repository, create a feature branch, and submit a pull request. All contributions are welcome.
This project is licensed under the MIT License - see the LICENSE file for details.