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:
- level
int
,optional
Logging level for filtering the messages that are allowed in the logger. The default is
10
, in which case theDEBUG
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
.- filename
str
,optional
Name of the file to write log messages to. The default is
pymechanical.log
.
- level
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#
Add a file handler to the logger. |
|
Add a standard output handler to the logger. |
|
Change the log level of the object and the attached handlers. |
|
Add a child logger to the main logger. |
|
Add a logger for a Mechanical instance. |
Redirect the output of an exception to a logger. |
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:
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:
- level
str
,optional
Level of logging, such as
DUBUG
. The default isLOG_LEVEL
.
- level
- Logger.setLevel(level='DEBUG')#
Change the log level of the object and the attached handlers.
- Parameters:
- level
str
,optional
Level of logging, such as
DUBUG
. The default isLOG_LEVEL
.
- 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.
- 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:
- name
str
Name for the new logger.
- mechanical_instance
ansys.mechanical.core.mechanical.Mechanical
Mechanical instance object. This object should contain the
name
attribute.- level
str
,optional
Level of logging. The default is
None
, in which case the"DEBUG"
level is used. Options are"DEBUG"
,"INFO"
,"WARNING"
, and"ERROR"
.
- name
- 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