StreamHandler that doesn't print newlines by default. 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.
| 26 | |
| 27 | # Modified from python2.5/__init__.py |
| 28 | class StreamHandlerNoNewline(logging.StreamHandler): |
| 29 | """StreamHandler that doesn't print newlines by default. |
| 30 | Since StreamHandler automatically adds newlines, define a mod to more |
| 31 | easily support interactive mode when we want it, or errors-only logging |
| 32 | for running unit tests.""" |
| 33 | |
| 34 | def emit(self, record): |
| 35 | """Emit a record. |
| 36 | If a formatter is specified, it is used to format the record. |
| 37 | The record is then written to the stream with a trailing newline |
| 38 | [ N.B. this may be removed depending on feedback ]. If exception |
| 39 | information is present, it is formatted using |
| 40 | traceback.printException and appended to the stream.""" |
| 41 | try: |
| 42 | msg = self.format(record) |
| 43 | fs = '%s' # was '%s\n' |
| 44 | if not hasattr(types, 'UnicodeType'): # if no unicode support... |
| 45 | self.stream.write(fs % msg) |
| 46 | else: |
| 47 | try: |
| 48 | self.stream.write(fs % msg) |
| 49 | except UnicodeError: |
| 50 | self.stream.write(fs % msg.encode('UTF-8')) |
| 51 | self.flush() |
| 52 | except (KeyboardInterrupt, SystemExit): |
| 53 | raise |
| 54 | except: |
| 55 | self.handleError(record) |
| 56 | |
| 57 | |
| 58 | class Singleton(type): |