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#
Adapter for keeping the reference to an MAPDL instance name dynamic. |
|
Provides a customized |
|
Ensures that the |
|
Provides the logger used for each PyDyna |
Functions#
|
Add a file handler to the input. |
|
Add a file handler to the logger. |
Module Contents#
- 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.DEFAULT_STDOUT_HEADER = Multiline-String[source]#
Show Value
""" LEVEL - INSTANCE NAME - MODULE - FUNCTION - MESSAGE """
- 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.
- 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.
- 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.
- 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:
- level
int
,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
.- filename
str
,optional
Name of the file to write log messages to if
to_file=True
. The default isFILE_NAME
.
- level
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)
- log_to_file(filename=FILE_NAME, level=LOG_LEVEL)[source]#
Add a file handler to logger.
- Parameters:
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:
- filename
str
,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.- level
str
,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
.
- filename
- setLevel(level='DEBUG')[source]#
Change the log level of the object and the attached handlers.
- Parameters:
- level
str
,optional
Level of logging. The default is
'DEBUG'
.
- level
- add_child_logger(suffix, 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.
- 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:
- name
str
Name for the new logger.
- mapdl_instance
ansys.mapdl.core.mapdl._MapdlCore
MAPDL instance object. This should contain the
name
attribute.
- name
- 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.
- 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:
- logger
logging.Logger
orlogging.Logger
Logger to add the file handler to.
- filename
str
,optional
Name of the output file. The default is
FILE_NAME
.- level
str
,optional
Level of logging. The default is
LOG_LEVEL
.- write_headersbool,
optional
Whether to write the headers to the file. The default is
False
.
- logger
- 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:
- logger
logging.Logger
orlogging.Logger
Logger to add the file handler to.
- level
str
,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
.
- logger
- Returns:
logger
Logger or Logger object.