# Logging and tests utilities


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Logging

#### Logging Levels

Python logging has five standard levels, in increasing order of
severity:

- DEBUG (10): Detailed information, typically useful for diagnosing
  problems
- INFO (20): Confirmation that things are working as expected
- WARNING (30): An indication something unexpected happened, but the
  program still works
- ERROR (40): Due to a more serious problem, the software couldn’t
  perform some function
- CRITICAL (50): A very serious error, indicating the program may be
  unable to continue

The function does return the root Logger, though typically you would not
use it directly.

------------------------------------------------------------------------

### set_logging

>  set_logging (level:int=20, format_file:str='%(asctime)s - %(name)s -
>                   %(levelname)s - %(message)s',
>                   format_console:str='%(levelname)s - %(message)s',
>                   datefmt:str='%Y-%m-%d %H:%M:%S', log_dir:str=None,
>                   filemode:str='a', backupCount:int=5, maxBytes:int=5242880)

*Set up the root Logger*

<table>
<colgroup>
<col style="width: 6%" />
<col style="width: 25%" />
<col style="width: 34%" />
<col style="width: 34%" />
</colgroup>
<thead>
<tr>
<th></th>
<th><strong>Type</strong></th>
<th><strong>Default</strong></th>
<th><strong>Details</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>level</td>
<td>int</td>
<td>20</td>
<td>The logging level</td>
</tr>
<tr>
<td>format_file</td>
<td>str</td>
<td>%(asctime)s - %(name)s - %(levelname)s - %(message)s</td>
<td>The logging format for the file</td>
</tr>
<tr>
<td>format_console</td>
<td>str</td>
<td>%(levelname)s - %(message)s</td>
<td>The logging format for the console</td>
</tr>
<tr>
<td>datefmt</td>
<td>str</td>
<td>%Y-%m-%d %H:%M:%S</td>
<td>The date format</td>
</tr>
<tr>
<td>log_dir</td>
<td>str</td>
<td>None</td>
<td>The logging directory, if None, logs to console</td>
</tr>
<tr>
<td>filemode</td>
<td>str</td>
<td>a</td>
<td>The logging file mode. ‘a’ for append, ‘w’ for overwrite</td>
</tr>
<tr>
<td>backupCount</td>
<td>int</td>
<td>5</td>
<td>The number of backup files to keep</td>
</tr>
<tr>
<td>maxBytes</td>
<td>int</td>
<td>5242880</td>
<td>The maximum size of the log file in bytes</td>
</tr>
<tr>
<td><strong>Returns</strong></td>
<td><strong>Logger</strong></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

#### Example usage

We must make sure the logging works when we are testing indivudual
notebooks and when running the complete code. To accomplish this we can
do the following.

1.  First in `run.py` or other entry point of the project, add the
    following to get the logging when the complete code is run:

> ``` python
> from hopsa import set_logging
>
> if __name__ == "__main__":
>    log_dir = "../logs"
>    set_logging(log_dir=log_dir, level=10)
> ```

or

> ``` python
> import logging
> from hopsa import set_logging
>
> if __name__ == "__main__":
>    log_dir = "../logs"
>    set_logging(log_dir=log_dir, level=logging.DEBUG)
> ```

2.  Then in each module/notebook, you create module-specific loggers:

At the top of each notebook (00_core.ipynb, 02_features.ipynb, etc.)

> ``` python
> #| export
> import logging
> ```

> ``` python
> #| eval: false
> from hopsa import lgtst
> ```

> ``` python
> #| eval: false
> lgtst.set_logging(log_dir="../logs", level=logging.DEBUG)
> ```

> ``` python
> #| export
> logger = logging.getLogger(__name__)
> ```

The `#| export` nbdev directive makes sure this cell will be used in the
python module. The `#| eval: false` nbdev directive makes sure this cell
will run when we run the notebook, but it won’t be used in the Python
module and it also won’t be tested when we run `nbdev_prepare`.

Then use the logger throughout the module

> ``` python
> logger.debug("Debug message")
> logger.info("Info message")
> ```

``` python
rt_logger = set_logging()
```

    INFO - Log file: /home/jelle/code/hopsa/logs/hopsa.log
    INFO - Log file: /home/jelle/code/hopsa/logs/hopsa.log
    INFO - Log file mode: a
    INFO - Log file mode: a
    INFO - Log backup count: 5
    INFO - Log backup count: 5
    INFO - Log max bytes: 5242880
    INFO - Log max bytes: 5242880

``` python
for h in rt_logger.handlers:
    h.close()
    rt_logger.removeHandler(h)
    print(f"Removed handler: {h}")
```

    Removed handler: <StreamHandler stderr (INFO)>
    Removed handler: <RotatingFileHandler /home/jelle/code/hopsa/logs/hopsa.log (INFO)>
