Configure Logging
Faceware Portal libraries for Python provide logging output using the standard Python logging library.
The general process to work with logging is as follows:
- Acquire the logging object and set the logging level.
- Register a handler for the logging stream.
Set logging levels
If you've got many modules and you only want to turn logging for pyportal
import logging
# ...
# Acquire the logger for a library (pyportal in this example)
logger = logging.getLogger('pyportal')
# Set the desired logging level
logger.setLevel(logging.INFO)
- This example aquires the logger for the
pyportal
library, then sets the logging level tologging.INFO
- You can call
logger.setLevel
at any time to change the logging level for different segments of the code.
Logging levels are the same as the standard logging library levels. The following table describes the general use of these logging levels in the Faceware Portal libraries for Python:
Logging level | Typical use |
---|---|
logging.ERROR | Failures where the application is unlikely to recover (such as out of memory). |
logging.WARNING (default) | A function fails to perform its intended task (but not when the function can recover, such as retrying a REST API call). Functions typically log a warning when raising exceptions. The warning level automatically enables the error level. |
logging.INFO | Function operates normally or a service call is canceled. Info events typically include requests and responses. The info level automatically enables the error and warning levels. |
logging.DEBUG | Detailed information that is commonly used for troubleshooting and includes a stack trace for exceptions. The debug level automatically enables the info, warning, and error levels. CAUTION: The debug level may include sensitive information such as account keys in headers and other credentials. Be sure to protect these logs to avoid compromising security. |
logging.NOTSET | Disable all logging. |
Register a log stream handler
To capture logging output, you must register at least one log stream handler in your code:
import logging
import sys
from faceware.pyportal import PortalClient
# Direct logging output to stdout.
logger = logging.getLogger('pyportal')
logger.setLevel(level=logging.DEBUG)
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
# pass the logger to PortalClient
client = PortalClient(
access_token="{YOUR-ACCESS-TOKEN}",
organization_id="{YOUR-ORGANIZATION-ID}",
parent_logger=logger
)
This example registers a handler that directs log output to stdout. You can use other types of handlers as described on logging handlers in the Python documentation or use the standard logging.basicConfig method.