473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7965
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.