-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
92 lines (73 loc) · 2.2 KB
/
logger.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import config
import sys, os
import time
#########
# This module allows us to have fine tuned control over what gets printed and to where.
# In the future in can be extended to be helpful for the admin
#########
# get logger config info from config file
config_data = config.read_config()
default_logfile = None
if 'Logger' in config_data:
if 'log_file' in config_data['Logger']:
default_logfile = config_data['Logger']['log_file']
# Each discrete functional unit should register its own logger
# Loggers can access each other through the logger.get_logger function farther down
# Loggers have their own set of options and their own logfile.
#
def get_logger(name):
if name in Logger.LoggerIDs:
return Logger.LoggerIDs[name]
else:
raise RuntimeError("attempted to modify a nonexistant logger")
def load_file(flname):
if flname == 'stdout':
return sys.stdout
elif flname == 'stderr':
return sys.stderr
elif flname.startswith('email:'):
import mailsend
return mailsend.EmailFile(flname[6:])
else:
return open(flname, 'w')
class Logger(object):
LoggerIDs = {}
def __init__(self, name, logfile = default_logfile):
self.options = {
'timestamp' : False,
'enable' : True,
'minimal' : False,
'logfile' : default_logfile or 'stdout',
}
self.name = name
Logger.LoggerIDs[name] = self
def set_option(self, **kwargs):
self.options.update(kwargs)
def set_logfile(self, flname):
self.options['logfile'] = flname
def enable(self):
self.options['enable'] = True
def disable(self):
self.options['enable'] = False
def get_timestamp(self):
return time.strftime('[%c]')
def error(self, info, what):
import mailsend
text = mailsend.error_message(what)
addr = 'email:' + info.sender
self.log_to(addr, text)
def log_to(self, flname, *text):
tmp = self.options['logfile']
self.set_logfile(flname)
self.log(*text)
self.set_logfile(tmp)
def log(self, *text):
text = ' '.join(str(x) for x in text)
if self.options['enable']:
if self.options['timestamp']:
timestamp = self.get_timestamp()
else:
timestamp = ''
output_line = timestamp + str(text) + os.linesep
logfile = load_file(self.options['logfile'])
logfile.write(output_line)