Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Logging and Handling
#1
Hello,

I typically write small programs for Python and never attempted to use logging or exception handling. This is all new to me, but with this program I'm currently writing I would like to incorporate error handling and file logging.

I'm receiving the following error when attempting to do so:

Error:
Traceback (most recent call last): File "C:/Users/AnelliaF/.PyCharmCE2019.3/config/scratches/sql_create_csv_w_logging.py", line 15, in main conn = pyodbc.connect('Driver={SQL Server};' pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53)') During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/Users/AnelliaF/.PyCharmCE2019.3/config/scratches/sql_create_csv_w_logging.py", line 45, in <module> main() File "C:/Users/AnelliaF/.PyCharmCE2019.3/config/scratches/sql_create_csv_w_logging.py", line 20, in main print("Error %d: %s" % (e.args[0], e.args[1])) TypeError: %d format: a number is required, not str
Here is the code:

import pyodbc
import csv
import logging
import sys

def main():
    #logging.basicConfig(filename='c:\\temp\\test.log', format='%(filename)s: %(message)s', level=logging.DEBUG)
    # Configure logging
    logging.basicConfig(filename='c:\\temp\\test.log', format='%(asctime)s: %(message)s', level=logging.DEBUG)
    logger = logging.getLogger()

    logger.info('Connecting to database...')
    # Create connection object that represents the database
    try:
        conn = pyodbc.connect('Driver={SQL Server};'
                              'Server=MJ;'
                              'Database=AdventureWorks2012;'
                              'Trusted_Connection=yes;')
    except pyodbc.Error as e:
        print("Error %d: %s" % (e.args[0], e.args[1]))
        sys.exit(1)

    logger.info('Selecting records...')
    cursor = conn.cursor()
    script = """
    SELECT TOP 10 * FROM person.person
    """
    cursor.execute(script)

    logger.info('Writing to CSV file...')
    with open("c:\\temp\\csv_from_sql.csv", 'w') as csv_from_sql:
        csv_writer = csv.writer(csv_from_sql, delimiter=',', lineterminator='\n')
        # Write field name header line
        csv_writer.writerow([i[0] for i in cursor.description])
        # Write data rows
        # Use enumerate() method to count rows
        for row_num, row in enumerate(cursor, start = 1):
            csv_writer.writerow(row)

    logger.debug(f'Number of records: {row_num}')
    #print(f'Number of rows: {row_num}')
    logger.info('Finished writing file')

if __name__ == '__main__':
    main()
I have two questions:

1 - It seems my exception is failing, but unsure why?
2 - Is there anywhere else in the code I should attempt to add logging and handling?

Thanks,
Frank
Reply
#2
with %d string format code you need to pass a number, you pass a string instead, thus the TypeError

>>> spam = 100
>>> eggs  = '200'
>>> print('%d' %spam)
100
>>> print('%d' %eggs)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str
As a side note, not relevant to error you get, instead of old-style formatting, use str.format() method or f-strings
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I went with new style format as you stated:

print("Error {}: {}".format(e.args[0], e.args[1]))
Error:
Error 08001: [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53)
It worked.

Any other areas in the code should I place logging or handling? Or is there other ways to handle the exceptions?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 341 Feb-15-2024, 11:15 AM
Last Post: rawatg
Star python exception handling handling .... with traceback mg24 3 1,219 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Handling pdf files with python. fuzzin 1 1,219 Jan-19-2022, 02:24 PM
Last Post: ThiefOfTime
  Handling Python Fatal Error richajain1785 7 5,764 Oct-14-2021, 01:34 PM
Last Post: Tails86
  Handling multi-input/output audio in python bor1904 4 3,498 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  Python Requests package: Handling xml response soumyarani 1 2,103 Sep-14-2020, 11:41 AM
Last Post: buran
  Handling Multiple USB ports in Python samalpramod 0 4,365 Aug-01-2020, 07:40 PM
Last Post: samalpramod
  Python logging error Mekala 2 1,904 Jun-25-2020, 04:03 AM
Last Post: Mekala
  Exception handling in regex using python ShruthiLS 1 2,332 May-04-2020, 08:12 AM
Last Post: anbu23
  Python logging RotatingFileHandler and TimedRotatingFileHandler is not working with Q waytosatyam7 2 4,730 Dec-24-2019, 08:44 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020