0

Hello I coding a simple looging in python with 2 handlers and 1 formatter and got a AttributeError but can't find where. Here My code:

import logging
from logging.handlers import RotatingFileHandler


#Configuration loggeur
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
file_handler = RotatingFileHandler(filename='activity2.log', mode='a', maxBytes=1000000, backupCount= 2, encoding='utf-8')

file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.DEBUG)
logger.addHandler(stream_handler)

logger.warning('On y va')

And here the ouput:

--- Logging error ---
Traceback (most recent call last):
  File "D:\Anaconda\envs\scapenv\lib\logging\__init__.py", line 1081, in emit
    msg = self.format(record)
  File "D:\Anaconda\envs\scapenv\lib\logging\__init__.py", line 925, in format
    return fmt.format(record)
AttributeError: 'int' object has no attribute 'format'
Call stack:
  File "d:/02 - Programmes/08 - Passerelles/HBS/St Julien - GS Nelson Mandela/Py Excel/testlog.py", line 32, in <module>
    logger.warning('On y va')
Message: 'On y va'
Arguments: ()

If y have some clues...

1 Answer 1

2

Third line starting from the bottom:

stream_handler.setFormatter(logging.DEBUG)

You are giving logging.DEBUG instead of formatter as argument.

Fix:

stream_handler.setFormatter(formatter)
Sign up to request clarification or add additional context in comments.

4 Comments

Hello thx for the answers but it not it: setFormatter is for fixing the level the handler take. Here i put logging.DEBUG who is a int of value 10.
It's definitely should fix your problem.
@olivier logging.DEBUG equals to 10. Just do print(loggin.DEBUG) to see it. That is why you got an error.
Ok my bad y are right it was the problem thanks you

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.