The dynalogging.py module#

Summary#

PymapdlCustomAdapter

Adapter for keeping the reference to an MAPDL instance name dynamic.

PymapdlPercentStyle

PymapdlFormatter

Provides a customized Formatter class for overwriting the default format styles.

InstanceFilter

Ensures that the instance_name record always exists.

Logger

Provides the logger used for each PyDyna pre session.

addfile_handler

Add a file handler to the input.

add_stdout_handler

Add a file handler to the logger.

Description#

Logging module supplying a general framework for logging in the PyDYNA pre service.

This module is built upon the Python logging module. It is not intended to replace this Python module but rather to provide a way for it and the PyDyna pre service to interact.

The loggers used in this PyDyna logging mdoule 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 instances of the PyDyNA pre service.

Global logger#

The global logger, named pydyna_global, is created at ansys.dyna.core.__init__. If you want to use the global logger, you must call it at the top of your module:

from ansys.dyna.core.pre import LOG

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

from ansys.dyna.core.pre import LOG as logger

The default logging level of LOG is ERROR. To change this to 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 set the logging level of LOG to DEBUG with one line of code:

.. code:: python

LOG.setLevel(“DEBUG”)

Using the preceding line 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 to log to a file, you can add a file handler:

import os

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

The preceding code sets the logger to also redirect to a file. If you want to change the characteristics of the global logger from the beginning of the execution, you must edit the __init__ file in the ansys.dyna.core.pre directory.

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

Instance loggers#

Every time an instance of the Mapdl class is created, a logger is created and stored in two places:

  • _MapdlCore._log: For backward compatibility.

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

Instance loggers inheritate the pydyna_global output handlers and logging level unless otherwise specified. Instance loggers work in a similar way to the global logger. You can use the log_to_file() method to add a file handler or the logger.Logging.setLevel() method to change the log level.

Other loggers#

You can create your own loggers using the Python logging module as you would do in any other script. No conflicts between these loggers exist.

Module detail#

dynalogging.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 LOG_LEVEL.

write_headersbool, optional

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

Returns:
logger

Logger or Logger object.

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

Add a file handler to the logger.

Parameters:
loggerlogging.Logger or logging.Logger

Logger to add the file handler to.

levelstr, optional

Level of logging. The default is LOG_LEVEL, in which case ""DEBUG" is used.

write_headersbool, optional

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

Returns:
logger

Logger or Logger object.

dynalogging.LOG_LEVEL = 10#
dynalogging.FILE_NAME = 'pydyna.log'#
dynalogging.DEBUG = 10#
dynalogging.INFO = 20#
dynalogging.WARN = 30#
dynalogging.ERROR = 40#
dynalogging.CRITICAL = 50#
dynalogging.STDOUT_MSG_FORMAT = '%(levelname)s - %(instance_name)s -  %(module)s - %(funcName)s - %(message)s'#
dynalogging.FILE_MSG_FORMAT = '%(levelname)s - %(instance_name)s -  %(module)s - %(funcName)s - %(message)s'#
dynalogging.DEFAULT_STDOUT_HEADER = Multiline-String#
Show Value
"""
LEVEL - INSTANCE NAME - MODULE - FUNCTION - MESSAGE
"""
dynalogging.DEFAULT_FILE_HEADER = Multiline-String#
Show Value
"""
LEVEL - INSTANCE NAME - MODULE - FUNCTION - MESSAGE
"""
dynalogging.NEW_SESSION_HEADER = Multiline-String#
Show Value
"""
===============================================================================
       NEW SESSION - Uninferable
==============================================================================="""
dynalogging.LOG#
dynalogging.string_to_loglevel#