AttributeError:' Logger'对象没有属性,警告'

时间:2017-05-23 12:44:22

标签: python python-3.x logging

我试图根据python documentation在我的python脚本中实现一个记录器。这是代码:

import logging

def generateLogger(loggername='SM-Logger', path="logs/log.log"):

    logger = logging.getLogger(loggername)
    logger.setLevel(logging.DEBUG)

    ch = logging.StreamHandler()
    ch.setLevel(logging.ERROR)

    formatter = logging.Formatter('%(asctime)s - %(name)s\
                              - %(levelname)s - %(message)s')

    ch.setFormatter(formatter)

    logger.addHandler(ch)

    return logger

logger = generateLogger("testlogger", "testlog.log")
logger.WARNING("testtest")

我收到此错误消息:

File "loggertest.py", line 39, in <module>
    logger.WARNING("testtest")
AttributeError: 'Logger' object has no attribute ‚WARNING'

1 个答案:

答案 0 :(得分:3)

解决方案是更改最后一行

logger.WARNING("testtest")

logger.warning("testtest")

小写&#34;警告&#34;是功能,大写&#34;警告&#34;是变量。