Re: Preserving logging streams through a daemon.DaemonContext switch
Ben Finney wrote: Vinay Sajip vinay_sa...@yahoo.co.uk writes: On Apr 8, 1:58 pm, Rebelo puntabl...@gmail.com wrote: Vinay Sajip wrote: My guess is - files_preserve needs to be passed a file handle and not alogginghandler. This is correct. As the ‘DaemonContext.__init__’ docstring says:: | `files_preserve` | :Default: ``None`` | | List of files that should *not* be closed when starting the | daemon. If ``None``, all open file descriptors will be closed. | | Elements of the list are file descriptors (as returned by a file | object's `fileno()` method) or Python `file` objects. Each | specifies a file that is not to be closed during daemon start. do you know where can i find good documnetation for python-daemon? The docstrings and PEP 3143 are currently the best documentation for ‘python-daemon’; both describe the ‘files_preserve’ option as above. http://groups.google.com/group/comp.lang.python/msg/851ce78e53812ade As expressed in the referenced message, I would welcome someone writing better documentation and contributing patches. It may lead you to more information. The thread shows that Sean DiZazzo got logging working with the package. It's important to note (as you did, thanks Vinay) that none of the ‘logging.handlers’ are file descriptors. Nor are they file objects. If we can get a Python ‘file’ object, we can use its ‘fileno()’ method to get the file descriptor. So how do we get the file object for a logging handler? The ‘logging’ module documentation doesn't mention any way to get at the stream object for the handlers. But looking at the source code for ‘StreamHandler’ on my system, ‘/usr/lib/python2.5/logging/__init__.py’, shows a ‘stream’ attribute that is bound to the stream object. It's not marked private (i.e. it's not named with a leading underscore), so one presumes it is part of the public API. I think you just have to pass the file object used by the handler (fh.stream) in the files_preserve array. Not quite. As the docs specify, you need to pass the *file descriptors* for the files you want preserved. For regular Python file objects, the ‘file.fileno()’ method returns the file descriptor: lh = logging.handlers.TimedRotatingFileHandler( LOG_FILENAME, # … ) log_stream_descriptor = lh.stream.fileno() daemon_context.files_preserve = [log_stream_descriptor] thank you both for a detailed explanation. -- http://mail.python.org/mailman/listinfo/python-list
Re: Preserving logging streams through a daemon.DaemonContext switch (was: daemon.DaemonContext)
On Apr 9, 12:46 am, Ben Finney ben+pyt...@benfinney.id.au wrote: I think you just have to pass the file object used by the handler (fh.stream) in the files_preserve array. Not quite. As the docs specify, you need to pass the *file descriptors* for the files you want preserved. Okay, but the docstring you quoted: Elements of the list are file descriptors (as returned by a file object's `fileno()` method) or Python `file` objects. implies that fh.stream would work as well as fh.stream.fileno(), and I presume you internally do a hasattr(thingy, 'fileno') to get the descriptor. Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Re: Preserving logging streams through a daemon.DaemonContext switch
Vinay Sajip vinay_sa...@yahoo.co.uk writes: On Apr 9, 12:46 am, Ben Finney ben+pyt...@benfinney.id.au wrote: Not quite. As the docs specify, you need to pass the *file descriptors* for the files you want preserved. Okay, but the docstring you quoted: Elements of the list are file descriptors (as returned by a file object's `fileno()` method) or Python `file` objects. implies that fh.stream would work as well as fh.stream.fileno() You're quite right, I made a mistake in what I wrote above. Rebelo puntabl...@gmail.com writes: Ben Finney wrote: So how do we get the file object for a logging handler? The ‘logging’ module documentation doesn't mention any way to get at the stream object for the handlers. But looking at the source code for ‘StreamHandler’ on my system, ‘/usr/lib/python2.5/logging/__init__.py’, shows a ‘stream’ attribute that is bound to the stream object. It's not marked private (i.e. it's not named with a leading underscore), so one presumes it is part of the public API. […] lh = logging.handlers.TimedRotatingFileHandler( LOG_FILENAME, # … ) log_stream_descriptor = lh.stream.fileno() daemon_context.files_preserve = [log_stream_descriptor] The above can then be simplified as: lh = logging.handlers.TimedRotatingFileHandler( LOG_FILENAME, # … ) daemon_context.files_preserve = [lh.stream] thank you both for a detailed explanation. I hope this helps you to make better use of ‘python-daemon’, and thank you for raising questions here. This is a good addition for the Frequently Asked Questions document; I will do this and it will be available in the next version of the library. -- \ “I prayed for twenty years but received no answer until I | `\ prayed with my legs.” —Frederick Douglass, escaped slave | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Preserving logging streams through a daemon.DaemonContext switch (was: daemon.DaemonContext)
Vinay Sajip vinay_sa...@yahoo.co.uk writes: On Apr 8, 1:58 pm, Rebelo puntabl...@gmail.com wrote: Vinay Sajip wrote: My guess is - files_preserve needs to be passed a file handle and not alogginghandler. This is correct. As the ‘DaemonContext.__init__’ docstring says:: | `files_preserve` | :Default: ``None`` | | List of files that should *not* be closed when starting the | daemon. If ``None``, all open file descriptors will be closed. | | Elements of the list are file descriptors (as returned by a file | object's `fileno()` method) or Python `file` objects. Each | specifies a file that is not to be closed during daemon start. do you know where can i find good documnetation for python-daemon? The docstrings and PEP 3143 are currently the best documentation for ‘python-daemon’; both describe the ‘files_preserve’ option as above. http://groups.google.com/group/comp.lang.python/msg/851ce78e53812ade As expressed in the referenced message, I would welcome someone writing better documentation and contributing patches. It may lead you to more information. The thread shows that Sean DiZazzo got logging working with the package. It's important to note (as you did, thanks Vinay) that none of the ‘logging.handlers’ are file descriptors. Nor are they file objects. If we can get a Python ‘file’ object, we can use its ‘fileno()’ method to get the file descriptor. So how do we get the file object for a logging handler? The ‘logging’ module documentation doesn't mention any way to get at the stream object for the handlers. But looking at the source code for ‘StreamHandler’ on my system, ‘/usr/lib/python2.5/logging/__init__.py’, shows a ‘stream’ attribute that is bound to the stream object. It's not marked private (i.e. it's not named with a leading underscore), so one presumes it is part of the public API. I think you just have to pass the file object used by the handler (fh.stream) in the files_preserve array. Not quite. As the docs specify, you need to pass the *file descriptors* for the files you want preserved. For regular Python file objects, the ‘file.fileno()’ method returns the file descriptor: lh = logging.handlers.TimedRotatingFileHandler( LOG_FILENAME, # … ) log_stream_descriptor = lh.stream.fileno() daemon_context.files_preserve = [log_stream_descriptor] -- \ “Only the educated are free.” —Epictetus, _Discourses_ | `\ | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list