> ## Documentation Index
> Fetch the complete documentation index at: https://developer.synopsys.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Logging

Scripting Toolkit logs internal operations using the Python logging module. Scripting Toolkit defines a hierarchy of
loggers which log the following information:

* `GDL.core`: Requests to Granta MI, including retries and elapsed time per request.
* `GDL.http`: HTTP status codes and full SOAP request and response content.
* `GDL.cache`: Streamlined layer caching information, including cache misses and cache clearing operations.

Python [`Logger`](https://docs.python.org/3/library/logging.html#logging.Logger) objects are hierarchical, with levels in the hierarchy being defined by the `.` character.
Messages are passed from lower level [`Logger`](https://docs.python.org/3/library/logging.html#logging.Logger) objects to higher level ones. As a result, it is possible to access
all log messages via the `GDL` logger.

By default, these messages are not output. To output the messages generated by this package and to add your own log
messages, you should:

1. Create a logger for your script.
2. Attach a handler to the logger.

The following sub-sections provide simple best practice for logging with this package. For more information about Python
logging in general, see the Python documentation:

* [Logging API documentation](https://docs.python.org/3/library/logging.html)
* [Logging HOWTO](https://docs.python.org/3/howto/logging.html).

## Creating a logger

The `GDL` logger can be accessed in two ways. The first is via the
[`get_foundation_logger()`](https://developer.synopsys.com/docs/granta-mi/granta-mi-scripting-toolkit/streamlined_api/helpers#ansys.grantami.core.mi_functions.get_foundation_logger) function:

```python theme={null}
logger = mpy.get_foundation_logger()
```

The second is by fetching the `GDL` logger by name using the Python [`getLogger()`](https://docs.python.org/3/library/logging.html#logging.getLogger) function:

```python theme={null}
import logging

logger = logging.getLogger("GDL")
```

The second option is more flexible since it allows access to more granular loggers. For example, to access only the
`GDL.core` logger:

```python theme={null}
core_logger = logging.getLogger("GDL.core")
```

The root of the Python logger hierarchy is the *root logger*, which contains all messages logged by all loggers in a
Python instance. To create an instance of the root logger, do not specify a logger name when using
[`getLogger()`](https://docs.python.org/3/library/logging.html#logging.getLogger):

```python theme={null}
root_logger = logging.getLogger()
```

[`Logger`](https://docs.python.org/3/library/logging.html#logging.Logger) objects only handle messages at or above a defined severity level. All new logger objects have a default
log level of [`WARNING`](https://docs.python.org/3/library/logging.html#logging.WARNING), so they ignore messages of [`INFO`](https://docs.python.org/3/library/logging.html#logging.INFO) or lower. To change a logger’s log level, use
[`Logger.setLevel()`](https://docs.python.org/3/library/logging.html#logging.Logger.setLevel):

```python theme={null}
root_logger.setLevel(logging.DEBUG)
```

You can then add log statements to the logger at a certain log level as follows:

```python theme={null}
logger.debug("This is a debug message")
logger.info("This is an info message")
```

#### NOTE

Until a log handler is attached, no log messages are emitted.

## Attaching a handler

There are two main types of handlers provided by the Python logging library: [`FileHandler`](https://docs.python.org/3/library/logging.handlers.html#logging.FileHandler) handlers and
[`StreamHandler`](https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler) handlers. A [`FileHandler`](https://docs.python.org/3/library/logging.handlers.html#logging.FileHandler) is used to write log messages to a file on disk, and a
[`StreamHandler`](https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler) is used to write log messages to `stderr` or `stdout`.

To add a [`StreamHandler`](https://docs.python.org/3/library/logging.handlers.html#logging.StreamHandler) handler to the root logger from the previous section, use the following code:

```python theme={null}
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root_logger.addHandler(ch)
```

It is possible to also log files to disk by using a [`FileHandler`](https://docs.python.org/3/library/logging.handlers.html#logging.FileHandler). The following example shows the creation of a
[`FileHandler`](https://docs.python.org/3/library/logging.handlers.html#logging.FileHandler) handler with a filename based on the current date:

```python theme={null}
import datetime

now = datetime.datetime.now()
filename = now.strftime("log-%Y%m%d.log")
fh = logging.FileHandler(filename=filename)
fh.setFormatter(formatter)

root_logger.addHandler(fh)
```

`%(name)s` includes the name of the logger to which the message was logged, which enables a single log file to be
used to capture information from all Scripting Toolkit loggers and loggers created for user code.

## Additional 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\Granta MI\ScriptingToolkit`
* Linux: `~/.appdata/Granta/Granta MI/ScriptingToolkit`

#### Versionchanged

Changed in version 4.2: Log location changed
