-
Notifications
You must be signed in to change notification settings - Fork 5
/
slog.py
64 lines (55 loc) · 2.32 KB
/
slog.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
import logging
import logging.handlers
class slog:
# Verbosity levels (1-5)
# 1 = only start/stop
# 2 = + status and errors
# 3 = + flow control
# 4 = + data
# 5 = anything
def __init__(self, ident='', verbosity = 3, log_type='syslog', log_address='/dev/log', log_port=514, cat = logging.INFO):
self.__ident = ident
self.__cat = cat
self.set_verbosity(verbosity)
self.__type = log_type
self.__address = log_address
self.__port = log_port
if log_type == 'sys.stdout':
ch = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
elif log_type == 'sys.stderr':
ch = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
else:
# default is to log to syslog
log_type='syslog'
if log_type == 'syslog':
if log_address == '/dev/log':
ch = logging.handlers.SysLogHandler(address=log_address, facility='daemon')
else:
ch = logging.handlers.SysLogHandler(address=(log_address, log_port), facility='daemon')
formatter = logging.Formatter('%(name)s: %(message)s')
ch.setFormatter(formatter)
self.__logger = logging.getLogger(self.__ident)
self.__logger.setLevel(self.__cat)
if self.__logger.handlers:
# remove previous handler
self.__logger.handlers.pop()
self.__logger.addHandler(ch)
def __repr__(self):
return 'log(' + str(self.__ident) + ',' + self.__verbosity + ',' + self.__type + ',' + self.__address + ',' + self.__port + ')'
def logMsg (self, msg, vlevel = 3, cat = None):
if cat == None:
cat = self.__cat
# Only write to log if vlevel <= verbosity
if vlevel <= self.__verbosity:
self.__logger.log(cat, msg)
def set_verbosity(self, verbosity):
if verbosity < 1:
verbosity = 1
if verbosity > 5:
verbosity = 5
self.__verbosity = verbosity