Logging

_images/logging.png

Structlog based beautiful logging.

This configuration offers YAML rendering for the json key in every log calls.

import cli2

cli2.log.warn("something happened", custom=key, json=will_be_prettyfied)

In general, you’ll want to use want to use:

  • log.debug(): to indicate that something is going to happen, or a request is being sent

  • log.info(): to indicate that something has happened, or a response was received

  • log.warn(): something hasn’t happened as expected, but your program can recover from that (ie. retrying a connection)

  • log.error(): your program couldn’t perform some function

  • log.critical(): your program may not be able to continue running

Anyway, it’s structlog so you can also create bound loggers that will carry on the given parameters:

import cli2
log = cli2.log.bind(some='var')
log.warn('hello')  # will log with some=var

Log level is set to warning by default, configurable over environment variables.

LOG_LEVEL

Setting this to INFO, DEBUG, or any other log level is safe.

LOG_FILE

Path to log file to use, with a couple of special values:

  • if LOG_FILE=auto, then a path will be calculated in ~/.local/cli2/log,

  • if LOG_FILE=none, then there will be no file logging.

Default: auto

DEBUG

Setting this will set LOG_LEVEL to DEBUG, but also activate otherwise hidden outputs, such as, in cli2.client: long pagination outputs, secret/masked variables. This variable is designed to never be enabled in automated runs, to avoid leaking way to much information in say Ansible Tower and stuff like that. But if you’re debugging manually, you will surely need that at some point.

cli2.log.configure(log_file=None)[source]

Configure logging.

Parameters:

log_file – override for LOG_FILE.

cli2.log.parse(data)[source]

Parse log file data into a list of entries.

Parameters:

data – Contents of a log file.