3

I'm trying to get a list of logger names that have already been created. I saw a few similar questions on here but none of the answers produced the output I wanted. I want the list of names that you can use to call, logging.getLogger(name)

Thus the list would be...

* name1
* name2
* name3

Two things I have tried via the other questions..

  1. loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]

    • Output - Logger name1 (DEBUG)>, <Logger name2 (DEBUG)
    • This is the closest one as it contains the name within it
  2. loggers = logging.getLogger().manager.loggerDict.keys()

    • Output - generator object RsyncProcessing.start_new_filelist_update_process.<locals>.<genexpr> at 0x7fd193d43650

The reason I need just the list of names is because my program will run continuously and I only need to set up a new log if the log does not already exist. Therefore, I would write...

if name in loggers: 
     break 
else: 
     make_new_log
2

1 Answer 1

4
if name in logging.root.manager.loggerDict:
    print("logger already exists")

The loggerDict is just an ordinary dictionary. Your output suggests something else though, so are you sure you copied that correctly. Or alternatively, which version and implementation of python are you using?

Also as has been pointed out in the comments: calling getLogger is idempotent. Multiple calls with the same name will return the same object.

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

1 Comment

Thanks, that worked! I should have clarified that I have a log file for each station that I pick up, so will probably have ~250 log files and 1 general one.. therefore I call getLogger multiple times because I switch back and forth while they are updating to log to the appropriate file. If a new station comes online, then it will need to create a new log file for that station.

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.