logging module and trailing newlines

Discussion in 'Python' started by Russell Warren, Oct 2, 2007.

  1. I was just setting up some logging in a make script and decided to
    give the built-in logging module a go, but I just found out that the
    base StreamHandler always puts a newline at the end of each log.
    There is a comment in the code that says "The record is then written
    to the stream with a trailing newline [N.B. this may be removed
    depending on feedback]"... I guess there wasn't the feedback to drive
    the change.

    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?
    Russell Warren, Oct 2, 2007
    #1
    1. Advertising

  2. Russell Warren

    Peter Otten Guest

    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
    Peter Otten, Oct 3, 2007
    #2
    1. Advertising

  3. 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 <> 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
    Russell Warren, Oct 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christoph Haas
    Replies:
    0
    Views:
    433
    Christoph Haas
    Jun 12, 2006
  2. Christoph Haas
    Replies:
    1
    Views:
    441
    Vinay Sajip
    Jun 14, 2006
  3. Dotan Cohen
    Replies:
    2
    Views:
    493
    Dotan Cohen
    Oct 10, 2011
  4. John

    Removing trailing newlines -

    John, Apr 23, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    162
    J. Gleixner
    Apr 24, 2008
  5. Replies:
    6
    Views:
    130
    Thomas 'PointedEars' Lahn
    Jan 27, 2008
Loading...

Share This Page