Skip to the content.

MycilaLogger

License: MIT Continuous Integration PlatformIO Registry

A simple and efficient logging library for Arduino / ESP32.

Basic Usage

First setup the outputs where to forward the logs in the setup() method:

Serial.begin(115200);
logger.forwardTo(&Serial);

WebSerial.begin(...);
logger.forwardTo(&Serial);

Then use the logger:

logger.debug(TAG, "Published in %u ms", (millis() - start));
logger.info(TAG, "Published in %u ms", (millis() - start));
logger.warn(TAG, "Published in %u ms", (millis() - start));
logger.error(TAG, "Published in %u ms", (millis() - start));

if(logger.isDebugEnabled()) {
  // some expensive debug code
}

will output something like this:

D      8102600 loopTask   (1)  WEBSITE Published in 38 ms

Note: the logging level can be controlled with the standard Arduino flag: -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG for example.

Advanced Usage: Dynamic logging level

Define -D MYCILA_LOGGER_CUSTOM_LEVEL and implement the getLevel() method. This method must return the current logging level used in your app.

Example by re-using ARDUHAL_LOG_LEVEL:

uint8_t Mycila::Logger::getLevel() const { return ARDUHAL_LOG_LEVEL; }

Example by using a configuration system:

uint8_t Mycila::Logger::getLevel() const {
  return Mycila::Config.getBool(KEY_DEBUG_ENABLE) ? ARDUHAL_LOG_LEVEL_DEBUG : ARDUHAL_LOG_LEVEL_INFO;
}

Tips

Thread safety

The logging code itself is thread-safe and can be called from multiple tasks and cores.

There is no locking strategy in place: the logging methods are delegating to the underlying Print implementations.