Logger#

class ansys.mechanical.core.logging.Logger(level=logging.DEBUG, to_file=False, to_stdout=True, filename=FILE_NAME)#

Provides for adding handlers to the logger for each Mechanical session.

This class allows you to add handlers to the logger to output to a file or the standard output.

Parameters:
levelint, optional

Logging level for filtering the messages that are allowed in the logger. The default is 10, in which case the DEBUG level is used.

to_filebool, optional

Whether to write log messages to a file. The default is False.

to_stdoutbool, optional

Whether to write log messages to the standard output. The default is True.

filenamestr, optional

Name of the file to write log messages to. The default is pymechanical.log.

Examples

Demonstrate logger usage from a Mechanical instance. The logger is automatically created when a Mechanical instance is created.

>>> from ansys.mechanical.core import launch_mechanical
>>> mechanical = launch_mechanical(loglevel='DEBUG')
>>> mechanical.log.info('This is a useful message')
INFO -  -   -  - This is LOG debug message.

Import the PyMechanical global logger and add a file output handler.

>>> import os
>>> from ansys.mechanical.core import LOG
>>> file_path = os.path.join(os.getcwd(), 'pymechanical.log')
>>> LOG.log_to_file(file_path)

Overview#

log_to_file

Add a file handler to the logger.

log_to_stdout

Add a standard output handler to the logger.

setLevel

Change the log level of the object and the attached handlers.

add_child_logger

Add a child logger to the main logger.

add_instance_logger

Add a logger for a Mechanical instance.

add_handling_uncaught_exceptions

Redirect the output of an exception to a logger.

__getitem__

Get the instance logger based on a key.

Import detail#

from ansys.mechanical.core.logging import Logger

Attribute detail#

Logger.file_handler = None#
Logger.std_out_handler = None#
Logger.logger#
Logger.propagate = True#
Logger.level#
Logger.debug#
Logger.info#
Logger.warning#
Logger.error#
Logger.critical#
Logger.log#

Method detail#

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

Add a file handler to the logger.

Parameters:
filenamestr, optional

Name of the file to write log messages to. The default is 'pymechanical.log'.

levelstr, optional

Level of logging. The default is 10, in which case the "DEBUG" level is used. Options are "DEBUG", "INFO", "WARNING" and "ERROR".

Examples

Write to the pymechanical.log file in the current working directory.

>>> from ansys.mechanical.core import LOG
>>> import os
>>> file_path = os.path.join(os.getcwd(), 'pymechanical.log')
>>> LOG.log_to_file(file_path)
Logger.log_to_stdout(level=LOG_LEVEL)#

Add a standard output handler to the logger.

Parameters:
levelstr, optional

Level of logging, such as DUBUG. The default is LOG_LEVEL.

Logger.setLevel(level='DEBUG')#

Change the log level of the object and the attached handlers.

Parameters:
levelstr, optional

Level of logging, such as DUBUG. The default is LOG_LEVEL.

Logger.add_child_logger(suffix, level=None)#

Add a child logger to the main logger.

This child logger is more general than an instance logger, which is designed to track the state of a Mechanical instance.

If the logging level is specified in the arguments, a new logger with a reference to the _global logger handlers is created instead of a child logger.

Parameters:
suffixstr

Name for the child logger.

levelstr, optional

Level of logging. The default is None, in which case the "DEBUG" level is used. Options are "DEBUG", "INFO", "WARNING", and "ERROR".

Returns:
logging.logger

Logger class.

Logger.add_instance_logger(name, mechanical_instance, level=None)#

Add a logger for a Mechanical instance.

The logger for a Mechanical instance has an adapter that adds contextual information, such as the name of the Mechanical instance. This logger is returned, and you can use it to log events as a normal logger. It is also stored in the _instances field.

Parameters:
namestr

Name for the new logger.

mechanical_instanceansys.mechanical.core.mechanical.Mechanical

Mechanical instance object. This object should contain the name attribute.

levelstr, optional

Level of logging. The default is None, in which case the "DEBUG" level is used. Options are "DEBUG", "INFO", "WARNING", and "ERROR".

Returns:
ansys.mechanical.core.logging.PyMechanicalCustomAdapter

Logger adapter customized to add Mechanical information to the logs. You can use this class to log events in the same way you would with the logger class.

Raises:
Exception

You can only input strings as name to this method.

Logger.__getitem__(key)#

Get the instance logger based on a key.

Parameters:
key
static Logger.add_handling_uncaught_exceptions(logger)#

Redirect the output of an exception to a logger.

Parameters:
loggerstr

Name of the logger.