Lazy logger message string evaluation

前端 未结 6 709
囚心锁ツ
囚心锁ツ 2020-12-02 11:12

I\'m using standard python logging module in my python application:

import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(\"log\")
whi         


        
6条回答
  •  失恋的感觉
    2020-12-02 11:35

    If you depend only on accessing global state attributes, you can instantiate a python class and lazify it by using the __str__ method:

    class get_lazy_debug(object):
        def __repr__(self):
            return ' '.join(
                    str(i) for i in range(20)
                )
    
    # Allows to pass get_lazy_debug as a function parameter without 
    # evaluating/creating its string!
    get_lazy_debug = get_lazy_debug()
    
    logger.debug( 'Stupid log message', get_lazy_debug )
    

    Related:

    1. Conditionally evaluated debug statements in Python
    2. What are metaclasses in Python?

提交回复
热议问题