-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
30 lines (23 loc) · 1.08 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
import sys
class Logger(object):
"""General purpose logging to console/file with log levels.
The log threshold is configurable, and determines which messages are
output (level <= threshold), and which are discarded (level > threshold).
"""
Levels = [ "error", "warning", "info", "debug" ] # High -> low importance
Threshold = "info" # Default threshold, override in constructor
@classmethod
def limit(cls, val, maximum=len(Levels), minimum=0):
"""Limit val to [minimum, maximum)."""
return max(minimum, val) if val < maximum else maximum - 1
@classmethod
def threshold(cls, adjust=0, default=Threshold):
"""Calculate new threshold by applying an adjustment to the default."""
return cls.Levels[cls.limit(cls.Levels.index(default) + adjust)]
def __init__(self, f=sys.stderr, threshold=Threshold):
self.f = f
self.t = self.Levels.index(threshold)
def __call__(self, level, msg):
"""Print msg if level <= threshold."""
if self.Levels.index(level) <= self.t:
print >>self.f, msg