logger output

S

skunkwerk

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
 
G

Gabriel Genellina

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."
 
S

skunkwerk

En Mon, 05 May 2008 00:33:12 -0300,skunkwerk<[email protected]> escribió:


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."

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
 
G

Gabriel Genellina

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
 
S

skunkwerk

En Mon, 05 May 2008 13:02:12 -0300,skunkwerk<[email protected]> escribió:


En Mon, 05 May 2008 00:33:12 -0300,skunkwerk<[email protected]> escribió:
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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top