forked from openstack/openstacksdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rationalize logging helpers and docs
shade and openstacksdk each have a logging doc and a logging setup helper function. They both basically do the same thing, and we have a TODO item about collapsing them. This moves openstack.utils.enable_logging to openstack.enable_logging (leaving behind a compat piece at openstack.utils.enable_logging) It adds the shade functionality to it, and also changes the behavior to match shade's WRT behavior when no parameters are passed (defaults to logging to stdout) Update the codebase to call openstack._log.setup_logging instead of logging.getLogger directly, as setup_logging attaches a NullHandler by default. Collapse the docs into a single document. There were only two places where openstacksdk was already logging to something other than 'openstack'. Collapse those down to 'openstack' until we come up with a reason to break them out more logically. Change-Id: I45fd5ffd18255450d38a1f56c80f5c157ea19ae3
- Loading branch information
Showing
35 changed files
with
407 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,113 @@ | ||
======= | ||
Logging | ||
======= | ||
|
||
Logging can save you time and effort when developing your code or looking | ||
for help. If your code is not behaving how you expect it to, enabling and | ||
configuring logging can quickly give you valuable insight into the root | ||
cause of the issue. If you need help from the OpenStack community, the | ||
logs can help the people there assist you. | ||
.. note:: TODO(shade) This document is written from a shade POV. It needs to | ||
be combined with the existing logging guide, but also the logging | ||
systems need to be rationalized. | ||
|
||
`openstacksdk` uses `Python Logging`_. As `openstacksdk` is a library, it does | ||
not configure logging handlers automatically, expecting instead for that to be | ||
the purview of the consuming application. | ||
|
||
Simple Usage | ||
------------ | ||
|
||
For consumers who just want to get a basic logging setup without thinking | ||
about it too deeply, there is a helper method. If used, it should be called | ||
before any other openstacksdk functionality. | ||
|
||
.. autofunction:: openstack.enable_logging | ||
|
||
.. code-block:: python | ||
.. note:: By default, no logging is done. | ||
import openstack | ||
openstack.enable_logging() | ||
Enable SDK Logging | ||
------------------ | ||
The ``stream`` parameter controls the stream where log message are written to. | ||
It defaults to `sys.stdout` which will result in log messages being written | ||
to STDOUT. It can be set to another output stream, or to ``None`` to disable | ||
logging to the console. | ||
|
||
The ``path`` parameter sets up logging to log to a file. By default, if | ||
``path`` is given and ``stream`` is not, logging will only go to ``path``. | ||
|
||
To enable logging you use :func:`~openstack.utils.enable_logging`. | ||
You can combine the ``path`` and ``stream`` parameters to log to both places | ||
simultaneously. | ||
|
||
The ``debug`` parameter controls the logging level. Set ``debug=True`` to | ||
log debug and higher messages. Set ``debug=False`` to log warning and higher | ||
messages. | ||
To log messages to a file called ``openstack.log`` and the console on | ||
``stdout``: | ||
|
||
To log debug and higher messages:: | ||
.. code-block:: python | ||
import sys | ||
from openstack import utils | ||
utils.enable_logging(debug=True, stream=sys.stdout) | ||
utils.enable_logging(debug=True, path='openstack.log', stream=sys.stdout) | ||
The ``path`` parameter controls the location of a log file. If set, this | ||
parameter will send log messages to a file using a | ||
:py:class:`~logging.FileHandler`. | ||
To log messages to a file called ``openstack.log``:: | ||
`openstack.enable_logging` also sets up a few other loggers and | ||
squelches some warnings or log messages that are otherwise uninteresting or | ||
unactionable by an openstacksdk user. | ||
|
||
from openstack import utils | ||
Advanced Usage | ||
-------------- | ||
|
||
utils.enable_logging(debug=True, path='openstack.log') | ||
`openstacksdk` logs to a set of different named loggers. | ||
|
||
The ``stream`` parameter controls the stream where log message are written to. | ||
If set to ``sys.stdout`` or ``sys.stderr``, this parameter will send log | ||
messages to that stream using a :py:class:`~logging.StreamHandler` | ||
Most of the logging is set up to log to the root ``openstack`` logger. | ||
There are additional sub-loggers that are used at times, primarily so that a | ||
user can decide to turn on or off a specific type of logging. They are listed | ||
below. | ||
|
||
To log messages to the console on ``stdout``:: | ||
openstack.config | ||
Issues pertaining to configuration are logged to the ``openstack.config`` | ||
logger. | ||
|
||
import sys | ||
from openstack import utils | ||
openstack.task_manager | ||
`openstacksdk` uses a Task Manager to perform remote calls. The | ||
``openstack.task_manager`` logger emits messages at the start and end | ||
of each Task announcing what it is going to run and then what it ran and | ||
how long it took. Logging ``openstack.task_manager`` is a good way to | ||
get a trace of external actions `openstacksdk` is taking without full | ||
`HTTP Tracing`_. | ||
|
||
utils.enable_logging(debug=True, stream=sys.stdout) | ||
openstack.iterate_timeout | ||
When `openstacksdk` needs to poll a resource, it does so in a loop that waits | ||
between iterations and ultimately times out. The | ||
``openstack.iterate_timeout`` logger emits messages for each iteration | ||
indicating it is waiting and for how long. These can be useful to see for | ||
long running tasks so that one can know things are not stuck, but can also | ||
be noisy. | ||
|
||
You can combine the ``path`` and ``stream`` parameters to log to both places | ||
simultaneously. | ||
openstack.fnmatch | ||
`openstacksdk` will try to use `fnmatch`_ on given `name_or_id` arguments. | ||
It's a best effort attempt, so pattern misses are logged to | ||
``openstack.fnmatch``. A user may not be intending to use an fnmatch | ||
pattern - such as if they are trying to find an image named | ||
``Fedora 24 [official]``, so these messages are logged separately. | ||
|
||
To log messages to a file called ``openstack.log`` and the console on | ||
``stdout``:: | ||
.. _fnmatch: https://pymotw.com/2/fnmatch/ | ||
|
||
import sys | ||
from openstack import utils | ||
HTTP Tracing | ||
------------ | ||
|
||
utils.enable_logging(debug=True, path='openstack.log', stream=sys.stdout) | ||
HTTP Interactions are handled by `keystoneauth`_. If you want to enable HTTP | ||
tracing while using openstacksdk and are not using `openstack.enable_logging`, | ||
set the log level of the ``keystoneauth`` logger to ``DEBUG``. | ||
|
||
For more information see https://docs.openstack.org/keystoneauth/latest/using-sessions.html#logging | ||
|
||
Enable requests Logging | ||
----------------------- | ||
.. _keystoneauth: https://docs.openstack.org/keystoneauth/latest/ | ||
|
||
The SDK depends on a small number other libraries. Notably, it uses | ||
`requests <https://pypi.python.org/pypi/requests>`_ for its transport layer. | ||
To get even more information about the request/response cycle, you enable | ||
logging of requests the same as you would any other library. | ||
Python Logging | ||
-------------- | ||
|
||
To log messages to the console on ``stdout``:: | ||
Python logging is a standard feature of Python and is documented fully in the | ||
Python Documentation, which varies by version of Python. | ||
|
||
import logging | ||
import sys | ||
For more information on Python Logging for Python v2, see | ||
https://docs.python.org/2/library/logging.html. | ||
|
||
logger = logging.getLogger('requests') | ||
formatter = logging.Formatter( | ||
'%(asctime)s %(levelname)s: %(name)s %(message)s') | ||
console = logging.StreamHandler(sys.stdout) | ||
console.setFormatter(formatter) | ||
logger.setLevel(logging.DEBUG) | ||
logger.addHandler(console) | ||
For more information on Python Logging for Python v3, see | ||
https://docs.python.org/3/library/logging.html. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.