sysloglogger - dl3ebb/OpenIot GitHub Wiki

SyslogLogger

While the serial console log output is sufficient during the development process, it becomes challenging —if not impossible— when devices are deployed in the field.

To address this problem, OpenIot includes a syslogLogger that redirects log outputs to a central syslog server. This allows you to filter and archive logs using the syslog's built-in mechanisms.

The following example demonstrates how to use the syslogLogger:

/**
 * Demo of the Syslog Logger 
 */

#define SWITCH_PIN 25
#define LED_PIN    32
#define WLAN_SSID   "your_ssid"
#define WLAN_PASSWD "your_passwd"
#define SYSLOG_SERVER "your.syslog.server"

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

#include <Modules/Wifi.h>

void setup() {
    serialLogger->setBaudrate(115200);

    syslogLogger->setEnable(true);
    syslogLogger->setLogServer(SYSLOG_SERVER);
    syslogLogger->setBackupLogger(serialLogger);
    openIot.setLogger(syslogLogger);
    
    wifi->setEnablePrimary(true);
    wifi->setSsid1(WLAN_SSID);
    wifi->setPassword1(WLAN_PASSWD);

    /**
     * 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("OpenIotSysLog", 1);
}

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

Configuring the Syslog Server

The syslog server is a machine running a syslog daemon configured to accept log messages from external devices. In our Management server, such a service is set up.

Setting Up the syslogLogger

The syslogLogger can be configured with the following four lines of code:

syslogLogger->setEnable(true);
syslogLogger->setLogServer(SYSLOG_SERVER);
syslogLogger->setBackupLogger(serialLogger);
openIot.setLogger(syslogLogger);
  • Enable the logger: Activates the syslog functionality.
  • Set the syslog server: Specifies the server to which the log messages will be sent.
  • Specify a backup logger: If the syslog server becomes unreachable, the log messages will be sent to the backup logger (e.g., the serial console).
  • Register the logger: Registers the syslogLogger with OpenIot.

Viewing Logs

Once the device starts, you can view the log messages in the syslog of the designated log server.

Key Notes

Log messages from the program are initially stored in a queue. These messages are sent to the log server line by line every 30 ms in the background. This approach ensures that sending logs does not slow down the application's execution.


| ← Previous Page (Back to MQTT Status) | ↑ Tutorial Main Page | Next Page (Web Frontend) → |

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