logger output

skunkwerk skunkwerk at gmail.com
Tue May 6 14:41:07 EDT 2008


On May 5, 3:44 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
> En Mon, 05 May 2008 13:02:12 -0300,skunkwerk<skunkw... at gmail.com> escribió:
>
>
>
> > On May 4, 10:40 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> > wrote:
> >> En Mon, 05 May 2008 00:33:12 -0300,skunkwerk<skunkw... at gmail.com> 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
>
> --
> 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



More information about the Python-list mailing list