473,396 Members | 1,929 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.

logger output

i'm redirecting the stdout & stderr of my python program to a log.
Tests i've done on a simple program with print statements, etc. work
fine. however, in my actual program i get weird output like this:

2008-05-04 20:20:44,790 DEBUG Grabbing message from queue, if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:Grabbing message from queue,
if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:DEBUG:doit:Grabbing message
from queue, if any
2008-05-04 20:20:44,790 DEBUG
DEBUG:doit:DEBUG:doit:DEBUG:doit:Grabbing message from queue, if any

followed by:
2008-05-04 20:20:44,815 DEBUG DEBUG:doit:Traceback (most recent call
last):
2008-05-04 20:20:44,815 DEBUG DEBUG:doit:DEBUG:doit:Traceback (most
recent call last):
2008-05-04 20:20:44,815 DEBUG
DEBUG:doit:DEBUG:doit:DEBUG:doit:Traceback (most recent call last):

the code I'm using for the log stuff:

import logging
logger = logging.getLogger('doit')
hdlr = logging.FileHandler('/home/imran/doit.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)

class write2Log:
def write(self, x):
if x!='\n':
logger.debug(str(x))

sys.stdout = write2Log()
sys.stderr= write2Log()

any ideas what might be causing the problems? some of the messages
being output are quite long - might this be a problem?

thanks
Jun 27 '08 #1
4 1165
En Mon, 05 May 2008 00:33:12 -0300, skunkwerk <sk*******@gmail.comescribió:
i'm redirecting the stdout & stderr of my python program to a log.
Tests i've done on a simple program with print statements, etc. work
fine. however, in my actual program i get weird output like this:

2008-05-04 20:20:44,790 DEBUG Grabbing message from queue, if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:Grabbing message from queue,
if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:DEBUG:doit:Grabbing message
from queue, if any
2008-05-04 20:20:44,790 DEBUG
DEBUG:doit:DEBUG:doit:DEBUG:doit:Grabbing message from queue, if any
class write2Log:
def write(self, x):
if x!='\n':
logger.debug(str(x))

any ideas what might be causing the problems? some of the messages
being output are quite long - might this be a problem?
Try this simplified example and see by yourself:

import sys

class Write2Log:
def write(self, x):
sys.__stdout__.write('[%s]' % x)

sys.stdout = Write2Log()

print "Hello world!"
age = 27
name = "John"
print "My name is", name, "and I am", age, "years old."

--
Gabriel Genellina

Jun 27 '08 #2
On May 4, 10:40 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Mon, 05 May 2008 00:33:12 -0300,skunkwerk<skunkw...@gmail.comescribió:
i'm redirecting the stdout & stderr of my python program to a log.
Tests i've done on a simple program with print statements, etc. work
fine. however, in my actual program i get weird output like this:
2008-05-04 20:20:44,790 DEBUG Grabbing message from queue, if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:Grabbing message from queue,
if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:DEBUG:doit:Grabbing message
from queue, if any
2008-05-04 20:20:44,790 DEBUG
DEBUG:doit:DEBUG:doit:DEBUG:doit:Grabbing message from queue, if any
class write2Log:
def write(self, x):
if x!='\n':
logger.debug(str(x))
any ideas what might be causing the problems? some of the messages
being output are quite long - might this be a problem?

Try this simplified example and see by yourself:

import sys

class Write2Log:
def write(self, x):
sys.__stdout__.write('[%s]' % x)

sys.stdout = Write2Log()

print "Hello world!"
age = 27
name = "John"
print "My name is", name, "and I am", age, "years old."

--
Gabriel Genellina
thanks Gabriel,
i tried the code you sent and got output like the following:
[My name is][][john][][and I am][][27][][years old.]

it doesn't really help me though. does this have any advantages over
the syntax i was using?
are there any limits on what kind of objects the logger can write? ie
ascii strings of any length?

thanks
Jun 27 '08 #3
En Mon, 05 May 2008 13:02:12 -0300, skunkwerk <sk*******@gmail.comescribió:
On May 4, 10:40 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Mon, 05 May 2008 00:33:12 -0300,skunkwerk<skunkw...@gmail.comescribió:
i'm redirecting the stdout & stderr of my python program to a log.
Tests i've done on a simple program with print statements, etc. work
fine. however, in my actual program i get weird output like this:
2008-05-04 20:20:44,790 DEBUG Grabbing message from queue, if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:Grabbing message from queue,
if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:DEBUG:doit:Grabbing message

Try this simplified example and see by yourself:

import sys

class Write2Log:
def write(self, x):
sys.__stdout__.write('[%s]' % x)

sys.stdout = Write2Log()

print "Hello world!"
age = 27
name = "John"
print "My name is", name, "and I am", age, "years old."

thanks Gabriel,
i tried the code you sent and got output like the following:
[My name is][][john][][and I am][][27][][years old.]

it doesn't really help me though. does this have any advantages over
the syntax i was using?
are there any limits on what kind of objects the logger can write? ie
ascii strings of any length?
The example doesn't use any logger, so loggers aren't the problem here, ok?

The write function above puts square brackets [] around anything it receives. This way you can see exactly how write() is called: once per *item* in the print statement, plus once per comma used (with an space character that you didn't copy correctly).

Back to your original code, you have to call logger.debug with a *line* of text, but you are calling it with many small pieces - that's the problem. Accumulate output until you see a '\n' - then join all the pieces into a single, complete line and finally call logger.debug

--
Gabriel Genellina

Jun 27 '08 #4
On May 5, 3:44*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
En Mon, 05 May 2008 13:02:12 -0300,skunkwerk<skunkw...@gmail.comescribió:
On May 4, 10:40 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Mon, 05 May 2008 00:33:12 -0300,skunkwerk<skunkw...@gmail.comescribió:
i'm redirecting the stdout & stderr of my python program to a log.
Tests i've done on a simple program with print statements, etc. work
fine. *however, in my actual program i get weird output like this:
2008-05-04 20:20:44,790 DEBUG Grabbing message from queue, if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:Grabbing message from queue,
if any
2008-05-04 20:20:44,790 DEBUG DEBUG:doit:DEBUG:doit:Grabbing message
Try this simplified example and see by yourself:
import sys
class Write2Log:
* * *def write(self, x):
* * * * *sys.__stdout__.write('[%s]' % x)
sys.stdout = Write2Log()
print "Hello world!"
age = 27
name = "John"
print "My name is", name, "and I am", age, "years old."
thanks Gabriel,
* *i tried the code you sent and got output like the following:
[My name is][][john][][and I am][][27][][years old.]
it doesn't really help me though. *does this have any advantages over
the syntax i was using?
are there any limits on what kind of objects the logger can write? *ie
ascii strings of any length?

The example doesn't use any logger, so loggers aren't the problem here, ok?

The write function above puts square brackets [] around anything it receives. This way you can see exactly how write() is called: once per *item* in the print statement, plus once per comma used (with an space character that you didn't copy correctly).

Back to your original code, you have to call logger.debug with a *line* oftext, but you are calling it with many small pieces - that's the problem. Accumulate output until you see a '\n' - then join all the pieces into a single, complete line and finally call logger.debug

--
Gabriel Genellina
thanks Gabriel,
i wrote the function below, but am now getting an "Error in
sys.exitfunc:" error (which disappears when i comment out the last two
lines below):

class write2Log:
def write(self, x):
if x!=',':#ignore if a comma
if str(x).count('\n')==0:
buffer += str(x)
else:
list = str(x).split('\n')
logger.debug(buffer)
buffer = ""
for text in list:
logger.debug(text)

sys.stdout = write2Log()
sys.stderr= write2Log()

any ideas what might be wrong?
thanks again
Jun 27 '08 #5

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

Similar topics

0
by: Laszlo | last post by:
Hi I need a GL::Logger wich implements Class::Singleton to be a singleton and Log::Log4perl to can log amy message into a file. Here is the module source:...
1
by: Federico | last post by:
I want to create an automatic log system that writes to a ofstream (the logfile) all the output sent to screen. I tried to make a class COutput with the operator << overloaded for the different...
1
by: peiyin.li | last post by:
Hi, I want to have two loggers that logs to two different files. Here is what I have: import logging import logging.handlers project1Handler = logging.handlers.RotatingFileHandler(...
10
by: Gary Jefferson | last post by:
Suppose I have 3 modules that belong to a project, 'A', 'A.a' and 'B', and each module has its own logger, created with: module1logger = logging.getLogger('project.A') and module2logger =...
2
by: Steve Greenland | last post by:
Apparently I don't understand logging levels. The code: import logging, logging.handlers logging.basicConfig(level=logging.WARNING) newlog = logging.handlers.TimedRotatingFileHandler(...
4
by: garyjefferson123 | last post by:
Suppose I have some sort of context variable that I want to appear in log messages. E.g.: logger = logging.getLogger("somelogger") class SomeOp: def __init__(self, ctx): self.ctx = ctx def...
1
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good...
3
by: ASh | last post by:
Hi, I have this source: import logging import logging.config logging.config.fileConfig("logging.properties") log = logging.getLogger("qname") log.debug("message")
1
by: Matimus | last post by:
Yes but in the other hand :http://docs.python.org/library/logging.html#logger-objects That is part of the power of the logging module. If you ask for a logger of the same name in two different...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.