AttributeError:'Logger'对象没有属性'FileHandler'

时间:2016-07-14 10:43:40

标签: python python-2.7

我正在尝试将FileHandler添加到我的脚本中的logger对象中:

FOO_LOGGER = logging.getLogger(LOGGER_NAME)

# create the logging file handler
fh = FOO_LOGGER.FileHandler('foo.log')

我收到此错误:

  

AttributeError:'Logger'对象没有属性'FileHandler'

我正在使用python版本的Python 2.7.6

3 个答案:

答案 0 :(得分:1)

它没有这样的对象。

尝试:

import logging

logger = logging.getLogger('simple_example')

# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')

logger.addHandler(fh)

更多信息可以在这里找到:https://docs.python.org/2/howto/logging-cookbook.html

答案 1 :(得分:0)

尝试一下

import logging

#Create and configure logger 
logging.basicConfig(filename="foo.log", 
                format='%(asctime)s %(message)s', 
                filemode='w')

FOO_LOGGER = logging.getLogger(LOGGER_NAME)

FOO_LOGGER.setLevel(logging.DEBUG)

FOO_LOGGER.info("Your Message!")

答案 2 :(得分:0)

要写入控制台和文件:

console = logging.StreamHandler()
file_handler = logging.FileHandler("D:\Shared\wbc_customer.log.txt", "w")
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)-15s: %(name)s: %(levelname)s: %(message)s',
    handlers = [file_handler, console]
)