Logging

The pwfdf-api uses the pwfdf-api logging namespace to report progress and status. Although the command line interface includes options to automatically configure logging options, you will need to configure the logs explicitly when working within a Python session. In addition to Python’s standard logging library, you can use the pwfdf_api.logging package to quickly configure the pwfdf-api logger.

In brief, the module is split into two modules. The console module contains commands that configure the logging stream to sys.stderr (the standard logging stream), and the file module contains functions that configure logging to file.

Each module contains three commands designed to mimic the logging options from the pwfdf-api command line interface. These are:

Command

Description

verbose

Configures a logging stream to log at the DEBUG level

info

Configures a logging stream to log at the INFO level

quiet

Configures a logging stream to log at the WARNING level

So you can mimic the command line console logs using:

# Mimics the --verbose option
pwfdf_api.logging.console.verbose()

# Mimics the default log
pwfdf_api.logging.console.info()

# Mimics the --quiet option
pwfdf_api.logging.console.quiet()

To prevent duplicate console logs, the commands in the console module will delete any pre-existing pwfdf-api logging streams to sys.stderr. However, you can have multiple file logs simultaneously. To configure a file log, use the corresponding command from the file module. Note that these commands require a file path as input:

# Creates a DEBUG-level file log
pwfdf_api.logging.file.verbose('my-debug-log.txt')

# And an INFO-level log
pwfdf_api.logging.file.info('my-info-log.txt')

# And a WARNING-level log
pwfdf_api.logging.file.quiet('my-warning-log.txt')

Log Format

When you configure the console log, it will preserve the logging format of any pre-existing pwfdf-api logging stream to sys.stderr. Alternatively, use the format input to specify an explicit logging format string. For example:

pwfdf_api.logging.console.verbose(format='%(asctime)s - %(message)s')

Note that the following command will create a file log formatted in the command line style:

format = "%(asctime)s - %(name)s - %(message)s"
pwfdf_api.logging.file.verbose('my-log.txt', format=format)