Created
December 11, 2012 21:45
-
-
Save vseloved/4262486 to your computer and use it in GitHub Desktop.
Send Lisp errors to Graylog2 server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ql:quickload "usocket") | |
(ql:quickload "cl-json") | |
(ql:quickload "salza2") | |
(ql:quickload "babel") | |
(ql:quickload "local-time") | |
(ql:quickload "rutils") | |
(named-readtables:in-readtable rutil:rutils-readtable) | |
(defvar *hostname* nil) | |
(defvar *graylog-host* #(127 0 0 1)) | |
(defvar *graylog-port* 12201) | |
(defun get-hostname () | |
(or *hostname* | |
(setf *hostname* (rutil:substr (rutil:read-file "/etc/hostname") 0 -1)))) | |
(defun unix-timestamp () | |
(local-time:timestamp-to-unix (local-time:now))) | |
(defun graylog (message &key level backtrace file line-no) | |
(let (sock) | |
(unwind-protect | |
(let ((msg (salza2:compress-data | |
(babel:string-to-octets | |
(json:encode-json-to-string #{:version "1.0" | |
:facility "lisp" | |
:host (get-hostname) | |
:|short_message| message | |
:|full_message| backtrace | |
:timestamp (unix-timestamp) | |
:level level | |
:file file | |
:line line-no | |
}) | |
:encoding :utf-8) | |
'salza2:zlib-compressor))) | |
(setf sock (usocket:socket-connect *graylog-host* *graylog-port* | |
:protocol :datagram | |
:element-type '(unsigned-byte 8))) | |
(usocket:socket-send sock msg (length msg)))) | |
(usocket:socket-close sock))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment