ansys.dyna.core.pre.dynalogging#

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.

Attributes#

Classes#

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.

Functions#

addfile_handler(logger[, filename, level, write_headers])

Add a file handler to the input.

add_stdout_handler(logger[, level, write_headers])

Add a file handler to the logger.

Module Contents#

ansys.dyna.core.pre.dynalogging.LOG_LEVEL[source]#
ansys.dyna.core.pre.dynalogging.FILE_NAME = 'pydyna.log'[source]#
ansys.dyna.core.pre.dynalogging.DEBUG[source]#
ansys.dyna.core.pre.dynalogging.INFO[source]#
ansys.dyna.core.pre.dynalogging.WARN[source]#
ansys.dyna.core.pre.dynalogging.ERROR[source]#
ansys.dyna.core.pre.dynalogging.CRITICAL[source]#
ansys.dyna.core.pre.dynalogging.STDOUT_MSG_FORMAT = '%(levelname)s - %(instance_name)s -  %(module)s - %(funcName)s - %(message)s'[source]#
ansys.dyna.core.pre.dynalogging.FILE_MSG_FORMAT[source]#
ansys.dyna.core.pre.dynalogging.DEFAULT_STDOUT_HEADER = Multiline-String[source]#
Show Value
"""
LEVEL - INSTANCE NAME - MODULE - FUNCTION - MESSAGE
"""
ansys.dyna.core.pre.dynalogging.DEFAULT_FILE_HEADER[source]#
ansys.dyna.core.pre.dynalogging.NEW_SESSION_HEADER[source]#
ansys.dyna.core.pre.dynalogging.string_to_loglevel[source]#
class ansys.dyna.core.pre.dynalogging.PymapdlCustomAdapter(logger, extra=None)[source]#

Bases: logging.LoggerAdapter

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

Using the standard approach, extra parameters must be supplied to the logger to indicate the MAPDL instance for which messages must be logged.

With this class, you only have to specify the MAPDL instance that you are referring to once.

level = None[source]#
file_handler = None[source]#
stdout_handler = None[source]#
logger[source]#
std_out_handler[source]#
process(msg, kwargs)[source]#

Process the logging message and keyword arguments passed in to a logging call to insert contextual information. You can either manipulate the message itself, the keyword args or both. Return the message and kwargs modified (or not) to suit your needs.

Normally, you’ll only need to override this one method in a LoggerAdapter subclass for your specific needs.

log_to_file(filename=FILE_NAME, level=LOG_LEVEL)[source]#

Add a file handler to the logger.

Parameters:
filenamestr, optional

Name of the file where logs are recorded. The default is FILE_NAME.

levelstr, optional

Level of logging. The default is LOG_LEVEL, which causes all messages to be recorded. For example, you can set the level of logging to DEBUG.

log_to_stdout(level=LOG_LEVEL)[source]#

Add a standard output handler to the logger.

Parameters:
levelstr, optional

Level of logging record. The default is LOG_LEVEL, which causes all messages to be recorded. For example, you can set the level of logging to "DEBUG".

setLevel(level='DEBUG')[source]#

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

Parameters:
levelstr, optional

Level of logging record. The default is "DEBUG.

class ansys.dyna.core.pre.dynalogging.PymapdlPercentStyle(fmt, *, defaults=None)[source]#

Bases: logging.PercentStyle

class ansys.dyna.core.pre.dynalogging.PymapdlFormatter(fmt=STDOUT_MSG_FORMAT, datefmt=None, style='%', validate=True, defaults=None)[source]#

Bases: logging.Formatter

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

class ansys.dyna.core.pre.dynalogging.InstanceFilter(name='')[source]#

Bases: logging.Filter

Ensures that the instance_name record always exists.

filter(record)[source]#

Determine if the specified record is to be logged.

Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.

class ansys.dyna.core.pre.dynalogging.Logger(level=logging.DEBUG, to_file=False, to_stdout=True, filename=FILE_NAME)[source]#

Provides the logger used for each PyDyna pre session.

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

Parameters:
levelint, optional

Logging level to filter the message severity allowed in the logger. The default is logging.DEBUG.

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 if to_file=True. The default is FILE_NAME.

Examples

Demonstrate logger usage from an MAPDL instance mapdl. This logger is automatically created when an MAPDL instance is created.

>>> from ansys.mapdl.core import launch_mapdl
>>> mapdl = launch_mapdl(loglevel='DEBUG')
>>> mapdl._log.info('This is a useful message')
INFO -  -   -  - This is LOG debug message.

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

>>> import os
>>> from ansys.mapdl.core import LOG
>>> file_path = os.path.join(os.getcwd(), 'pymapdl.log')
>>> LOG.log_to_file(file_path)
file_handler = None[source]#
std_out_handler = None[source]#
logger[source]#
propagate = True[source]#
level[source]#
debug[source]#
info[source]#
warning[source]#
error[source]#
critical[source]#
log[source]#
log_to_file(filename=FILE_NAME, level=LOG_LEVEL)[source]#

Add a file handler to logger.

Parameters:
filenamestr, optional

Name of the file where the logs are recorded. The default is FILE_NAME, in which case they are recorded in the 'pymapdl.log' file.

levelstr, optional

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

Examples

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

>>> from ansys.mapdl.core import LOG
>>> import os
>>> file_path = os.path.join(os.getcwd(), 'pymapdl.log')
>>> LOG.log_to_file(file_path)
log_to_stdout(level=LOG_LEVEL)[source]#

Add a standard output handler to the logger.

Parameters:
filenamestr, optional

Name of the file where the logs are recorded. The default is FILE_NAME, in which case they are recorded in the 'pymapdl.log' file.

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 True.

setLevel(level='DEBUG')[source]#

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

Parameters:
levelstr, optional

Level of logging. The default is 'DEBUG'.

add_child_logger(sufix, level=None)[source]#

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 MAPDL instances.

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

Parameters:
sufixstr

Name of the logger.

levelstr, optional

Level of logging. The default is None.

Returns:
logging.logger

Logger class.

add_instance_logger(name, mapdl_instance, level=None)[source]#

Create a logger for an MAPDL instance.

The MAPDL instance logger is a logger with an adapter that adds contextual information, such as the MAPDL instance name. 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.

mapdl_instanceansys.mapdl.core.mapdl._MapdlCore

MAPDL instance object. This should contain the name attribute.

Returns:
ansys.mapdl.core.logging.PymapdlCustomAdapter

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

Raises:
Exception

You can only input strings as name to this method.

__getitem__(key)[source]#
add_handling_uncaught_expections(logger)[source]#

Redirect the output of an exception to the logger.

ansys.dyna.core.pre.dynalogging.addfile_handler(logger, filename=FILE_NAME, level=LOG_LEVEL, write_headers=False)[source]#

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.

ansys.dyna.core.pre.dynalogging.add_stdout_handler(logger, level=LOG_LEVEL, write_headers=False)[source]#

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.

ansys.dyna.core.pre.dynalogging.LOG[source]#