logging - dl3ebb/OpenIot GitHub Wiki

02 Logging

Log output during development can easily be sent to the serial console. However, once the device is deployed in the field, accessing the log can become challenging. To address this, OpenIot provides a flexible logging interface that can be adapted to the specific environment.

In my case, I use a centralized log server running a syslog service.

The logging interface supports four log levels: DEBUG, INFO, WARN, and ERROR. The DEBUG level is intended solely for development purposes and is not logged in the production environment.

The following example demonstrates how to configure and use the logging interface.

/**
 * Demo of Serial Logging
 */

#define SWITCH_PIN 25
#define LED_PIN    32

#include <OpenIot.h>
#include <Connector/DigitalInConnector.h>
#include <Connector/DigitalOutConnector.h>
#include <Logger/SerialLogger.h>

void setup() {
    serialLogger->setBaudrate(115200);
    openIot.setLogger(serialLogger);
    
    /**
     * Create the Elements and register them to the connectors
     */
    BoolElement *switch1 = new BoolElement("Switch1");
    digitalInConnector->registerElement(switch1, SWITCH_PIN, true);

    BoolElement *led1 = new BoolElement("Led1");
    digitalOutConnector->registerElement(led1, LED_PIN);

    eventManager.addListener<NewBoolValueEvent>(NewBoolValue, "Switch1", [led1](NewBoolValueEvent *event) {
        led1->setValue(event->newValue);
        logger->info (F("LED new Value : %s"), event->newValue ? "true" : "false");
    });

    openIot.setup("OpenIotLogging", 1);
}

void loop() {
    openIot.loop();
}

This example demonstrates how to use the serial logger. It is initialized by specifying the baud rate and assigning the logger to the OpenIot core.

serialLogger->setBaudrate(115200);
openIot.setLogger(serialLogger);

Logging to the system is performed by calling one of the methods error, warn, info, or debug on the global logger object.

logger->info (F("LED new Value : %s"), event->newValue ? "true" : "false");

The logger object internally uses the vsnprintf method. The string can include any standard printf placeholders and accept any number of arguments. The methods only accept the __FlashStringHelper object for the format string, as the strings should be stored in flash memory anyway.

The syslog feature is explained in a separate example: 09 SyslogLogger.


| ← Previous Page (Hello OpenIot) | ↑ Tutorial Main Page | Next Page (Wi-Fi) → |

⚠️ **GitHub.com Fallback** ⚠️