-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog_manager.py
86 lines (57 loc) · 1.58 KB
/
log_manager.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
'''
The logging file
This file controls all the logging functions.
- The `console_level` parameter defines de degree of logs make it to the stdout.
- Logfile output is stored in the `logfile` parameter. All error levels are written to this file.
- Finally the print function is rewritten and replaced by log.INFO call.
'''
import logging,os
user = os.popen('echo $USER').read().strip()
if user == 'root': __RDIR__ = '/root'
else: __RDIR__ = '/home/'+user
logfile = __RDIR__+'/script.log'
print('logging in ',logfile)
console_level = logging.INFO
'''
Console Stream
'''
console = logging.StreamHandler()
formatter = logging.Formatter('%(levelname)-10s %(message)s')
console.setFormatter(formatter)
console.setLevel(console_level)
'''
File Debug
'''
tofile = logging.FileHandler(logfile, mode='a')
formatter = logging.Formatter('%(asctime)s ~ %(name)s ~ %(levelname)s ~ %(message)s')
tofile.setFormatter(formatter)
tofile.setLevel(logging.DEBUG)
def getlog(name):
'''
Function to set up a new logger for each module
'''
name = str(name)
if name=='': name = 'unknown'
log = logging.getLogger(name) ## if running interactively with ipython, replace this with a descriptive string
log.propagate = False
log.setLevel(logging.DEBUG)
'''
Remove any pre-existing handlers
'''
while len(log.handlers):
log.removeHandler(log.handlers[0])
'''
Console Stream
'''
log.addHandler(console)
'''
File Debug
'''
log.addHandler(tofile)
'''
Make log printlike
'''
def print(*argv):
return log.info(' '.join(map(str,argv)))
log.print = print
return log