ArcPy-Logging Documentation#

ArcPy-Logging is a succinct project providing tight integration of ArcPy messaging into Python logging using standard Python logging. If all you want is a quick logging object, grab the get_logger() method. If you want to create your own logger, use the ArcpyHandler object.

arcpy_logging#

ArcpyHandler([level])

Logging message handler capable of routing logging through ArcPy AddMessage, AddWarning and AddError methods.

get_logger([logger_name, log_level, ...])

Get Python Logger configured to provide stream, file or, if available, ArcPy output.

log_pandas_df(logger, pandas_df, title[, ...])

Helper function facilitating outputting a Pandas DataFrame into a logfile.

class arcpy_logging.ArcpyHandler(level=10)[source]#

Logging message handler capable of routing logging through ArcPy AddMessage, AddWarning and AddError methods. DEBUG and INFO logging messages are be handled by the AddMessage method. WARNING logging messages are handled by the AddWarning method. ERROR and CRITICAL logging messages are handled by the AddError method.

Basic use consists of the following.

logger = logging.getLogger('arcpy-logger')
logger.setLevel('INFO')

ah = ArcpyHandler()
logger.addHandler(ah)

logger.debug('nauseatingly detailed debugging message')
logger.info('something actually useful to know')
logger.warning('The sky may be falling')
logger.error('The sky is falling.)
logger.critical('The sky appears to be falling because a giant meteor is colliding with the earth.')

Initializes the instance - basically setting the formatter to None and the filter list to empty.

emit(record)

type record:

LogRecord

emit(record)[source]#
Parameters:

record (LogRecord) – Record containing all information needed to emit a new logging event.

Note

This method should not be called directly, but rather enables the Logger methods to be able to use this handler correctly.

Return type:

None

arcpy_logging.get_logger(logger_name='arcpy-logger', log_level='INFO', logfile_pth=None, propagate=False)[source]#

Get Python Logger configured to provide stream, file or, if available, ArcPy output. The way the method is set up, logging will be routed through ArcPy messaging using ArcpyHandler if ArcPy is available. If ArcPy is not available, messages will be sent to the console using a StreamHandler. Next, if the logfile_path is provided, log messages will also be written to the provided path to a logfile using a FileHandler.

Valid log_level inputs include:

  • DEBUG - Detailed information, typically of interest only when diagnosing problems.

  • INFO - Confirmation that things are working as expected.

  • WARNING or WARN - An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

  • ERROR - Due to a more serious problem, the software has not been able to perform some function.

  • CRITICAL - A serious error, indicating that the program itself may be unable to continue running.

Parameters:
  • logger_name (Optional[str]) – Name for logger. Default is ‘arcpy-logger’.

  • log_level (Union[str, int, None]) – Logging level to use. Default is ‘INFO’.

  • logfile_pth (Union[Path, str, None]) – Where to save the logfile.log if file output is desired.

  • propagate (bool) – Whether to propagate message up to any parent loggers. Defaults to False to avoid repeated messages to ArcPy.

# assuming part of python module
logger = get_logger(__name__)

logger.debug('nauseatingly detailed debugging message')
logger.info('something actually useful to know')
logger.warning('The sky may be falling')
logger.error('The sky is falling.)
logger.critical('The sky appears to be falling because a giant meteor is colliding with the earth.')
Return type:

Logger

arcpy_logging.log_pandas_df(logger, pandas_df, title, line_tab_prefix='\\t\\t')[source]#

Helper function facilitating outputting a Pandas DataFrame into a logfile. This typically is used for including descriptive statistics in logfile outputs.

Parameters:
  • logger (Logger) – Logger being used.

  • pandas_df (DataFrame) – Pandas DataFrame to be converted to a string and included in the logfile.

  • title (str) – String title describing the data frame.

  • line_tab_prefix – Optional string comprised of tabs (\t\t) to prefix each line with providing indentation.

Return type:

None