1 ! Copyright (C) 2021 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors calendar calendar.format io io.encodings.utf8
5 io.sockets io.streams.byte-array io.streams.string literals math
6 math.parser namespaces sequences ;
10 ! RFC 3164 (http://www.faqs.org/rfcs/rfc3164.html)
12 ! The first part is called the PRI, the second part is the
13 ! HEADER, and the third part is the MSG. The total length of
14 ! the packet MUST be 1024 bytes or less. There is no minimum
15 ! length of the syslog message although sending a syslog packet
16 ! with no contents is worthless and SHOULD NOT be transmitted.
18 CONSTANT: EMERGENCY 0 ! system is unusable
19 CONSTANT: ALERT 1 ! action must be taken immediately
20 CONSTANT: CRITICAL 2 ! critical conditions
21 CONSTANT: ERROR 3 ! error conditions
22 CONSTANT: WARNING 4 ! warning conditions
23 CONSTANT: NOTICE 5 ! normal but significant condition
24 CONSTANT: INFO 6 ! informational
25 CONSTANT: DEBUG 7 ! debug-level messages
36 SYMBOL: syslog-facility
37 LOCAL0 syslog-facility set-global
40 "127.0.0.1" 514 <inet4> syslog-server set-global
45 day>> number>string 2 CHAR: \s pad-head write ;
47 : write-priority ( level -- )
49 syslog-facility get-global 8 * + number>string write
52 : write-mdhms ( timestamp -- )
53 { MONTH " " dd " " hh ":" mm ":" ss } formatted ;
55 : write-timestamp ( -- )
56 now write-mdhms " " write ;
58 : write-hostname ( -- )
59 host-name write " " write ;
61 : write-syslog ( message level -- )
62 write-priority write-timestamp write-hostname write nl ;
66 : syslog ( message level -- )
67 utf8 [ write-syslog ] with-byte-writer
69 syslog-server get-global
70 $[ f 0 <inet4> <datagram> ]
73 : log-debug ( message -- ) DEBUG syslog ;
75 : log-info ( message -- ) INFO syslog ;
77 : log-warning ( message -- ) WARNING syslog ;
79 : log-error ( message -- ) ERROR syslog ;
81 : log-critical ( message -- ) CRITICAL syslog ;
83 : log-alert ( message -- ) ALERT syslog ;
86 ! Must contain only visible (printing) characters
87 ! "<PRI>TIMESTAMP HOSTNAME"
90 ! <34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8
92 ! relays should handle cases:
93 ! 1) Valid PRI and TIMESTAMP
94 ! 2) Valid PRI but no TIMESTAMP or invalid TIMESTAMP
95 ! 3) No PRI or unidentifiable PRI