लॉग स्तर सेट करने के निम्नलिखित 2 तरीकों में क्या अंतर है और क्या दोनों के लिए कोई निश्चित उपयोग मामला है?

logger.setLevel(logging.DEBUG)

logging.basicConfig(level=logging.DEBUG)

मैं यह प्रश्न इसलिए पूछता हूं क्योंकि मैंने दूसरे के बजाय एक का उपयोग किया है और यह इस अजीब दोहराने वाले लॉगिंग आउटपुट की ओर ले जाता है:

(envCSSS) csss@csss-website:~/csss-site-in-dev/csss-site/src$ python3.7 manage.py getmail
2020-01-14 18:24:56 = INFO - [settings.py] BASE_DIR set to /home/csss
2020-01-14 18:24:56 = INFO - [settings.py] DEBUG set to True
2020-01-14 18:24:56 = INFO - [settings.py] ROOT_DIR set to /home/csss/csss-site-in-dev
2020-01-14 18:24:56 = INFO - [settings.py] STATIC_URL set to /STATIC_URL/
2020-01-14 18:24:56 = INFO - [settings.py] STATIC_ROOT set to /home/csss/static_root/
2020-01-14 18:24:56 = INFO - [settings.py] MEDIA_URL set to /MEDIA_URL/
2020-01-14 18:24:56 = INFO - [settings.py] MEDIA_ROOT set to /home/csss/media_root/
2020-01-14 18:24:56 = INFO - [settings.py] FILE_FORM_MASTER_DIR set to form_uploads/form_uploads/
2020-01-14 18:24:56 = INFO - [settings.py] FILE_FORM_UPLOAD_DIR set to form_uploads/form_uploads/temporary_files/
2020-01-14 18:24:56 = INFO - [settings.py] DJANGO_MAILBOX_ATTACHMENT_UPLOAD_TO set to mailbox_attachments/%Y/%m/%d/
2020-01-14 10:24:57 = WARNING - INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:INFO:django_mailbox.management.commands.getmail:Gathering messages for csss gmail
.....................
2020-01-14 10:24:57 = WARNING - Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):
2020-01-14 10:24:57 = WARNING - WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:WARNING:csss_site:Traceback (most recent call last):

setLevel उपरोक्त समस्या का कारण है और लॉगिंग समस्या को ठीक करने के लिए मुझे इसे basicConfig के लिए स्वैप करना पड़ा।

1
modernNeo 11 मार्च 2020, 07:27
आपको वह कोड दिखाना चाहिए जहां आप लॉगर को कॉन्फ़िगर कर रहे हैं और जहां आप इसका उपयोग कर रहे हैं।
 – 
dspencer
11 मार्च 2020, 07:29

1 उत्तर

सबसे बढ़िया उत्तर

दस्तावेज़ीकरण स्वयं व्याख्यात्मक है:

logger.setLevel

यहाँ, logger एक Logger का उदाहरण लगता है। setLevel का उपयोग करके आप इस विशेष लकड़हारे के लिए सीमा स्तर निर्धारित कर रहे हैं।

logging.basicConfig(level=logging.DEBUG)

डिफ़ॉल्ट Formatter के साथ एक StreamHandler बनाकर और इसे रूट लॉगर में जोड़कर लॉगिंग सिस्टम के लिए बुनियादी कॉन्फ़िगरेशन करता है। फ़ंक्शन debug(), info(), warning(), error() और critical() स्वचालित रूप से basicConfig() को कॉल करेंगे यदि रूट लॉगर के लिए कोई हैंडलर परिभाषित नहीं किया गया है।

यदि रूट लकड़हारा पहले से परिभाषित नहीं है, तो basicConfig को कॉल करना हैंडलर और फॉर्मेटर के साथ एक लकड़हारा सेटअप करेगा। यदि आप यहां level पास करते हैं तो यह रूट लॉगर के स्तर पर सेट हो जाएगा।

जैसा कि आप देखेंगे कि setLevel एक logger इंस्टेंस पर उपलब्ध है जबकि basicConfig logging मॉड्यूल पर उपलब्ध है।

1
AKS 11 मार्च 2020, 07:46