2011-09-07 16 views
11

मैं पाइथन लॉगिंग के लिए नया हूं और मैं आसानी से देख सकता हूं कि यह घर-ब्रू समाधान के लिए कैसे पसंद करता है।प्रति विधि/फ़ंक्शन लॉगिंग अक्षम करें?

एक प्रश्न मुझे कोई जवाब नहीं मिल रहा है: मैं प्रति-विधि/फ़ंक्शन आधार पर लॉगिंग संदेशों को कैसे घुमाता हूं?

मेरा hypothetical मॉड्यूल एक एकल समारोह है। मैं विकसित रूप में, लॉग कॉल एक बहुत मदद कर रहे हैं:

logging.basicConfig(level=logging.DEBUG, 
       format=('%(levelname)s: %(funcName)s(): %(message)s')) 
log = logging.getLogger() 

my_func1(): 
    stuff... 
    log.debug("Here's an interesting value: %r" % some_value) 
    log.info("Going great here!") 
    more stuff... 

मैं 'my_func1' पर मेरे काम लपेट और, 'my_func2', से प्रवेश संदेशों एक दूसरे समारोह पर काम शुरू 'my_func1' जा रहा शुरू "सहायक" से "अव्यवस्था" तक।

क्या एक लाइन-लाइन जादू कथन है, जैसे कि 'logging.disabled_in_this_func()' जिसे मैं 'my_func1' के शीर्ष पर जोड़ सकता हूं ताकि 'my_func1' के भीतर सभी लॉगिंग कॉल अक्षम हो सकें, लेकिन फिर भी लॉगिंग कॉल छोड़ दें अन्य कार्यों/विधियों अपरिवर्तित?

धन्यवाद

लिनक्स, पायथन 2.7.1

+0

संबंधित: http://stackoverflow.com/questions/879732/logging-with-filters –

उत्तर

8

चाल कई लॉगर्स बनाने के लिए है।

इसके लिए कई पहलू हैं।

पहले। मॉड्यूल की शुरुआत में logging.basicConfig() का उपयोग न करें। मुख्य आयात स्विच

if __name__ == "__main__": 
    logging.basicConfig(...) 
    main() 
    logging.shutdown() 

दूसरा अंदर इसका इस्तेमाल केवल। वैश्विक वरीयताओं को सेट करने के अलावा, "रूट" लॉगर कभी न लें।

तीसरा। व्यक्तिगत नाम उन चीज़ों के लिए लॉगर्स प्राप्त करें जिन्हें सक्षम या अक्षम किया जा सकता है।

log = logging.getLogger(__name__) 

func1_log = logging.getLogger("{0}.{1}".format(__name__, "my_func1") 

अब आप प्रत्येक नामित लॉगर पर लॉगिंग स्तर सेट कर सकते हैं।

log.setLevel(logging.INFO) 
func1_log.setLevel(logging.ERROR) 
+0

मुख्य आयात स्विच के अंदर लॉगिंग कॉन्फ़िगरेशन डालने की सिफारिश के लिए धन्यवाद। जानकार अच्छा लगा। क्या रूट लॉगर का उपयोग एक एकल-मॉड्यूल स्क्रिप्ट के लिए भी अनुशंसित नहीं है जैसा कि मैं अभी काम कर रहा हूं? क्या आप ऐसा करने के लिए विपक्ष को स्पष्ट कर सकते हैं? –

+2

"रूट लॉगर का उपयोग करने की अनुशंसा नहीं की जाती"। अवधि। इसका इस्तेमाल न करें। यह "अज्ञात" है। आप केवल लॉगर नामित करना चाहते हैं ताकि आप कॉन्फ़िगर और फ़िल्टर कर सकें। –

+0

@ एसएलॉट के स्पष्ट संदेश पर विस्तृत करने के लिए: यदि आप रूट लॉगर में हैंडलर जोड़ते हैं, तो आप ** ** ** आपके द्वारा उपयोग किए जाने वाले अन्य सभी पैकेजों से संदेश प्राप्त करना शुरू कर देंगे, जो बहुत गन्दा हो सकते हैं (उदा। 'अनुरोध')। और इसके विपरीत, जब कोई अन्य पैकेज आपके पैकेज का उपयोग करता है, तो आपके पैकेज के संदेशों को अलग से संभाला नहीं जा सकता है। खराब डिजाइन – j08lue

2

आप एक डेकोरेटर इस्तेमाल कर सकते हैं:

import logging 
import functools 

def disable_logging(func): 
    @functools.wraps(func) 
    def wrapper(*args,**kwargs): 
     logging.disable(logging.DEBUG) 
     result = func(*args,**kwargs) 
     logging.disable(logging.NOTSET) 
     return result 
    return wrapper 

@disable_logging 
def my_func1(...): 
+0

हां! तुमने मुझे सेकंड से हराया। 'रैपर' के अंदर सावधान रहें क्योंकि यह 'func()' कॉल के लॉगिंग को फिर से सक्षम नहीं करेगा अपवाद फेंकता है। यही कारण है कि मैं 'try/end' वाक्यांश का उपयोग करता हूं ताकि यह सुनिश्चित किया जा सके कि लॉगिंग फिर से सक्षम है या नहीं, भले ही 'func()' सफल हो। –

6

आप एक डेकोरेटर कि अस्थायी रूप से प्रवेश करने को निलंबित हैं बना सकते हैं, आला:

from functools import wraps 

def suspendlogging(func): 
    @wraps(func) 
    def inner(*args, **kwargs): 
     previousloglevel = log.getEffectiveLevel() 
     try: 
      return func(*args, **kwargs) 
     finally: 
      log.setLevel(previousloglevel) 
    return inner 

@suspendlogging 
def my_func1(): ... 

चेतावनी: जो my_func से बुलाए गए किसी भी फ़ंक्शन के लिए लॉगिंग को भी निलंबित कर देगा 1 इसलिए सावधान रहें कि आप इसका उपयोग कैसे करते हैं।

+0

बहुत सारे अनुप्रयोगों के लिए अच्छा दृष्टिकोण। धन्यवाद! –

+0

मुझे लगता है कि यह सजावटी कारखाना निलंबित करने के लिए लॉगर लेना चाहिए था। –

+0

अगर मैं गलत हूं तो कृपया मुझे सही करें, लेकिन मुझे लगता है कि यह केवल 'लॉग' नामक एक लॉगर को अक्षम करेगा। यदि एक ही समय में कई लॉगर्स का उपयोग किया जा रहा है, उदाहरण के लिए, Django एप्लिकेशन में, यह सभी लॉगर्स को कैसे अक्षम कर सकता है? –

0

मुझे एसएलॉट द्वारा सुझाए गए उप-लॉगर्स को कार्यान्वित करने के तरीके के बारे में जानने के लिए मुझे कुछ समय लगा।

यह देखते हुए कि जब मैं शुरू कर रहा था, तो लॉगिंग कैसे सेट अप करना है, यह समझना कितना मुश्किल था, मुझे लगता है कि तब से मैंने जो कुछ सीखा है उसे साझा करने में काफी समय लगता है।

कृपया ध्यान रखें कि लॉगर्स/सब-लॉगर्स सेट अप करने का यह एकमात्र तरीका नहीं है, न ही यह सबसे अच्छा है। यह वही तरीका है जिसका उपयोग मैं अपनी जरूरतों को पूरा करने के लिए नौकरी पाने के लिए करता हूं। मुझे आशा है कि यह किसी के लिए सहायक होगा। टिप्पणी/साझा/आलोचना करने के लिए स्वतंत्र महसूस करें।


मान लीजिए कि हमारे पास एक साधारण लाइब्रेरी है जिसे हम उपयोग करना चाहते हैं। मुख्य कार्यक्रम से, हम लाइब्रेरी से प्राप्त लॉगिंग संदेशों को नियंत्रित करने में सक्षम होना चाहते हैं। बेशक हम विचारशील लाइब्रेरी निर्माता हैं, इसलिए हम इसे आसान बनाने के लिए हमारी लाइब्रेरी को कॉन्फ़िगर करते हैं।

सबसे पहले, मुख्य कार्यक्रम:

# some_prog.py 

import os 
import sys 

# Be sure to give Vinay Sajip thanks for his creation of the logging module 
# and tireless efforts to answer our dumb questions about it. Thanks Vinay! 
import logging 

# This module will make understanding how Python logging works so much easier. 
# Also great for debugging why your logging setup isn't working. 
# Be sure to give it's creator Brandon Rhodes some love. Thanks Brandon! 
import logging_tree 

# Example library 
import some_lib 

# Directory, name of current module 
current_path, modulename = os.path.split(os.path.abspath(__file__)) 
modulename = modulename.split('.')[0] # Drop the '.py' 


# Set up a module-local logger 
# In this case, the logger will be named 'some_prog' 
log = logging.getLogger(modulename) 

# Add a Handler. The Handler tells the logger *where* to send the logging 
# messages. We'll set up a simple handler that send the log messages 
# to standard output (stdout) 
stdout_handler = logging.StreamHandler(stream=sys.stdout) 
log.addHandler(stdout_handler) 


def some_local_func(): 
    log.info("Info: some_local_func()") 
    log.debug("Debug: some_local_func()") 


if __name__ == "__main__": 

    # Our main program, here's where we tie together/enable the logging infra 
    # we've added everywhere else. 

    # Use logging_tree.printout() to see what the default log levels 
    # are on our loggers. Make logging_tree.printout() calls at any place in 
    # the code to see how the loggers are configured at any time. 
    # 
    # logging_tree.printout() 

    print("# Logging level set to default (i.e. 'WARNING').") 
    some_local_func() 
    some_lib.some_lib_func() 
    some_lib.some_special_func() 

    # We know a reference to our local logger, so we can set/change its logging 
    # level directly. Let's set it to INFO: 
    log.setLevel(logging.INFO) 
    print("# Local logging set to 'INFO'.") 
    some_local_func() 
    some_lib.some_lib_func() 
    some_lib.some_special_func() 


    # Next, set the local logging level to DEBUG: 
    log.setLevel(logging.DEBUG) 
    print("# Local logging set to 'DEBUG'.") 
    some_local_func() 
    some_lib.some_lib_func() 
    some_lib.some_special_func() 


    # Set the library's logging level to DEBUG. We don't necessarily 
    # have a reference to the library's logger, but we can use 
    # logging_tree.printout() to see the name and then call logging.getLogger() 
    # to create a local reference. Alternately, we could dig through the 
    # library code. 
    lib_logger_ref = logging.getLogger("some_lib") 
    lib_logger_ref.setLevel(logging.DEBUG) 

    # The library logger's default handler, NullHandler() won't output anything. 
    # We'll need to add a handler so we can see the output -- in this case we'll 
    # also send it to stdout. 
    lib_log_handler = logging.StreamHandler(stream=sys.stdout) 
    lib_logger_ref.addHandler(lib_log_handler) 
    lib_logger_ref.setLevel(logging.DEBUG) 

    print("# Logging level set to DEBUG in both local program and library.") 
    some_local_func() 
    some_lib.some_lib_func() 
    some_lib.some_special_func() 


    print("# ACK! Setting the library's logging level to DEBUG output") 
    print("# all debug messages from the library. (Use logging_tree.printout()") 
    print("# To see why.)") 
    print("# Let's change the library's logging level to INFO and") 
    print("# only some_special_func()'s level to DEBUG so we only see") 
    print("# debug message from some_special_func()") 

    # Raise the logging level of the libary and lower the logging level 
    # of 'some_special_func()' so we see only some_special_func()'s 
    # debugging-level messages. 
    # Since it is a sub-logger of the library's main logger, we don't need 
    # to create another handler, it will use the handler that belongs 
    # to the library's main logger. 
    lib_logger_ref.setLevel(logging.INFO) 
    special_func_sub_logger_ref = logging.getLogger('some_lib.some_special_func') 
    special_func_sub_logger_ref.setLevel(logging.DEBUG) 

    print("# Logging level set to DEBUG in local program, INFO in library and") 
    print("# DEBUG in some_lib.some_special_func()") 
    some_local_func() 
    some_lib.some_lib_func() 
    some_lib.some_special_func() 

इसके बाद, हमारे पुस्तकालय:

# some_lib.py 

import os 
import logging 

# Directory, name of current module 
current_path, modulename = os.path.split(os.path.abspath(__file__)) 
modulename = modulename.split('.')[0] # Drop the '.py' 

# Set up a module-local logger. In this case the logger will be 
# named 'some_lib' 
log = logging.getLogger(modulename) 

# In libraries, always default to NullHandler so you don't get 
# "No handler for X" messages. 
# Let your library callers set up handlers and set logging levels 
# in their main program so the main program can decide what level 
# of messages they want to see from your library. 
log.addHandler(logging.NullHandler()) 

def some_lib_func(): 
    log.info("Info: some_lib.some_lib_func()") 
    log.debug("Debug: some_lib.some_lib_func()") 

def some_special_func(): 
    """ 
    This func is special (not really). It just has a function/method-local 
    logger in addition to the library/module-level logger. 
    This allows us to create/control logging messages down to the 
    function/method level. 

    """ 
    # Our function/method-local logger 
    func_log = logging.getLogger('%s.some_special_func' % modulename) 

    # Using the module-level logger 
    log.info("Info: some_special_func()") 

    # Using the function/method-level logger, which can be controlled separately 
    # from both the library-level logger and the main program's logger. 
    func_log.debug("Debug: some_special_func(): This message can be controlled at the function/method level.") 

अब कमेंटरी ट्रैक के साथ कार्यक्रम चलाने के लिए, करते हैं:

# Logging level set to default (i.e. 'WARNING'). 

सूचना नहीं है डिफ़ॉल्ट स्तर पर कोई आउटपुट नहीं है क्योंकि हमने कोई चेतावनी-स्तर संदेश नहीं बनाया है।

# Local logging set to 'INFO'. 
Info: some_local_func() 

लाइब्रेरी की संचालकों NullHandler() के लिए डिफ़ॉल्ट है, इसलिए हम मुख्य कार्यक्रम से केवल उत्पादन को देखते हैं। यह अच्छा है।

# Local logging set to 'DEBUG'. 
Info: some_local_func() 
Debug: some_local_func() 

मुख्य प्रोग्राम लॉगर DEBUG पर सेट है। हम अभी भी पुस्तकालय से कोई आउटपुट नहीं देखते हैं। यह अच्छा है।

# Logging level set to DEBUG in both local program and library. 
Info: some_local_func() 
Debug: some_local_func() 
Info: some_lib.some_lib_func() 
Debug: some_lib.some_lib_func() 
Info: some_special_func() 
Debug: some_special_func(): This message can be controlled at the function/method level. 

ओह।

# ACK! Setting the library's logging level to DEBUG output 
# all debug messages from the library. (Use logging_tree.printout() 
# To see why.) 
# Let's change the library's logging level to INFO and 
# only some_special_func()'s level to DEBUG so we only see 
# debug message from some_special_func() 
# Logging level set to DEBUG in local program, INFO in library and 
# DEBUG in some_lib.some_special_func() 
Info: some_local_func() 
Debug: some_local_func() 
Info: some_lib.some_lib_func() 
Info: some_special_func() 
Debug: some_special_func(): This message can be controlled at the function/method level. 

यह भी केवल डिबग संदेशों केवल some_special_func() से प्राप्त करना संभव है। logging_tree.printout() का उपयोग यह पता लगाने के लिए करें कि कौन से लॉगिंग स्तर को ऐसा करने के लिए ट्विक करना है!

संबंधित मुद्दे