Skip to main content

Granta MI Scripting Toolkit 4.1

Logging

Last update: 16.07.2025

Writing Scripting Toolkit logs to the console

The Foundation layer defines a logger with the name GDL that can be used to capture logs about the requests made to Granta MI.

To use this logger, first fetch the logger object either via the get_foundation_logger function, or by using the Python logging module directly.

import logging

from GRANTA_MIScriptingToolkit import granta as mpy

logger = mpy.get_foundation_logger()

# Alternatively, the logger can be fetched by name.
logger = logging.getLogger("GDL")

Next, configure and attach a StreamHandler to the logger. A StreamHandler writes log messages to stderr.

console_handler = logging.StreamHandler()
string_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
formatter = logging.Formatter(string_format)
console_handler.setFormatter(formatter)

logger.addHandler(console_handler)

Finally, set the logger level to DEBUG to ensure all messages are displayed. The log level defines the minimum log level to be handled by this logger.

logger.setLevel(logging.DEBUG)

To test that the logging has been set up successfully, perform some Scripting Toolkit operations. The log messages generated by the Foundation Layer are shown on the console or in the notebook, depending on how this script is executed.

mi = mpy.connect("http://my.server.name/mi_servicelayer", autologon=True)
my_db = mi.get_db(db_key="MI_Training")

Previous cell output:

2024-10-30 16:10:48,965 - GDL - DEBUG - Executing Browse_GetDatabases() command, attempt 1.
```*Previous cell output:*
```output
2024-10-30 16:10:49,001 - GDL - INFO - Browse_GetDatabases() command succeeded on attempt 1 after 0.0351679939776659s.

Writing Scripting Toolkit logs to a file

If you would like to write logs to disk, you can use a FileHandler. In this example, use a timestamp to define a unique file name for each execution of the script.

from datetime import datetime

file_name = f"scripting_toolkit_{datetime.now().strftime('%Y-%m-%d-%H%M%S')}.log"
file_handler = logging.FileHandler(filename=file_name)
formatter = logging.Formatter(string_format)
file_handler.setFormatter(formatter)

logger.addHandler(file_handler)

To test that the log files are being generated, perform some more Scripting Toolkit operations. The log messages will be available both on the console (or in the notebook) and in a file on disk.

tables = my_db.tables

Previous cell output:

2024-10-30 16:10:49,012 - GDL - DEBUG - Executing Browse_GetTables() command, attempt 1.
```*Previous cell output:*
```output
2024-10-30 16:10:49,273 - GDL - INFO - Browse_GetTables() command succeeded on attempt 1 after 0.2604787079617381s.

If required, you can also define additional loggers for your own log messages, which may have different handlers and may log at different levels.

For more details on managing logging, including additional handler types with more advanced file management capabilities, consult the Python logging documentation.

Additional Scripting Toolkit logging

The MI Scripting Toolkit underlying C libraries also perform their own additional logging separate to the Python logging. These logs can be found in the following locations:

  • Windows: %LOCALAPPDATA%\Granta Design\MIScriptingToolkit
  • Linux: ~/.appdata/Granta Design/MIScriptingToolkit

Connect with Ansys