4.7.5.4. Logging utilities

Logging utilities.

class axonius_api_client.logs.HideFormatter(fmt=None, datefmt=None, style='%')[source]

Bases: Formatter

Hide the rest of the line for any lines against HIDE_REGEX.

HIDE_ENABLED: bool = True

Enable hiding of matches to HIDE_REGEX.

HIDE_REGEX: typing.Pattern = re.compile('(password|secret).*', re.IGNORECASE)

Pattern of sensitive info to hide.

HIDE_REPLACE: str = '\\1 ...REST OF LINE HIDDEN...'

Value to replace matches to HIDE_REGEX with.

format(record)[source]

Pass.

__init__(fmt=None, datefmt=None, style='%')

Initialize the formatter with specified format strings.

Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.

Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting, str.format() ({}) formatting or string.Template formatting in your format string.

Changed in version 3.2: Added the style parameter.

converter()
gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,

tm_sec, tm_wday, tm_yday, tm_isdst)

Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT). When ‘seconds’ is not passed in, convert the current time instead.

If the platform supports the tm_gmtoff and tm_zone, they are available as attributes only.

default_msec_format = '%s,%03d'
default_time_format = '%Y-%m-%d %H:%M:%S'
formatException(ei)

Format and return the specified exception information as a string.

This default implementation just uses traceback.print_exception()

formatMessage(record)
formatStack(stack_info)

This method is provided as an extension point for specialized formatting of stack information.

The input data is a string as returned from a call to traceback.print_stack(), but with the last trailing newline removed.

The base implementation just returns the value passed in.

formatTime(record, datefmt=None)

Return the creation time of the specified LogRecord as formatted text.

This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the ‘converter’ attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the ‘converter’ attribute in the Formatter class.

usesTime()

Check if the format uses the creation time of the record.

axonius_api_client.logs.get_echoer(level)[source]

Pass.

Parameters:

level (typing.Union[int, str]) –

Return type:

typing.Callable

axonius_api_client.logs.get_log_method(obj, level=None)[source]

Pass.

Parameters:
Return type:

typing.Callable

axonius_api_client.logs.gmtime()[source]

Set the logging system to use GMT for time strings.

axonius_api_client.logs.localtime()[source]

Set the logging system to use local time for time strings.

axonius_api_client.logs.get_obj_log(obj, level=None, **kwargs)[source]

Get a child logger for an object.

Parameters:
Return type:

logging.Logger

axonius_api_client.logs.set_log_level(obj, level=None)[source]

Set a logger or handler to a log level.

Parameters:
axonius_api_client.logs.str_level(level)[source]

Get a logging level in str format.

Parameters:

level (typing.Union[int, str]) – level to get str format of

Raises:

ToolsError – if level is not mappable as an int or str to a known logger level

Return type:

str

axonius_api_client.logs.add_stderr(obj, level='debug', hname='handler_stderr', fmt='%(levelname)-8s [%(name)s] %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p %Z')[source]

Add a StreamHandler to a logger object that outputs to STDERR.

Parameters:
  • obj (logging.Logger) – logger obj to add handler to

  • level (typing.Union[int, str]) – log level to assign to handler

  • hname (str) – name to assign to handler

  • fmt (str) – logging format to use

  • datefmt (str) – date format to use

Return type:

logging.StreamHandler

axonius_api_client.logs.add_stdout(obj, level='debug', hname='handler_stdout', fmt='%(levelname)-8s [%(name)s] %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p %Z')[source]

Add a StreamHandler to a logger object that outputs to STDOUT.

Parameters:
  • obj (logging.Logger) – logger obj to add handler to

  • level (typing.Union[int, str]) – log level to assign to handler

  • hname (str) – name to assign to handler

  • fmt (str) – logging format to use

  • datefmt (str) – date format to use

Return type:

logging.StreamHandler

axonius_api_client.logs.add_file(obj, level='debug', hname='handler_file', file_path='/home/docs/checkouts/readthedocs.org/user_builds/axonius-api-client/checkouts/stable/docs', file_name='axonius_api_client.log', file_path_mode=448, max_mb=5, max_files=5, fmt='%(asctime)s %(levelname)-8s [%(name)s:%(module)s:%(funcName)s:%(pathname)s:%(lineno)d] %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p %Z')[source]

