The official location and documentation for this library is: https://github.com/rickkas7/SdCardLogHandlerRK.
It’s in the Particle community libraries as: SdCardLogHandlerRK.
You’ll need a SD card reader, presumably a Micro SD card reader. Make sure you get one that’s compatible with the 3.3V logic levels used on the Photon and Electron. Some readers designed for the Arduino expect the 5V logic levels used by the original Arduino. I use this one from Sparkfun but there are others.
This library uses the Logging API that was added in system firmware 0.6.0.
For example:
Log.trace("Low level debugging message");
Log.info("This is info message");
Log.warn("This is warning message");
Log.error("This is error message");
Log.info("System version: %s", System.version().c_str());
It does not remap Serial, so if you’re using Serial.print for logging, you should switch to using Log.info instead.
The library creates a directory (default: “logs”) at the top level of the SD card and stores the log files in that. Each log file is a .txt file 6-digit number beginning with 000001.txt.
The default log file is 1 MB (1000000 bytes), but you can reconfigure this. The actual size will be slightly larger than that, as the log is rotated when it exceeds the limit, and the file will always contain a whole log entry.
When rotated, the default is to keep 10 log files, but this is configurable. The oldest is deleted when the maximum number is reached.
By default, SdCardLogHandler writes to Serial as well, like SerialLogHandler. This can be reconfigured.
This is the example program:
#include "Particle.h"
#include "SdFat.h"
#include "SdCardLogHandlerRK.h"
SYSTEM_THREAD(ENABLED);
const int SD_CHIP_SELECT = A2;
SdFat sd;
SdCardLogHandler logHandler(sd, SD_CHIP_SELECT, SPI_FULL_SPEED);
size_t counter = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
Log.info("testing counter=%d", counter++);
delay(1000);
}