Join the Kedro community

Updated 2 weeks ago

Configuring a custom logger in logging.yml

Hi everyone! Has anyone implemented a customer log handler and successfully configured it in logging.yml? I'm getting a "No module named 'myproject.handlers'" error. I guess the logging is instantiated at a point where the project hasn't been loaded yet. Any idea how to get a custom logger running?

D
P
6 comments

Hi Paul, could you share your logging.yml and handlers.py files?

version: 1

disable_existing_loggers: False

formatters:
simple:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

handlers:
console:
class: logging.StreamHandler
level: INFO
formatter: simple
stream: ext://sys.stdout

custom_handler:
class: myproject.handlers.MyCustomHandler
level: INFO

rich:
class: kedro.logging.RichHandler
rich_tracebacks: True
level: INFO

loggers:
kedro:
level: INFO

myproject:
level: INFO

root:
handlers: [rich, custom_handler]
level: INFO

class MyCustomHandler(logging.Handler):
def __init__(self):
super().__init__()

def emit(self, record):
log_line = self.format(record)
print(log_line)

Hi Dmitry, above is the content of logging.yml and handlers.py

I tried it, and it works well on my side after adding the src folder to PYTHONPATH to enable the import:

export PYTHONPATH="${PYTHONPATH}:$(pwd)/src"

then I will try that, thanks so much

Add a reply
Sign up and join the conversation on Slack