0

I have two scripts

  • first.py
  • second.py

in first.py, I am creating a logger as

log = logging.getLogger()

in second.py I am doing same thing as

log = logging.getLogger()

they are working fine individually. Problem is when I call first.py into second.py. Which lead to create two instance of logger. How to avoid this thing?

I want to use multiple such file to call in each other.

2
  • According to the docs: Multiple calls to getLogger() with the same name will always return a reference to the same Logger object. So how do you know that multiple loggers are being created? Commented Jun 19, 2017 at 8:58
  • in logging i can see one it for first.py and second one is for root Commented Jun 19, 2017 at 9:00

2 Answers 2

1

Jai, it is not a problem to have multiple loggers, but they need different names. Ideally, you set them up with log = logging.getLogger(__name__). name is an name unique to each module which also corresponds to your package structure. You can use your own names, but most of the time that is more trouble than benefit.

Sign up to request clarification or add additional context in comments.

Comments

0

You can assign a name to the logger so that python could tell it's the same logger. In both first.py and second.py:

log = logging.getLogger('myLogger')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.