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);
Mycila::Logger.forwardTo(&Serial);

WebSerial.begin(...);
Mycila::Logger.forwardTo(&Serial);

Then use the logger:

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

if(Mycila::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::LoggerClass::getLevel() const { return ARDUHAL_LOG_LEVEL; }

Example by using a configuration system:

uint8_t Mycila::LoggerClass::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.