1 """A simple log mechanism styled after PEP 282."""
3 # The class here is styled after PEP 282 so that it could later be
4 # replaced with a standard Python logging implementation.
16 def __init__(self
, threshold
=WARN
):
17 self
.threshold
= threshold
19 def _log(self
, level
, msg
, args
):
20 if level
not in (DEBUG
, INFO
, WARN
, ERROR
, FATAL
):
21 raise ValueError('%s wrong log level' % str(level
))
23 if level
>= self
.threshold
:
26 if level
in (WARN
, ERROR
, FATAL
):
30 stream
.write('%s\n' % msg
)
33 def log(self
, level
, msg
, *args
):
34 self
._log
(level
, msg
, args
)
36 def debug(self
, msg
, *args
):
37 self
._log
(DEBUG
, msg
, args
)
39 def info(self
, msg
, *args
):
40 self
._log
(INFO
, msg
, args
)
42 def warn(self
, msg
, *args
):
43 self
._log
(WARN
, msg
, args
)
45 def error(self
, msg
, *args
):
46 self
._log
(ERROR
, msg
, args
)
48 def fatal(self
, msg
, *args
):
49 self
._log
(FATAL
, msg
, args
)
53 debug
= _global_log
.debug
54 info
= _global_log
.info
55 warn
= _global_log
.warn
56 error
= _global_log
.error
57 fatal
= _global_log
.fatal
59 def set_threshold(level
):
60 # return the old threshold for use from tests
61 old
= _global_log
.threshold
62 _global_log
.threshold
= level