Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysterious logging.basicConfig problem (Python)

Tags:

python

logging

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?

like image 547
Max Avatar asked Mar 08 '26 10:03

Max


1 Answers

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.

like image 149
Vinay Sajip Avatar answered Mar 10 '26 22:03

Vinay Sajip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!