2010-11-10 15 views
20

कहीं मेरे कोड की आंत में मैं की तरह कुछ है:पदानुक्रम बनाम रूट लॉगर लॉगिंग?

logger = logging.getLogger('debug0.x') 

तरह से मैं इसे समझ, इस करना चाहिए केवल जवाब जब मैं पहले की तरह कुछ किया है:

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG, name='debug0') 

ध्यान दें कि नाम को डीबग के रूप में परिभाषित किया गया है। हालांकि, मैं ने पाया है कि अगर नाम कीवर्ड बिना

logging.basicConfig(filename='10Nov2010a.txt',level=logging.DEBUG) 

करते हैं, तो debug0.x लकड़हारा ऊपर परिभाषित प्रतिक्रिया करता है, और लॉग फ़ाइल के लिए लिखता है। मैं सोच रहा था कि यह केवल पहले मामले में प्रतिक्रिया करेगा, जब लॉगर का नाम दिया गया था।

मैं उलझन में हूं।

+3

['logging.basicConfig()'] (http://docs.python.org/library/logging.html#logging.basicConfig) में कोई 'नाम' कीवर्ड तर्क नहीं है। –

उत्तर

52

अजगर logging मॉड्यूल एक पदानुक्रम में लकड़हारा आयोजन करता है। सभी लॉगजर रूट लॉगर के वंशज हैं। प्रत्येक लॉगर लॉग संदेशों को अपने माता-पिता को पास करता है।

getLogger() फ़ंक्शन के साथ नए लॉगर्स बनाए गए हैं। फ़ंक्शन कॉल logging.getLogger('debug0.x') एक लॉगर x बनाता है जो debug0 का बच्चा है जो स्वयं रूट लॉगर का बच्चा है। इस लॉगर पर लॉग इन करते समय, यह संदेश को अपने माता-पिता को भेजेगा, और उसके माता-पिता रूट लॉगर को संदेश पास करेंगे। आपने basicConfig() फ़ंक्शन द्वारा फ़ाइल में लॉग इन करने के लिए रूट लॉगर कॉन्फ़िगर किया है, इसलिए आपका संदेश वहां समाप्त हो जाएगा।

+0

तो पाइथन में लॉगर प्राप्त करने के लिए सबसे अच्छा अभ्यास क्या है? जावा में आप केवल 'getLogger (getClass()। GetName())' (या कुछ सुविधा विधि 'getLogger (getClass()) ', जो मैंने निर्दिष्ट अन्य विधि को प्रस्तुत किया है)। पाइथन में हमें क्या उपयोग करना चाहिए? निश्चित रूप से हमें कुछ लॉगर आईडी बनाने और मैन्युअल रूप से इसे बनाने वाले प्रत्येक मॉड्यूल के लिए प्लग इन करने की आवश्यकता नहीं है। –

+2

@Garret पायथन में सबसे आम दृष्टिकोण प्रत्येक मॉड्यूल के शीर्ष पर'logger = logging.getLogger (__ name __) 'का उपयोग करना है। परिवर्तनीय '__name__' में वर्तमान मॉड्यूल का बिंदीदार नाम शामिल है। –

+1

कोई कारण नहीं है कि सर्वोत्तम अभ्यास '_logger' का उपयोग नहीं करना है ताकि मॉड्यूल आयात होने पर चर का खुलासा न हो? –

10

आप कोड या डॉक देखें हैं:

>>> print logging.basicConfig.__doc__ 

    Do basic configuration for the logging system. 

    This function does nothing if the root logger already has handlers 
    configured. ............... 
    A number of optional keyword arguments may be specified, which can alter 
    the default behaviour. 

    filename Specifies that a FileHandler be created, using the specified 
       filename, rather than a StreamHandler. 
    filemode Specifies the mode to open the file, if filename is specified 
       (if filemode is unspecified, it defaults to 'a'). 
    format Use the specified format string for the handler. 
    datefmt Use the specified date/time format. 
    level  Set the root logger level to the specified level. 
    stream Use the specified stream to initialize the StreamHandler. Note 
       that this argument is incompatible with 'filename' - if both 
       are present, 'stream' is ignored. 

logging.basicConfig बिल्कुल नाम तर्क का उपयोग नहीं करता है। यह रूट लॉगर शुरू करता है। getLogger एक "नाम" तर्क लेता है

>>> print logging.getLogger.__doc__ 

    Return a logger with the specified name, creating it if necessary. 

    If no name is specified, return the root logger. 
संबंधित मुद्दे