discord.py/docs/logging.rst
Hornwitser 320cd39b6a Print to stderr in on_error
Apparently the clever hack for logging in on_error was not so clever
after all.  If logging isn't configured, by the logging modules
definition of not configured, which is root logger not having an
Handlers attached, it will call logging.basicConfig().  Which messes up
setups that define handlers for other loggers than the root logger.

Going directly to the root logger rather than using the broken
convenience methods for logger is not an option either, as logger before
Python 3.2 does not have lastResort on the root logger, and prints an
error when invoked without any handlers.

Resolve by printing tracebacks to stderr by default in on_error.
2015-10-22 22:07:50 +02:00

47 lines
1.6 KiB
ReStructuredText

.. versionadded:: 0.6.0
.. _logging_setup:
Setting Up Logging
===================
*discord.py* logs errors and debug information via the `logging`_ python
module. It is strongly recommended that the logging module is
configured, as no errors or warnings will be output if it is not set up.
Configuration of the ``logging`` module can be as simple as::
import logging
logging.basicConfig(level=logging.INFO)
Placed at the start of the application. This will output the logs from
discord as well as other libraries that uses the ``logging`` module
directly to the console.
The optinal ``level`` argument specifies what level of events to log
out and can any of ``CRITICAL``, ``ERROR``, ``WARNING``, ``INFO``, and
``DEBUG`` and if not specified defaults to ``WARNING``.
More advance setups are possible with the ``logging`` module. To for
example write the logs to a file called ``discord.log`` instead of
outputting them to to the console the following snippet can be used::
import discord
import logging
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
This is recommended, especially at verbose levels such as ``INFO``,
and ``DEBUG`` as there are a lot of events logged and it would clog the
stdout of your program.
For more information, check the documentation and tutorial of the
`logging`_ module.
.. _logging: https://docs.python.org/2/library/logging.html