-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathsyslog_client.py
More file actions
47 lines (38 loc) · 1.23 KB
/
syslog_client.py
File metadata and controls
47 lines (38 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import socket
class Facility:
"Syslog facilities"
KERN, USER, MAIL, DAEMON, AUTH, SYSLOG, \
LPR, NEWS, UUCP, CRON, AUTHPRIV, FTP = range(12)
LOCAL0, LOCAL1, LOCAL2, LOCAL3, \
LOCAL4, LOCAL5, LOCAL6, LOCAL7 = range(16, 24)
class Level:
"Syslog levels"
EMERG, ALERT, CRIT, ERR, \
WARNING, NOTICE, INFO, DEBUG = range(8)
class Syslog:
"""A syslog client that logs to a remote server.
Example:
>>> log = Syslog(host="foobar.example")
>>> log.send("hello", Level.WARNING)
"""
def __init__(self,
host="localhost",
port=514,
facility=Facility.DAEMON):
self.host = host
self.port = port
self.facility = facility
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send(self, message, level):
"Send a syslog message to remote host using UDP."
data = "<%d>%s" % (level + self.facility*8, message)
self.socket.sendto(data, (self.host, self.port))
def warn(self, message):
"Send a syslog warning message."
self.send(message, Level.WARNING)
def notice(self, message):
"Send a syslog notice message."
self.send(message, Level.NOTICE)
def error(self, message):
"Send a syslog error message."
self.send(message, Level.ERR)