The logging.py
module#
Summary#
Keeps the reference to the name of the Mechanical instance dynamic. |
|
Controls the way PyMechanical formats the percent style. |
|
Provides for overwriting default format styles with custom format styles. |
|
Ensures that the instance name record always exists. |
|
Provides for adding handlers to the logger for each Mechanical session. |
Add a file handler to the input. |
|
Add a file handler to the stand output handler. |
Default log level configuration. |
|
Default file name. |
|
Constant for logging.DEBUG. |
|
Constant for logging.INFO. |
|
Constant for logging.WARN. |
|
Constant for logging.ERROR. |
|
Constant for logging.CRITICAL. |
|
Standard output message format. |
|
File message format. |
|
Default standard output header. |
|
Default file 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 adict
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:
- 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
None
. Options are"DEBUG"
,"INFO"
,"WARNING"
and"ERROR"
.- write_headersbool,
optional
Whether to write headers to the file. The default is
False
.
- logger
- 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:
- logger
logging.Logger
orlogging.Logger
Logger to add the file handler to.
- level
str
,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
.
- logger
- 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#