|
| 1 | +# Developer Guide for Using ArduinoLog: Log Methods and LOG_LEVEL |
| 2 | + |
| 3 | +When using the `ArduinoLog` library, the logging methods correspond to different log levels, which help you control the verbosity of your logging output. The log levels are hierarchical, meaning that enabling a specific log level will also enable all log levels above it. |
| 4 | + |
| 5 | +## Log Levels and Methods |
| 6 | + |
| 7 | +1. **`LOG_LEVEL_SILENT`**: |
| 8 | + |
| 9 | + - No logging output. |
| 10 | + - No corresponding `Log.` method. |
| 11 | + |
| 12 | +2. **`LOG_LEVEL_FATAL`**: |
| 13 | + |
| 14 | + - Only the most critical errors. |
| 15 | + - Corresponds to `Log.fatal()`. |
| 16 | + - Example: `Log.fatal("This is a fatal error");` |
| 17 | + |
| 18 | +3. **`LOG_LEVEL_ERROR`**: |
| 19 | + |
| 20 | + - Critical errors. |
| 21 | + - Corresponds to `Log.error()`. |
| 22 | + - Example: `Log.error("This is an error");` |
| 23 | + |
| 24 | +4. **`LOG_LEVEL_WARNING`**: |
| 25 | + |
| 26 | + - Warnings and errors. |
| 27 | + - Corresponds to `Log.warning()`. |
| 28 | + - Example: `Log.warning("This is a warning");` |
| 29 | + |
| 30 | +5. **`LOG_LEVEL_INFO` and `LOG_LEVEL_NOTICE`**: |
| 31 | + |
| 32 | + - Informational messages, warnings, and errors. |
| 33 | + - Corresponds to `Log.info()` and `Log.notice()`. |
| 34 | + - Recommend to use `Log.ino()` as `Log.notice()` only kept for backward compatability |
| 35 | + - Example: `Log.info("This is an info message");` |
| 36 | + |
| 37 | +6. **`LOG_LEVEL_TRACE`**: |
| 38 | + |
| 39 | + - Detailed tracing information, notices, informational messages, warnings, and errors. |
| 40 | + - Corresponds to `Log.trace()`. |
| 41 | + - Example: `Log.trace("This is a trace message");` |
| 42 | + |
| 43 | +7. **`LOG_LEVEL_VERBOSE`**: |
| 44 | + - Very detailed tracing information, traces, notices, informational messages, warnings, and errors. |
| 45 | + - Corresponds to `Log.verbose()`. |
| 46 | + - Example: `Log.verbose("This is a verbose message");` |
| 47 | + |
| 48 | +## Setting the Log Level |
| 49 | + |
| 50 | +You can set the log level globally in your `config.h` file. For example: |
| 51 | + |
| 52 | +```cpp |
| 53 | +#define LOG_LEVEL LOG_LEVEL_TRACE |
| 54 | +``` |
| 55 | +
|
| 56 | +To use logging in your code, include the following: |
| 57 | +
|
| 58 | +```cpp |
| 59 | +#include <ArduinoLog.h> |
| 60 | +``` |
| 61 | + |
| 62 | +### Examples |
| 63 | + |
| 64 | +##### (note: all of these methods have variants without the trailing 'ln' e.g. Log.info(). The difference being the ln variant adds a newline character (\n)) |
| 65 | + |
| 66 | +```cpp |
| 67 | +Log.infoln("This is an info message."); |
| 68 | +Log.warningln("Warning: Device is overheating!"); |
| 69 | +Log.errorln("Error: Sensor not found."); |
| 70 | +Log.fatalln("Fatal: System crash imminent!"); |
| 71 | +Log.infoln("Sensor value: " + String(sensorValue)); |
| 72 | + |
| 73 | +Log.infoln("Count: %d", count); |
| 74 | +Log.infoln("Status: %s, Value: %d", status.c_str(), value); |
| 75 | +Log.warningln("Temperature: %.2f°C", temperature); |
| 76 | +Log.errorln("Error %d: %s", errorCode, errorMessage.c_str()); |
| 77 | +Log.traceln("Loop iteration: %d", loopCount); |
| 78 | + |
| 79 | +Log.infonln("Device: " + String(deviceName) + ", ID: %d", deviceId); |
| 80 | +Log.warninln("Battery level: %d%%, Status: %s", batteryLevel, status.c_str()); |
| 81 | +``` |
| 82 | + |
| 83 | +Do NOT use Arduino Strings directly for %s, but use .c_str(), like in Serial.printf() |
| 84 | + |
| 85 | +## General Guidance |
| 86 | + |
| 87 | +1. Opt for using `Log` instead of `Serial.print` where possible. |
| 88 | +2. Keep `INFO`, `NOTICE` log messages limited and concise to avoid cluttering the serial monitor with messages that may not be meaningful (`INFO` and `NOTICE` are synonymous). |
| 89 | +3. Use `WARNING` log messages for unexpected occurrences during execution that may not cause immediate issues (e.g., "Only 4 stock symbols defined"). |
| 90 | +4. Use `TRACE` log messages for debugging purposes, such as tracking execution flow or dumping JSON returned from an API call. |
| 91 | +5. Use `WARNING` for less critical errors, `ERROR` for more critical errors, and `FATAL` only for the most critical errors. |
0 commit comments