Logging library for platformio
  • C++ 97.6%
  • C 2.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-09-07 23:13:33 +01:00
.vscode Copy logging code over from environmental sensor project 2026-09-04 18:49:04 +01:00
examples Add examples 2026-09-07 22:56:57 +01:00
include Copy logging code over from environmental sensor project 2026-09-04 18:49:04 +01:00
src Copy logging code over from environmental sensor project 2026-09-04 18:49:04 +01:00
library.json Add library.json 2026-09-07 23:13:33 +01:00
README.md Add versioning details to README 2026-09-07 23:04:29 +01:00

Logging

This is a logging library for PlatformIO projects. I use it across all my PlatformIO projects.

To use this library, add the following to your platformio.ini file. Replace VERSION with the version tag you want to use, for example v1.0.0.

lib_deps =
	git+https://code.ggrainger.uk/ggrainger/platformio-logging.git#VERSION

Omitting the version tag will use the latest version of the library. This is not recommended as any breaking changes may affect your project.

Usage

A few different types of loggers are provided, some examples are shown below.

Stream Logger

The stream logger is a simple logger that logs to a given Stream. It's most useful for logging to Serial, an example of it's use is shown below.

StreamLogger logger;

void begin() {
    Serial.begin(9600);
    logger.begin(&Serial);
}

void loop() {
    logger.info("Loop", "Hello World");
}

File Logger

The file logger logs to a folder on a given FileSystem, it is generally used to write logs to an SD card. Log files are automatically rolled every time the device is reset, each boot generates a new log file with an incrementing counter.

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <Logging.h>

#define SPI_SCK 12
#define SPI_MOSI 13
#define SPI_MISO 14
#define SPI_SD_CS 15

FileLogger logger;

void setup()
{
    Serial.begin(9600);

    // Initialize SPI for SD card
    Serial.println("Initializing SPI...");
    SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
    Serial.println("SPI initialized.");

    // Initialize the SD card and set up the logger
    Serial.println("Initializing SD card...");
    if (SD.begin(SPI_SD_CS, SPI, 4000000, "/sd", 5, false))
    {
        logger.begin(&SD, "/logs", LogLevel::Information);
        Serial.println("SD card initialized.");
        logger.info("Setup", "Logging to SD card");
    }
    else
    {
        Serial.println("Failed to initialize SD card");
    }
}

void loop()
{
    logger.info("Loop", "Hello World!");
    delay(1000);
}

Aggregate Logger

The aggregate logger allows you to log to multiple loggers at once. The example below shows logging to an SD card and the Serial interface.

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>
#include <Logging.h>

#define LOG_LEVEL LogLevel::Information
#define SPI_SCK 12
#define SPI_MOSI 13
#define SPI_MISO 14
#define SPI_SD_CS 15

StreamLogger serialLogger;
FileLogger fileLogger;
AggregateLogger logger;

void setup()
{
    // Initialise aggregate logger
    logger.begin(LOG_LEVEL);

    // Create serial logger
    Serial.begin(9600);
    serialLogger.begin(&Serial, LOG_LEVEL);
    serialLogger.info("Setup", "Starting up...");
    logger.addLogger(&serialLogger);

    SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);

    // Initialise file logger
    if (SD.begin(SPI_SD_CS, SPI, 4000000, "/sd", 5, false))
    {
        fileLogger.begin(&SD, "/logs", LOG_LEVEL);
        logger.addLogger(&fileLogger);
        serialLogger.info("Setup", "Logging to SD card");
    }
    else
    {
        serialLogger.begin(&Serial, LOG_LEVEL);
        serialLogger.warn("Setup", "SD card not available, falling back to serial logging");
    }

    logger.info("Setup", "Logging initialised");
}

void loop()
{
    logger.info("Loop", "Hello World!");
    delay(1000);
}