Files
openstacksdk/doc/source/users/guides/logging.rst
liuxiaoyang f18dc29eb0 Add doc8 rule and check doc/source files
doc8 is a linter for documents and used in openstack-manuals.
It is better to enforce document linters for simple checking.
This change is to add doc8 in tox file and fix line too long
in some files.

The current rules are as bellow:
- invalid rst format - D000
- lines should not be longer than 79 characters - D001
  - RST exception: line with no whitespace except in the beginning
  - RST exception: lines with http or https urls
  - RST exception: literal blocks
  - RST exception: rst target directives
- no trailing whitespace - D002
- no tabulation for indentation - D003
- no carriage returns (use unix newlines) - D004
- no newline at end of file - D005

Change-Id: I5b409fbfd95e05921310c6ecf4afea0220fb0bf0
2017-08-23 08:27:31 -04:00

2.4 KiB

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

By default, no logging is done.

Enable SDK Logging

To enable logging you use ~openstack.utils.enable_logging.

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 debug and higher messages:

import sys
from openstack import utils

utils.enable_logging(debug=True, 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~logging.FileHandler.

To log messages to a file called openstack.log:

from openstack import utils

utils.enable_logging(debug=True, path='openstack.log')

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~logging.StreamHandler

To log messages to the console on stdout:

import sys
from openstack import utils

utils.enable_logging(debug=True, stream=sys.stdout)

You can combine the path and stream parameters to log to both places simultaneously.

To log messages to a file called openstack.log and the console on stdout:

import sys
from openstack import utils

utils.enable_logging(debug=True, path='openstack.log', stream=sys.stdout)

Enable requests Logging

The SDK depends on a small number other libraries. Notably, it uses 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.

To log messages to the console on stdout:

import logging
import sys

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)