0

I am working with the logging module for the first time. I was able to write the program as per my requirement. The logic written inside the try and except is also working almost successfully, and logs are getting generated in the log file. But for some reason I am seeing "AttributeError" on my IDE console for all the "logging.info" and "logging.exception". So, to cross verify I commented out all those location and this time my code ran without any error, but nothing was getting logged in the log file. Which was quite obvious. Below is the whole program

import logging
from logging.handlers import TimedRotatingFileHandler

logger = logging.handlers.TimedRotatingFileHandler('amitesh.log', when='midnight', interval=1)
logger.suffix = '%y_%m_%d.log'
# create a logging format
LOGGING_MSG_FORMAT = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger.setFormatter(LOGGING_MSG_FORMAT)

LOGGING_DATE_FORMAT = '%d-%b-%y %H:%M:%S'

# create basic configuration
logging.basicConfig(level=logging.INFO, format=LOGGING_MSG_FORMAT, datefmt=LOGGING_DATE_FORMAT)


root_logger = logging.getLogger('')


# add the handlers to the logger
root_logger.addHandler(logger)
while True:
    print(" ")
    print("This is a Logging demo")
    print(" ")
    logging.info("new request came")
    print(" ")
    try:
        x = int(input("Enter the first number: "))
        y = int(input("Enter the second number: "))
        print(x / y)
    except ZeroDivisionError as msg:
        print("cannot divide with zero")
        logging.exception(msg)
        print(" ")
    except ValueError as msg:
        print("enter only integer value")
        logging.exception(msg)
        print(" ")
    logging.info("executed successfully")

print(" ")

Below is the error message from my IDE console:

    return self._fmt.find(self.asctime_search) >= 0
AttributeError: 'Formatter' object has no attribute 'find'
Call stack:
  File "/Users/amitesh/PycharmProjects/Automation/Databases/DB_Conn.py", line 68, in <module>
    logging.info("new request came")
Message: 'new request came'
Arguments: ()

I have gone through the internet in last 2 days without any luck. Please help me figure out my mistake(s).

added more error:

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 388, in usesTime
    return self._fmt.find(self.asctime_search) >= 0
AttributeError: 'Formatter' object has no attribute 'find'
Call stack:
  File "/Users/amitesh/PycharmProjects/Automation/Databases/DB_Conn.py", line 68, in <module>
    logging.info("new request came")
Message: 'new request came'
Arguments: ()

Thank you.

user3521180
  • 1,044
  • 2
  • 20
  • 45
  • I don't see 'self._fmt' defined anywhere. Where does that come from? – pookie Mar 27 '19 at 12:47
  • I have added more detailed error in my original post. The 'self._fmt' is a part of constructor of the logging module, and when I open that __init__ file, I get " def usesTime(self): return self._fmt.find(self.asctime_search) >= 0" – user3521180 Mar 27 '19 at 12:54

1 Answers1

1

Removed format=LOGGING_MSG_FORMAT, LOGGING_MSG_FORMAT and defined the values inside the basicConfig with "format" param directly as the parameter "format" takes a String not the type Formatter.

user3521180
  • 1,044
  • 2
  • 20
  • 45
  • You should edit and update your original question, rather than post this as an answer. – pookie Mar 28 '19 at 13:54
  • 3
    @pookie I disagree. It could probably be phrased better, as answers go, but if this resolved the original issue, it deserves to be an answer. – jdmcnair Jun 21 '19 at 15:38
  • what @user3521180 intends to say is - change this line `LOGGING_MSG_FORMAT = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')` to `LOGGING_MSG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'`. This worked for me. – Batman May 28 '21 at 17:13