1 module syslogserviceclient; 2 3 import vibe.core.core:runTask; 4 import vibe.http.client; 5 6 /// 7 class SyslogServiceClient 8 { 9 private: 10 immutable string m_url; 11 12 public: 13 /// 14 this(in string _url) 15 { 16 import std.string:endsWith; 17 18 if(_url.length > 0 && !_url.endsWith("/")) 19 m_url = _url~"/"; 20 else 21 m_url = _url; 22 } 23 24 /// 25 void log(string _event)(in string[string] params) 26 { 27 if(m_url.length == 0) 28 return; 29 30 runTask({ 31 auto requestUrl = m_url~getAdditionalUrlString()~_event; 32 33 auto res = requestHTTP(requestUrl, 34 (scope HTTPClientRequest req) { 35 req.method = HTTPMethod.POST; 36 37 import vibe.http.form; 38 req.writeFormBody(params); 39 }); 40 scope(exit) res.dropBody(); 41 }); 42 } 43 44 /// 45 void log(string _event)() 46 { 47 if(m_url.length == 0) 48 return; 49 50 runTask({ 51 auto requestUrl = m_url~getAdditionalUrlString()~_event; 52 53 auto res = requestHTTP(requestUrl, 54 (scope HTTPClientRequest req) { 55 req.method = HTTPMethod.POST; 56 57 req.writeBody(cast(ubyte[])""); 58 }); 59 scope(exit) res.dropBody(); 60 }); 61 } 62 63 /// 64 protected string getAdditionalUrlString() 65 { 66 return ""; 67 } 68 } 69 70 version(none) 71 { 72 shared static this() 73 { 74 SyslogServiceClient logger = new SyslogServiceClient("http://localhost:8888/"); 75 76 logger.log!"event1"(); 77 78 logger.log!"event2"(["param2":"value"]); 79 } 80 }