Repository navigation
Expand file tree
/
Copy pathlogger.py
More file actions
93 lines (77 loc) · 3.06 KB
/
Copy pathlogger.py
File metadata and controls
93 lines (77 loc) · 3.06 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Ginkgo logger
This module provides the class for a logger object used by the runner module's
`Process` object to manage, configure, and provide services around Python's
standard logging module. Most notably it allows you to easily configure the
Python logger using Ginkgo configuration.
"""
import logging
import logging.config
import os
import os.path
import sys
import ginkgo
DEFAULT_FORMAT = "%(asctime)s %(levelname) 7s %(module)s: %(message)s"
class Logger(object):
logfile = ginkgo.Setting("logfile", default=None, help="""
Path to primary log file. Ignored if logconfig is set.
""")
loglevel = ginkgo.Setting("loglevel", default='debug', help="""
Log level to use. Valid options: debug, info, warning, critical
Ignored if logconfig is set.
""")
config = ginkgo.Setting("logconfig", default=None, help="""
Configuration of standard Python logger. Can be dict for basicConfig,
dict with version key for dictConfig, or ini filepath for fileConfig.
""")
def __init__(self, process):
self.process = process
if self.logfile is None:
process.config.set("logfile", os.path.expanduser(
"~/.{}.log".format(process.service_name)))
self.load_config()
def load_config(self):
if self.config is None:
self._load_default_config()
else:
if isinstance(self.config, str) and os.path.exists(self.config):
logging.config.fileConfig(self.config)
elif 'version' in self.config:
logging.config.dictConfig(self.config)
else:
self._reset_basic_config(self.config)
def _load_default_config(self):
default_config = dict(
format=DEFAULT_FORMAT,
level=getattr(logging, self.loglevel.upper()))
if hasattr(self.process, 'pidfile'):
default_config['filename'] = self.logfile
self._reset_basic_config(default_config)
def _reset_basic_config(self, config):
for h in logging.root.handlers[:]:
logging.root.removeHandler(h)
logging.basicConfig(**config)
def capture_stdio(self):
# TODO: something smarter than this?
try:
os.dup2(logging._handlerList[0]().stream.fileno(), sys.stdout.fileno())
os.dup2(logging._handlerList[0]().stream.fileno(), sys.stderr.fileno())
except:
pass
@property
def file_descriptors(self):
return [handler.stream.fileno() for handler in [wr() for wr in
logging._handlerList] if isinstance(handler, logging.FileHandler)]
def shutdown(self):
logging.shutdown()
def print_log(self):
with open(self.logfile, "r") as f:
print (f.read())
def tail_log(self):
with open(self.logfile, "r") as f:
lines = f.readlines()
for line in lines[-20:]:
print (line.strip())
while True:
line = f.readline()
if line:
print (line.strip())