The logger.py module#

Summary#

PyGeometryCustomAdapter

Keeps the reference to the Geometry service instance name dynamic.

PyGeometryPercentStyle

Provides a common messaging style for the PyGeometryFormatter class.

PyGeometryFormatter

Provides a Formatter class for overwriting default format styles.

InstanceFilter

Ensures that the instance_name record always exists.

Logger

Provides the logger used for each PyAnsys Geometry session.

addfile_handler

Add a file handler to the input.

add_stdout_handler

Add a standout handler to the logger.

Description#

Provides a general framework for logging in PyAnsys Geometry.

This module is built on the Logging facility for Python. It is not intended to replace the standard Python logging library but rather provide a way to interact between its logging class and PyAnsys Geometry.

The loggers used in this module include the name of the instance, which is intended to be unique. This name is printed in all active outputs and is used to track the different Geometry service instances.

Logger usage#

Global logger#

There is a global logger named PyAnsys_Geometry_global that is created when ansys.geometry.core.__init__ is called. If you want to use this global logger, you must call it at the top of your module:

from ansys.geometry.core import LOG

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

from ansys.geometry.core import LOG as logger

The default logging level of LOG is ERROR. You can change this level and output lower-level messages with this code:

LOG.logger.setLevel("DEBUG")
LOG.file_handler.setLevel("DEBUG")  # If present.
LOG.stdout_handler.setLevel("DEBUG")  # If present.

Alternatively, you can ensure that all the handlers are set to the input log level with this code:

LOG.setLevel("DEBUG")

This logger does not log to a file by default. If you want, you can add a file handler with this code:

import os

file_path = os.path.join(os.getcwd(), "pyansys-geometry.log")
LOG.log_to_file(file_path)

This also sets the logger to 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 __init__ file in the directory ansys.geometry.core.

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

>>> import logging
>>> from ansys.geometry.core.logging import Logger
>>> LOG = Logger(level=logging.DEBUG, to_file=False, to_stdout=True)
>>> LOG.debug("This is LOG debug message.")

DEBUG -  -  <ipython-input-24-80df150fe31f> - <module> - This is LOG debug message.

Instance logger#

Every time an instance of the Modeler class is created, a logger is created and stored in LOG._instances. This field is a dictionary where the key is the name of the created logger.

These instance loggers inherit the PyAnsys_Geometry_global output handlers and logging level unless otherwise specified. The way this logger works is very similar to the global logger. If you want to add a file handler, you can use the log_to_file() method. If you want to change the log level, you can use the setLevel() method.

Here is an example of how you can use this logger:

>>> from ansys.geometry.core import Modeler
>>> modeler = Modeler()
>>> modeler._log.info("This is a useful message")

INFO - GRPC_127.0.0.1:50056 -  <...> - <module> - This is a useful message

Other loggers#

You can create your own loggers using a Python logging library as you would do in any other script. There would be no conflicts between these loggers.

Module detail#

logger.addfile_handler(logger, filename=FILE_NAME, level=LOG_LEVEL, write_headers=False)#

Add a file handler to the input.

Parameters:
loggerlogging.Logger

Logger to add the file handler to.

filenamestr, default: β€œpyansys-geometry.log”

Name of the output file.

levelint, default: 10

Level of logging. The default is 10, in which case the logging.DEBUG level is used.

write_headersbool, default: False

Whether to write the headers to the file.

Returns:
Logger

Logger or logging.Logger object.

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

Add a standout handler to the logger.

Parameters:
loggerlogging.Logger

Logger to add the file handler to.

levelint, default: 10

Level of logging. The default is 10, in which case the logging.DEBUG level is used.

write_headersbool, default: False

Whether to write headers to the file.

Returns:
Logger

Logger or logging.Logger object.

logger.LOG_LEVEL#
logger.FILE_NAME = 'pyansys-geometry.log'#
logger.DEBUG#
logger.INFO#
logger.WARN#
logger.ERROR#
logger.CRITICAL#
logger.STDOUT_MSG_FORMAT = '%(levelname)s - %(instance_name)s -  %(module)s - %(funcName)s - %(message)s'#
logger.FILE_MSG_FORMAT#
logger.DEFAULT_STDOUT_HEADER = Multiline-String#
Show Value
"""
LEVEL - INSTANCE NAME - MODULE - FUNCTION - MESSAGE
"""
logger.DEFAULT_FILE_HEADER#
logger.NEW_SESSION_HEADER#
logger.LOG#
logger.string_to_loglevel#