Skip to content
Advertisement

Python logging: how to write logs into file and stdout

I have a main file where I use all the functions from the module. I want to log all the necessary things into file and into stdout.

It works when I use logger.info("Write it into file") inside main file, but it doesn’t work when I import here some functions from the other scripts.

I use it inside main file to make a logger

JavaScript

And in other file I use only

JavaScript

How can I get all the logs?

Advertisement

Answer

__name__ has different values in your two modules, so you really have two different loggers. You are only adding the file handler to one of them.

You want to configure a common ancestor. That could be the root logger:

JavaScript

or a specific logger that only your two loggers inherit from.

JavaScript

and

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement