Skip to content
Snippets Groups Projects
Commit 7b804ffb authored by Brandon Heller's avatar Brandon Heller
Browse files

More flexible logging support

Use customized StreamHandler from Python logging module to print
only messages for the specified loglevel to the console.
parent 1095628b
No related branches found
No related tags found
No related merge requests found
# 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)
\ No newline at end of file
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment