logging module and trailing newlines

Russell Warren russandheather at gmail.com
Wed Oct 3 10:32:41 EDT 2007


Both are very good responses... thanks!  I had forgotten the ease of
"monkey-patching" in python and the Stream class is certainly cleaner
than the way I had been doing it.

On Oct 3, 3:15 am, Peter Otten <__pete... at web.de> wrote:
> Russell Warren wrote:
> > All I'm after is the ability to log things like...
>
> > Compiling 'shrubbery.c'...      [DONE]
>
> > where the "[DONE]" was added later in time than the "Compiling...", and
> > the output goes to both stdout and to a log file.  ie: I want to tee my
> > print statements and keep the ability to skip the trailing newline.  I had
> > rolled my own primitive version than decided to try the logging module for
> > kicks.
>
> > Anyone have a suggestion on how to get logging to work like this?  Or know
> > of a way to tee in Windows without forcing other users to install a tee
> > package?
>
> (1) Logging
>
> If you are too lazy to subclass you can monkey-patch:
>
> >>> import logging
> >>> def emit(self, record):
>
> ...     msg = self.format(record)
> ...     fs = "%s" if getattr(record, "continued", False) else "%s\n"
> ...     self.stream.write(fs % msg)
> ...     self.flush()
> ...>>> logging.StreamHandler.emit = emit
> >>> continued = dict(continued=True)
> >>> logging.error("Compiling... ", extra=continued); logging.error("[Done]")
>
> ERROR:root:Compiling... ERROR:root:[Done]
>
> (2) Teeing
>
> "Primitive", but should work:
>
> >>> class Stream(object):
>
> ...     def __init__(self, *streams):
> ...             self.streams = streams
> ...     def write(self, s):
> ...             for stream in self.streams:
> ...                     stream.write(s)
> ...     def flush(self):
> ...             for stream in self.streams:
> ...                     stream.flush()
> ...>>> import sys
> >>> stream = Stream(sys.stdout, sys.stderr)
> >>> print >> stream, "Compiling...",
>
> Compiling...Compiling...>>>
>
> I'd probably go with the latter.
>
> Peter





More information about the Python-list mailing list