Skip to content

Commit 1067baf

Browse files
miss-islingtonpablogsal
authored andcommitted
[3.11] [docs] Update logging cookbook with recipe for using a logger like an output… (GH-97730) (GH-97735)
1 parent c8bdf33 commit 1067baf

File tree

1 file changed

+0
-77
lines changed

1 file changed

+0
-77
lines changed

Doc/howto/logging-cookbook.rst

-77
Original file line numberDiff line numberDiff line change
@@ -3703,83 +3703,6 @@ instance). Then, you'd get this kind of result:
37033703
Of course, these above examples show output according to the format used by
37043704
:func:`~logging.basicConfig`, but you can use a different formatter when you
37053705
configure logging.
3706-
=======
3707-
How to treat a logger like an output stream
3708-
-------------------------------------------
3709-
3710-
Sometimes, you need to interface to a third-party API which expects a file-like
3711-
object to write to, but you want to direct the API's output to a logger. You
3712-
can do this using a class which wraps a logger with a file-like API.
3713-
Here's a short script illustrating such a class:
3714-
3715-
.. code-block:: python
3716-
3717-
import logging
3718-
3719-
class LoggerWriter:
3720-
def __init__(self, logger, level):
3721-
self.logger = logger
3722-
self.level = level
3723-
3724-
def write(self, message):
3725-
if message != '\n': # avoid printing bare newlines, if you like
3726-
self.logger.log(self.level, message)
3727-
3728-
def flush(self):
3729-
# doesn't actually do anything, but might be expected of a file-like
3730-
# object - so optional depending on your situation
3731-
pass
3732-
3733-
def close(self):
3734-
# doesn't actually do anything, but might be expected of a file-like
3735-
# object - so optional depending on your situation. You might want
3736-
# to set a flag so that later calls to write raise an exception
3737-
pass
3738-
3739-
def main():
3740-
logging.basicConfig(level=logging.DEBUG)
3741-
logger = logging.getLogger('demo')
3742-
info_fp = LoggerWriter(logger, logging.INFO)
3743-
debug_fp = LoggerWriter(logger, logging.DEBUG)
3744-
print('An INFO message', file=info_fp)
3745-
print('A DEBUG message', file=debug_fp)
3746-
3747-
if __name__ == "__main__":
3748-
main()
3749-
3750-
When this script is run, it prints
3751-
3752-
.. code-block:: text
3753-
3754-
INFO:demo:An INFO message
3755-
DEBUG:demo:A DEBUG message
3756-
3757-
You could also use ``LoggerWriter`` to redirect ``sys.stdout`` and
3758-
``sys.stderr`` by doing something like this:
3759-
3760-
.. code-block:: python
3761-
3762-
import sys
3763-
3764-
sys.stdout = LoggerWriter(logger, logging.INFO)
3765-
sys.stderr = LoggerWriter(logger, logging.WARNING)
3766-
3767-
You should do this *after* configuring logging for your needs. In the above
3768-
example, the :func:`~logging.basicConfig` call does this (using the
3769-
``sys.stderr`` value *before* it is overwritten by a ``LoggerWriter``
3770-
instance). Then, you'd get this kind of result:
3771-
3772-
.. code-block:: pycon
3773-
3774-
>>> print('Foo')
3775-
INFO:demo:Foo
3776-
>>> print('Bar', file=sys.stderr)
3777-
WARNING:demo:Bar
3778-
>>>
3779-
3780-
Of course, the examples above show output according to the format used by
3781-
:func:`~logging.basicConfig`, but you can use a different formatter when you
3782-
configure logging.
37833706

37843707
.. patterns-to-avoid:
37853708

0 commit comments

Comments
 (0)