-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathextraLoggers.py
More file actions
73 lines (62 loc) · 3.28 KB
/
Copy pathextraLoggers.py
File metadata and controls
73 lines (62 loc) · 3.28 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
import logging
import logging.config
import os
import sys
import yaml
from messageCounterHandler import MsgCounterHandler
if os.path.exists('loggerconfig.yaml'):
with open('loggerconfig.yaml', 'r') as loggeryaml: # this works on all relevant platforms so pylint: disable=unspecified-encoding
config = yaml.safe_load(loggeryaml.read())
logging.config.dictConfig(config)
else:
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
# a logger for information we try to "insist" that the user sees
mustsee = logging.getLogger("cewe2pdf.mustsee")
# a logger for configuration, to distinguish that from logging in the album processing
configlogger = logging.getLogger("cewe2pdf.config")
class ConversionMessageCounters:
"""Count log records for one conversion and detach cleanly afterwards.
The handlers deliberately count records before logger-level filtering, as
did the original global handlers. Their lifetime is now one conversion,
rather than the whole Python process, so consecutive conversions no longer
inherit one another's expected-message totals.
"""
def __init__(self):
self.root_handler = MsgCounterHandler()
self.root_handler.setLevel(logging.DEBUG)
logging.getLogger().addHandler(self.root_handler)
self.config_handler = MsgCounterHandler()
self.config_handler.setLevel(logging.DEBUG)
configlogger.addHandler(self.config_handler)
def close(self):
"""Detach the handlers once the conversion has reported its totals."""
logging.getLogger().removeHandler(self.root_handler)
configlogger.removeHandler(self.config_handler)
def verify(self, configSection):
# if he has specified "normal" values for the number of messages of each kind, then warn if we do not see that number
if configSection is None:
return
# the expectedLoggingMessageCounts section is one or more newline separated list of
# loggername: levelname[count], ...
# e.g.
# root: WARNING[4], INFO[38]
# Any loggername that is missing is not checked, any logging level that is missing is expected to have 0 messages
ff = configSection.get('expectedLoggingMessageCounts', '').splitlines()
loggerdefs = filter(lambda bg: (len(bg) != 0), ff)
for loggerdef in loggerdefs:
items = loggerdef.split(":")
if len(items) == 2:
loggerName = items[0].strip()
leveldefs = items[1].strip() # a comma separated list of levelname[count]
if loggerName == configlogger.name:
self.config_handler.checkCounts(loggerName,leveldefs)
elif loggerName == logging.getLogger().name:
self.root_handler.checkCounts(loggerName,leveldefs)
else:
print(f"Invalid expectedLoggingMessageCounts logger name, entry ignored: {loggerdef}")
else:
print(f"Invalid expectedLoggingMessageCounts entry ignored: {loggerdef}")
def print_summary(self):
print("Total message counts, including messages suppressed by logger configuration")
print(f" cewe2pdf.config: {self.config_handler.messageCountText()}")
print(f" root: {self.root_handler.messageCountText()}")