-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpulp_logging.py
More file actions
78 lines (62 loc) · 2.07 KB
/
pulp_logging.py
File metadata and controls
78 lines (62 loc) · 2.07 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
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
#
# Create our own logging class based on logging module
# Problem with Python logging is that it of course can write to multiple streams
# to stdout, and file. But if you have many system calls with Popen, the output
# from these calls won't be duplicated between several streams
# It is only possible to achieve running subprocess to PIPE and then logging this output
# as implemented in process2log function
#
import logging
import subprocess
# My logger class that emulates somewhat system logging + has extra functionality
class PulpLogger(object):
"""
Fake file-like stream that redirects writes to a logger instance
"""
def __init__(self, name=None, log_level=logging.INFO):
self.logger = logging.getLogger(name)
self.log_level = log_level
self.logger.setLevel(self.log_level) # our default verbosity level
# adding stream handler
self.logger.addHandler(logging.StreamHandler())
def set_loglevel (self, log_level):
self.log_level = log_level
self.logger.setLevel(self.log_level)
def get_logfile (self):
logfile=None
for handler in self.logger.handlers:
try:
logfile=handler.baseFilename
break
except: continue
return logfile
def addHandler(self, handler):
self.logger.addHandler(handler)
def removeHandler(self, handler):
self.logger.removeHandler(handler)
def write(self, msg):
self.logger.log(self.log_level, msg)
# for line in msg.rstrip().splitlines():
# self.logger.log(self.log_level, line.rstrip())
def flush(self):
for handler in self.logger.handlers:
handler.flush()
def debug(self, msg):
self.logger.debug(msg)
def info(self, msg):
self.logger.info(msg)
def error(self, msg):
self.logger.error(msg)
def warning(self, msg):
self.logger.warning(msg)
def critical(self, msg):
self.logger.critical(msg)
def exception(self, msg):
self.logger.exception(msg)
# to log the output from subprocess shell process
# assuming that stderr from the process will be first redirected to stdout
def process2log(self, process):
while True:
line = process.stdout.readline()
if not line: break
self.info(line.rstrip())