1

I am trying to add FileHandler to logger object in my script:

FOO_LOGGER = logging.getLogger(LOGGER_NAME)

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

and I am getting this error:

AttributeError: 'Logger' object has no attribute 'FileHandler'

I am using python version Python 2.7.6

3 Answers 3

5

It has no object like that.

Try:

import logging

logger = logging.getLogger('simple_example')

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

logger.addHandler(fh)

More can be found here: https://docs.python.org/2/howto/logging-cookbook.html

Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

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!")

Comments

1

To write to console and a file:

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]
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.