The logger.py module#

Summary#

PyAdditivePercentStyle

Provides a common messaging style for the PyAdditiveFormatter class.

PyAdditiveFormatter

Provides a Formatter class for overwriting default format styles.

Logger

Provides the logger used for each PyAdditive session.

addfile_handler

Add a file handler to the input.

add_stdout_handler

Add a standout handler to the logger.

LOG_LEVEL

FILE_NAME

STDOUT_MSG_FORMAT

FILE_MSG_FORMAT

LOG

Description#

Provides a general framework for logging in PyAdditive.

Logger usage#

Global logger#

There is a global logger named PyAdditive_global that is created when ansys.additive.core.__init__ is called. If you want to use this global logger, you must call it at the top of your module:

from ansys.additive.core import LOG

You can rename this logger to avoid conflicts with other loggers (if any):

from ansys.additive.core import LOG as logger

The default logging level of LOG is WARNING. You can change this level and output lower-level messages with this code:

LOG.logger.setLevel("DEBUG")
LOG.file_handler.setLevel("DEBUG")  # If present.
LOG.stdout_handler.setLevel("DEBUG")  # If present.

Alternatively, you can ensure that all the handlers are set to the input log level with this code:

LOG.setLevel("DEBUG")

This logger does not log to a file by default. If you want, you can add a file handler with this code:

import os

file_path = os.path.join(os.getcwd(), "pyadditive.log")
LOG.log_to_file(file_path)

Module detail#

class logger.PyAdditivePercentStyle(fmt, *, defaults=None)#

Bases: logging.PercentStyle

Provides a common messaging style for the PyAdditiveFormatter class.

Import detail#

from ansys.additive.core.logger import PyAdditivePercentStyle
class logger.PyAdditiveFormatter(fmt=STDOUT_MSG_FORMAT, datefmt=None, style='%', validate=True, defaults=None)#

Bases: logging.Formatter

Provides a Formatter class for overwriting default format styles.

Import detail#

from ansys.additive.core.logger import PyAdditiveFormatter
class logger.Logger(level=logging.DEBUG, to_file=False, to_stdout=True, filename=FILE_NAME)#

Provides the logger used for each PyAdditive session.

This class allows you to add handlers to the logger to output messages to a file or to the standard output (stdout).

Parameters:
leveldefaultDEBUG

Logging level to filter the message severity allowed in the logger.

to_filebool, default: False

Whether to write log messages to a file.

to_stdoutbool, default: True

Whether to write log messages to the standard output.

filenamestr, default: obj:FILE_NAME

Name of the file to write log log messages to.

Overview#

log_to_file

Add a file handler to the logger.

log_to_stdout

Add the standard output handler to the logger.

add_handling_uncaught_expections

Redirect the output of an exception to a logger.

Import detail#

from ansys.additive.core.logger import Logger

Attribute detail#

Logger.file_handler#
Logger.std_out_handler#

Method detail#

Logger.log_to_file(filename=FILE_NAME, level=LOG_LEVEL)#

Add a file handler to the logger.

Parameters:
filenamestr, default: obj:FILE_NAME

Name of the file to write log messages to.

leveldefaultDEBUG

Logging level to filter the message severity allowed in the logger.

Logger.log_to_stdout(level=LOG_LEVEL)#

Add the standard output handler to the logger.

Parameters:
leveldefaultDEBUG

Logging level to filter the message severity allowed in the logger.

Logger.add_handling_uncaught_expections(logger)#

Redirect the output of an exception to a logger.

Parameters:
loggerstr

Name of the logger.

Logger.addfile_handler(logger, filename=FILE_NAME, level=LOG_LEVEL)#

Add a file handler to the input.

Parameters:
loggerlogging.Logger

Logger to add the file handler to.

filenamestr, default: obj:FILE_NAME

Name of the output file.

leveldefaultDEBUG

Logging level to filter the message severity allowed in the logger.

Returns:
Logger

Logger or logging.Logger object.

Logger.add_stdout_handler(logger, level=LOG_LEVEL)#

Add a standout handler to the logger.

Parameters:
loggerlogging.Logger

Logger to add the file handler to.

leveldefaultDEBUG

Logging level to filter the message severity allowed in the logger.

Returns:
Logger

Logger or logging.Logger object.

Logger.LOG_LEVEL#
Logger.FILE_NAME = 'pyadditive.log'#
Logger.STDOUT_MSG_FORMAT = '%(asctime)s - %(levelname)s - %(module)s - %(funcName)s - %(message)s'#
Logger.FILE_MSG_FORMAT#
Logger.LOG#