Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,28 @@ Use [Python logging](https://docs.python.org/3/library/logging.html) instead of
```python
# Initialize logging
import os, logging

# Each log line includes the date and time, the log level, the current function and the message
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(funcName)-30s %(message)s')

# The log file is the same as the module name plus the suffix ".log"
# i.e.: calculate.py -> calculate.py.log
fh = logging.FileHandler("%s.log" % (os.path.basename(__file__)))
sh = logging.StreamHandler()
fh.setLevel(logging.DEBUG) # set the log level for the log file
fh.setFormatter(formatter)

# Additionally, we want to display the current log output on stdout.
# This can be archived using an additional stream handler.
sh = logging.StreamHandler()
sh.setFormatter(formatter)
sh.setLevel(logging.INFO) # set the log level for the console

logger = logging.getLogger(__name__)

# Add the Handler to the logger instance.
logger.addHandler(fh)
logger.addHandler(sh)

logger.setLevel(logging.DEBUG)
logger.propagate = False
```
Expand All @@ -71,6 +80,20 @@ Here are some example calls. Use `logger.exception()` in an outer `except` block
logger.exception(f"Error during calculation for batch_index={batch_index} uuid={uuid}")
```

### Use Child Logger for Each Imported Class or Module

When structuring a python application or a script in multiple modules (or classes), a `ChildLogger` for each
module should be used.

```python

import logging

class MyClass():
def __init__(self):
self.__logger = logging.getLogger(__name__).getChild('my_class')
```

## Editor/IDE and code check

* [PyCharm](https://www.jetbrains.com/pycharm/)
Expand Down