3

I'm writing a Python script to retrieve data from Flickr. For logging purposes, I have the following setup function:

def init_log(logfile):
    format = '%(asctime)s - %(levelname)s - %(message)s'
    logging.basicConfig(filename=logfile,level=logging.DEBUG,format=format)

I've tested this using the python shell and it works as expected, creating a file if one doesn't already exist. But calling it from within my program is where it stops working. The function is definitely being called, and the logfile parameter is working properly – logging.basicConfig just isn't creating any file. I'm not even getting any errors or warnings.

My use of the Python Flickr API may be the culprit, but I doubt it. Any ideas?

1 Answer 1

6

The logging.basicConfig function only does anything if the root logger has no handlers configured. If called when there are already some handlers attached to the root, it's basically a no-op (as is documented).

Possibly the Python Flickr API does some logging, in which case you may find that basicConfig should be called earlier in your code.

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

1 Comment

Thanks! After opening dozens of stackoverflow questions, your answer was the one that helped me. I don't know what lib or commit caused this to stop working, I didn't see anything messing the logging from the latest commits. But just moving my logger setup to the start of my script (instead of inside a main function) solved my problems.

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.