Use customized StreamHandler from Python logging module to print only messages for the specified loglevel to the console.
35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
# Since StreamHandler automatically adds newlines, define a mod to more easily
|
|
# support interactive mode when we want it, or errors-only logging for running
|
|
# unit tests.
|
|
|
|
from logging import StreamHandler
|
|
import types
|
|
|
|
# Modified from python2.5/__init__.py to not require newlines
|
|
class StreamHandlerNoNewline(StreamHandler):
|
|
|
|
def emit(self, record):
|
|
"""
|
|
Emit a record.
|
|
|
|
If a formatter is specified, it is used to format the record.
|
|
The record is then written to the stream with a trailing newline
|
|
[N.B. this may be removed depending on feedback]. If exception
|
|
information is present, it is formatted using
|
|
traceback.print_exception and appended to the stream.
|
|
"""
|
|
try:
|
|
msg = self.format(record)
|
|
fs = "%s" # was "%s\n"
|
|
if not hasattr(types, "UnicodeType"): #if no unicode support...
|
|
self.stream.write(fs % msg)
|
|
else:
|
|
try:
|
|
self.stream.write(fs % msg)
|
|
except UnicodeError:
|
|
self.stream.write(fs % msg.encode("UTF-8"))
|
|
self.flush()
|
|
except (KeyboardInterrupt, SystemExit):
|
|
raise
|
|
except:
|
|
self.handleError(record) |