为什么我的log会输出三行信息?

王胖胖
  • 29
import logging
import logging.handlers
#LOG
import time


def log_info(log):
    LOG_FILENAME='log'
    # Set up a specific logger with our desired output level
    my_logger = logging.getLogger('log')
    my_logger.setLevel(logging.INFO)
    # Add the log message handler to the logger
    handler = logging.handlers.RotatingFileHandler(LOG_FILENAME,
                maxBytes=1048576,
                backupCount=5,
               )
    my_logger.addHandler(handler)
    # Log some messages
    my_logger.info(log)


def test(log):
    log_info(log=log)



for i in range(2):
    print i
    a = "1111111111111"
    b = test(a)

结果:
1111111111111
1111111111111
1111111111111

如何解决?正确应该是两行啊。。。。。。 掉到延迟绑定的坑里了?

回复
阅读 2.3k
2 个回答
✓ 已被采纳
import logging
import logging.handlers
# LOG
import time


class Logger:
    def __init__(self):
        LOG_FILENAME = 'log'
        # Set up a specific logger with our desired output level
        self.logger = logging.getLogger('log')
        self.logger.setLevel(logging.INFO)
        # Add the log message handler to the logger
        handler = logging.handlers.RotatingFileHandler(LOG_FILENAME,
                                                       maxBytes=1048576,
                                                       backupCount=5,
                                                       )
        self.logger.addHandler(handler)

    def info(self, log):
        self.logger.info(log)


logger = Logger()
for i in range(2):
    print i
    a = "1111111111111"
    logger.info(a)

试了下这个,挺有意思的~

for i in range(5):
    b = test(i)

结果:

0
1
1
2
2
2
3
3
3
3
4
4
4
4
4
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