Add a RotatingFileHandler to a logger object.

Parameters:
  • obj (logging.Logger) – logger obj to add handler to

  • level (typing.Union[int, str]) – log level to assign to handler

  • hname (str) – name to assign to handler

  • fmt (str) – logging format to use

  • datefmt (str) – date format to use

  • file_path (typing.Union[pathlib.Path, str]) – path to write file_name to

  • file_name (typing.Union[pathlib.Path, str]) – name of file to write log entries to

  • file_path_mode – permissions to assign to directory for log file when created

  • max_mb (int) – rollover trigger in MB

  • max_files (int) – max files to keep for rollover

Return type:

logging.handlers.RotatingFileHandler

axonius_api_client.logs.add_null(obj, traverse=True, hname='NULL')[source]

Add a NullHandler to a logger if it has no handlers.

Parameters:
  • obj (logging.Logger) – logger obj to add handler to

  • traverse (bool) – traverse the logger obj supplied up to the root logger

  • hname – name to assign to handler

Return type:

typing.Optional[logging.NullHandler]

axonius_api_client.logs.add_handler(obj, htype, hname, fmt='%(levelname)-8s [%(name)s] %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p %Z', level=None, **kwargs)[source]

Add a handler to a logger obj.

Parameters:
  • obj (logging.Logger) – logger obj to add handler to

  • htype (logging.Handler) – handler class to instantiate

  • level (typing.Union[int, str, None]) – level to assign to handler obj

  • hname (str) – name to assign to handler obj

  • fmt (str) – logging format to assign to handler obj

  • datefmt (str) – date format to assign to handler obj

  • **kwargs – passed to instantiation of htype

Return type:

logging.Handler

axonius_api_client.logs.del_stderr(obj, traverse=True, hname='handler_stderr')[source]

Remove the STDERR StreamHandler from a logger if found.

Parameters:
  • obj (logging.Logger) – logger obj to remove handler from

  • traverse (bool) – traverse the logger obj supplied up to the root logger

  • hname (str) – name of handler to search for and remove

Return type:

typing.Dict[str, typing.List[logging.Handler]]

axonius_api_client.logs.del_stdout(obj, traverse=True, hname='handler_stdout')[source]

Remove the STDOUT StreamHandler from a logger if found.

Parameters:
  • obj (logging.Logger) – logger obj to remove handler from

  • traverse (bool) – traverse the logger obj supplied up to the root logger

  • hname (str) – name of handler to search for and remove

Return type:

typing.Dict[str, typing.List[logging.Handler]]

axonius_api_client.logs.del_file(obj, traverse=True, hname='handler_file')[source]

Remove the RotatingFileHandler from a logger if found.

Parameters:
  • obj (logging.Logger) – logger obj to remove handler from

  • traverse (bool) – traverse the logger obj supplied up to the root logger

  • hname – name of handler to search for and remove

Return type:

typing.Dict[str, typing.List[logging.Handler]]

axonius_api_client.logs.del_null(obj, traverse=True, hname='NULL')[source]

Remove the NullHandler from a logger if found.

Parameters:
  • obj (logging.Logger) – logger obj to remove handler from

  • traverse (bool) – traverse the logger obj supplied up to the root logger

  • hname (str) – name of handler to search for and remove

Return type:

typing.Dict[str, typing.List[logging.Handler]]

axonius_api_client.logs.del_handler(obj, hname='', htype=None, traverse=True)[source]

Remove the NullHandler from a logger if found.

Parameters:
  • obj (logging.Logger) – logger obj to remove handler from

  • traverse (bool) – traverse the logger obj supplied up to the root logger

  • hname (str) – name of handler to search for and remove

  • htype (typing.Optional[logging.Handler]) – type of handler to find and remove

Return type:

typing.Dict[str, typing.List[logging.Handler]]

axonius_api_client.logs.find_handlers(obj, hname='', htype=None, traverse=True)[source]

Remove the NullHandler from a logger if found.

Notes

  • will remove handler if hname supplied and handler obj name matches

  • will remove handler if htype supplied and handler obj type matches

Parameters:
Return type:

typing.Dict[str, typing.List[logging.Handler]]

axonius_api_client.logs.handle_unhandled_exception(exc_type, exc_value, exc_traceback)[source]

Log unhandled exceptions.