logging and daemons
Neal Becker
ndbecker2 at gmail.com
Mon Feb 16 08:39:35 EST 2009
Garrett Cooper wrote:
> On Mon, Feb 16, 2009 at 5:15 AM, Fernando M. Maresca <fmaresca at gmail.com>
> wrote:
>> Hello, thanks for the answer.
>>
>> On Mon, Feb 16, 2009 at 05:07:45AM -0800, Garrett Cooper wrote:
>>> You can actually set sys.std[err|out] to your ?file? descriptor of
>>> choice in python (it has to have read, write, and flush methods, IIRC
>>> to function). The only thing is (like all things dealing with multiple
>>> file descriptors like std[err|out]) the output may come in out of
>>> order due to flushing and insertion into the buffers, but it shouldn't
>>> be as much of an issue considering that the file descriptor for both
>>> items is the same descriptor, but this is just a note of forewarning.
>> Yes, but I'm trying to use *TimedRotating*FileHandler, which makes the
>> fd of the logfile change in every rotation of the logfile. So the direct
>> approach of std[out|err] redirection to the logfile fd obtained from
>> the logger instance is unusable (it works fine with a simple file
>> handler).
>> I'm looking into this because I really need rotating, because when
>> debugging is on, large amounts of data are logged, and because I like
>> the logging module approach in every aspect.
>
> I cannot comment about this because I haven't used this module before.
>
>> Also, may it be possible to derive the class and add a file-like write
>> method? Anyone using logging in this manner?
>
> Indeed, yes. That's sort of what I was hinting at with my statement
> that you must have a read, write and flush method for your class. Now
> that I think back, other modules like pexpect implement simple logic
> to deal with writing to psuedo file descriptors as well.
>
> Locking the data streams may or may not be required -- I'm not sure
> what the underlaying portions of python expose or do not expose in
> terms of scheduling in output streams.
>
> HTH,
> -Garrett
> --
> http://mail.python.org/mailman/listinfo/python-list
I use this, but for my own specialized purpose:
import atexit
class logger (object):
def __init__ (self, name):
self.name = name # final file name
self.f = open (self.name, 'w')
atexit.register (self.__del__)
def write (self, stuff):
self.f.write (stuff)
def close (self):
self.f.close()
def flush (self):
self.f.flush()
def reopen (self):
self.f.flush()
self.f.close()
os.rename (self.name, self.name + '.old')
self.f = open (self.name, 'w')
def __del__ (self):
try:
os.remove (self.name + '.old')
except:
pass
More information about the Python-list
mailing list