The logging.py module#

Summary#

PyMechanicalCustomAdapter

Keeps the reference to the name of the Mechanical instance dynamic.

PyMechanicalPercentStyle

Controls the way PyMechanical formats the percent style.

PyMechanicalFormatter

Provides for overwriting default format styles with custom format styles.

InstanceFilter

Ensures that the instance name record always exists.

Logger

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

addfile_handler

Add a file handler to the input.

add_stdout_handler

Add a file handler to the stand output handler.

LOG_LEVEL

Default log level configuration.

FILE_NAME

Default file name.

DEBUG

Constant for logging.DEBUG.

INFO

Constant for logging.INFO.

WARN

Constant for logging.WARN.

ERROR

Constant for logging.ERROR.

CRITICAL

Constant for logging.CRITICAL.

STDOUT_MSG_FORMAT

Standard output message format.

FILE_MSG_FORMAT

File message format.

DEFAULT_STDOUT_HEADER

Default standard output header.

DEFAULT_FILE_HEADER

Default file header.

NEW_SESSION_HEADER

Default new session header containing date and time.

Description#

Logging module.

This module supplies the general framework for logging in PyMechanical. This module is built upon the logging package. The intent is not for this module to replace the logging package but rather to provide a way for the logging package and PyMechancial to interact.

The loggers used in the module include the name of the instance, which is intended to be unique. This name is printed in all the active outputs and is used to track the different Mechanical instances.

Usage#

Global logger#

There is a global logger named pymechanical_global, which is created at ansys.mechanical.core.__init__. If you want to use this global logger, you must call it at the top of your module:

from ansys.mechanical.core import LOG

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

from ansys.mechanical.core import LOG as logger

The default logging level of LOG is ERROR. To change this and output lower-level messages, you can use this code:

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

Alternatively, you can use this code:

LOG.setLevel("DEBUG")

This alternative code ensures that all the handlers are set to the input log level.

By default, this logger does not log to a file. If you want, you can add a file handler:

import os

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

The preceding code sets the logger to also be redirected to this file. If you want to change the characteristics of this global logger from the beginning of the execution, you must edit the file __init__ in the ansys.mechanical.core directory.

To log using this logger, call the desired method as a normal logger:

>>> import logging
>>> from ansys.mechanical.core.logging import Logger
>>> LOG = Logger(level=logging.DEBUG, to_file=False, to_stdout=True)
>>> LOG.debug("This is LOG debug message.")

DEBUG -  -   -  - This is the LOG debug message.

Instance Logger#

Every time an instance of the Mechanical ckass is created, a logger is created and stored here:

  • LOG._instances. This field is a dict where the key is the name of the created logger.

These logger instances inherit the pymechanical_global output handlers and logging level unless otherwise specified. The way this logger works is very similar to the global logger. You can add a file handler if you want using the log_to_file() method or change the log level using the logger.Logging.setLevel() method.

You can use this logger like this:

>>> from ansys.mechanical.core import launch_mechanical
>>> mechanical = launch_mechanical()
>>> mechanical.log.info("This is a useful message")

INFO - GRPC_127.0.0.1:50056 -   -  -
This is a useful message

Other loggers#

You can create your own loggers using the Python logging package as you would do in any other script. There are no conflicts between these loggers.

Module detail#

logging.addfile_handler(logger, filename=FILE_NAME, level=LOG_LEVEL, write_headers=False)#

Add a file handler to the input.

Parameters:
loggerlogging.Logger or logging.Logger

Logger to add the file handler to.

filenamestr, optional

Name of the output file. The default is FILE_NAME.

levelstr, optional

Level of logging. The default is None. Options are "DEBUG", "INFO", "WARNING" and "ERROR".

write_headersbool, optional

Whether to write headers to the file. The default is False.

Returns:
logger

Logger object.

logging.add_stdout_handler(logger, level=LOG_LEVEL, write_headers=False)#

Add a file handler to the stand output handler.

Parameters:
loggerlogging.Logger or logging.Logger

Logger to add the file handler to.

levelstr, optional

Level of logging. The default is None. Options are "DEBUG", "INFO", "WARNING" and "ERROR".

write_headersbool, optional

Whether to write headers to the file. The default is False.

Returns:
logger

Logger object.

logging.LOG_LEVEL#

Default log level configuration.

logging.FILE_NAME = 'pymechanical.log'#

Default file name.

logging.DEBUG#

Constant for logging.DEBUG.

logging.INFO#

Constant for logging.INFO.

logging.WARN#

Constant for logging.WARN.

logging.ERROR#

Constant for logging.ERROR.

logging.CRITICAL#

Constant for logging.CRITICAL.

logging.STDOUT_MSG_FORMAT = '%(levelname)s - %(instance_name)s -  %(module)s - %(funcName)s - %(message)s'#

Standard output message format.

logging.FILE_MSG_FORMAT#

File message format.

logging.DEFAULT_STDOUT_HEADER = Multiline-String#
Show Value
"""
LEVEL - INSTANCE NAME - MODULE - FUNCTION - MESSAGE
"""

Default standard output header.

logging.DEFAULT_FILE_HEADER#

Default file header.

logging.NEW_SESSION_HEADER#

Default new session header containing date and time.

logging.string_to_loglevel#