472,958 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

logging module and trailing newlines

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?

Oct 2 '07 #1
2 7853
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
Oct 3 '07 #2
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...@web.dewrote:
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

Oct 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robert.Schmitt | last post by:
I found that the configuration system of the new logging package of Python 2.3 has some unintuitive idiosyncracies that are worth mentioning because they can cost you quite some development time...
1
by: Doug Fort | last post by:
Hi, Perhaps this is as much a Unix question as a Python question. I really like the Python logging module, and I've used it extensively, with good results. Now I have a requirement to add a...
6
by: Ville Vainio | last post by:
Just posting this for the sake of google: Like everyone else, I figured it's time to start using the 'logging' module. I typically want to dump "info" level (and up) log information to...
2
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have no...
23
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
7
by: Andrew McLean | last post by:
I have a bunch of csv files that have the following characteristics: - field delimiter is a comma - all fields quoted with double quotes - lines terminated by a *space* followed by a newline ...
4
by: lihao0129 | last post by:
Hi, folks: I recently went through a strange problem with my Javascript code, say: I have a string variable which are from a 'textarea' element and I want to remove the trailing newlines inside...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.