Debugging

Serial.print vs. Log.info

SerialLogHandler logHandler;

void setup() {
    Log.info("Device OS version: %s", (const char*)System.version());
}

void loop() {
}

In Arduino, it's most common to use Serial.print() as well as its relatives like Serial.println() and Serial.printlnf(). While these are also available on Particle devices, it's strongly recommended that you instead use the Logging facility.

  • Serial is not thread-safe. It's possible that if you log simultaneously with both Serial and Logging calls, the device may crash.
  • Using Logging you can adjust the verbosity from a single statement in your code, including setting the logging level per module at different levels.
  • Using Logging you can redirect the logs between USB serial and UART serial with one line of code.
  • Other logging handlers allow you to store logs on SD cards, to a network service like syslog, or to a cloud-based service like Solarwinds Papertrail.

Being able to switch from USB to UART serial is especially helpful if you are using sleep modes. Because USB serial disconnects on sleep, it can take several seconds for it to reconnect to your computer. By using UART serial (Serial1, for example) with a USB to TTL serial converter, the USB serial will stay connected to the adapter so you can begin logging immediately upon wake.