olivier
olivier

Reputation: 179

Python --- Logging error --- AttributeError but can't find where?

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...

Upvotes: 0

Views: 1628

Answers (1)

Arount
Arount

Reputation: 10431

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)

Upvotes: 2

Related Questions