├── PapertrailLogger.cpp ├── PapertrailLogger.h ├── README.md ├── papertrail_credentials.h ├── papertrail_test.ino └── wifi_credentials.h /PapertrailLogger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "PapertrailLogger.h" 3 | 4 | const int FacilityCode = 16; 5 | 6 | PapertrailLogger::PapertrailLogger(String host, int port, LogLevel level, String color, String system, String context) { 7 | mHost = host; 8 | mPort = port; 9 | mLevel = level; 10 | mColor = color; 11 | mSystem = system; 12 | mContext = context; 13 | mBufferPos = 0; 14 | mWifiUdp = new WiFiUDP(); 15 | } 16 | 17 | size_t PapertrailLogger::write(uint8_t c) { 18 | // check to see if we've reached the end of our buffer or received a newline character 19 | if (c == '\n' || mBufferPos == BUFFER_SIZE - 1) { 20 | // add a null terminating byte to the buffer 21 | mBuffer[mBufferPos] = 0; 22 | // construct the syslog message (see https://tools.ietf.org/html/rfc5424#page-19) 23 | String syslogMessage = "<" + String(FacilityCode*8 + mLevel) + ">1 - " + mSystem + " " + mContext + " - - - " + mColor + String((const char *) mBuffer); 24 | // send the syslog message to papertrail using UDP 25 | mWifiUdp->beginPacket(mHost.c_str(), mPort); 26 | mWifiUdp->write((const uint8_t *) syslogMessage.c_str(), syslogMessage.length()); 27 | mWifiUdp->endPacket(); 28 | // send the message to the Serial port 29 | Serial.println((const char *) mBuffer); 30 | mBufferPos = 0; 31 | } else { 32 | // buffer the character up for sending later 33 | mBuffer[mBufferPos] = c; 34 | mBufferPos++; 35 | } 36 | return 1; 37 | } 38 | -------------------------------------------------------------------------------- /PapertrailLogger.h: -------------------------------------------------------------------------------- 1 | #ifndef __papertrail_logger_h__ 2 | #define __papertrail_logger_h__ 3 | 4 | class WiFiUDP; 5 | 6 | enum LogLevel { 7 | Error = 3, 8 | Warning = 4, 9 | Notice = 5, 10 | Info = 6, 11 | Debug = 7 12 | }; 13 | 14 | #define BUFFER_SIZE 200 15 | 16 | class PapertrailLogger: public Print { 17 | private: 18 | String mHost; 19 | int mPort; 20 | WiFiUDP *mWifiUdp; 21 | LogLevel mLevel; 22 | String mSystem; 23 | String mContext; 24 | String mColor; 25 | 26 | uint8_t mBuffer[BUFFER_SIZE]; 27 | int mBufferPos; 28 | 29 | public: 30 | PapertrailLogger(String host, int port, LogLevel level, String color, String system, String context); 31 | size_t write(uint8_t c); 32 | }; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | This project demonstrates how to log messages t a remote logging system from an Arudiono. 4 | 5 | For this example we use Papertrail - https://www.papertrail.com/ 6 | 7 | [![Demo Video](https://img.youtube.com/vi/GLoXRyAtytY/0.jpg)](https://www.youtube.com/watch?v=GLoXRyAtytY) 8 | 9 | # Setup 10 | 11 | Sign up for a free Papertail account and click the "Add System" to get your host and port number. 12 | 13 | Update the file papertrail_credentials.h with the correct values: 14 | 15 | ```c 16 | #define PAPERTRAIL_HOST "" 17 | #define PAPERTRAIL_PORT 18 | ``` 19 | 20 | Update the file `wifi_credentials.h` with your SSID and PASSWORD. 21 | 22 | ```c 23 | #define SSID "YOUR_SSID" 24 | #define PASSWORD "YOUR_PASSWORD" 25 | ``` 26 | 27 | Now just compile and run the sketch. 28 | 29 | You should see messages appear in Papertrail. 30 | -------------------------------------------------------------------------------- /papertrail_credentials.h: -------------------------------------------------------------------------------- 1 | #define PAPERTRAIL_HOST "YOUR_PAPERTRAIL_HOST" 2 | #define PAPERTRAIL_PORT YOUR_PAPERTRAIL_PORT 3 | -------------------------------------------------------------------------------- /papertrail_test.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "wifi_credentials.h" 5 | #include "papertrail_credentials.h" 6 | #include "PapertrailLogger.h" 7 | 8 | PapertrailLogger *errorLog; 9 | PapertrailLogger *warningLog; 10 | PapertrailLogger *noticeLog; 11 | PapertrailLogger *debugLog; 12 | PapertrailLogger *infoLog; 13 | 14 | void setup() { 15 | // put your setup code here, to run once: 16 | Serial.begin(115200); 17 | 18 | WiFi.mode(WIFI_STA); 19 | WiFi.begin(SSID, PASSWORD); 20 | if (WiFi.waitForConnectResult() != WL_CONNECTED) 21 | { 22 | Serial.println("WiFiConnection Failed"); 23 | delay(5000); 24 | ESP.restart(); 25 | } 26 | errorLog = new PapertrailLogger(PAPERTRAIL_HOST, PAPERTRAIL_PORT, LogLevel::Error, "\033[0;31m", "papertrail-test", "testing"); 27 | warningLog = new PapertrailLogger(PAPERTRAIL_HOST, PAPERTRAIL_PORT, LogLevel::Warning, "\033[0;33m", "papertrail-test", "testing"); 28 | noticeLog = new PapertrailLogger(PAPERTRAIL_HOST, PAPERTRAIL_PORT, LogLevel::Notice, "\033[0;36m", "papertrail-test", "testing"); 29 | debugLog= new PapertrailLogger(PAPERTRAIL_HOST, PAPERTRAIL_PORT, LogLevel::Debug, "\033[0;32m","papertrail-test", "testing"); 30 | infoLog = new PapertrailLogger(PAPERTRAIL_HOST, PAPERTRAIL_PORT, LogLevel::Info, "\033[0;34m", "papertrail-test", "testing"); 31 | Serial.println("Up and running"); 32 | } 33 | 34 | 35 | int count = 0; 36 | void loop() { 37 | infoLog->println("Looping"); 38 | infoLog->printf("In the loop %d\n", count); 39 | 40 | errorLog->printf("In the loop error %d\n", count); 41 | warningLog->printf("In the loop warning %d\n", count); 42 | noticeLog->printf("In the loop notice %d\n", count); 43 | debugLog->printf("In the loop debug %d\n", count); 44 | infoLog->printf("In the loop info %d\n", count); 45 | 46 | count++; 47 | delay(3000); 48 | } 49 | -------------------------------------------------------------------------------- /wifi_credentials.h: -------------------------------------------------------------------------------- 1 | #define SSID "YOUR_SSID" 2 | #define PASSWORD "YOUR_PASSWORD" 3 | --------------------------------------------------------------------------------