SaiKiran
SaiKiran

Reputation: 6504

Which logic is better for implementing exception logging ( Particularly in case of logging not in case of regular string formatting )?

In python, i have a logging mechanism setup which will catch all the errors and exceptions to a file.

Logic - 1

logger.info('Running get_all_files_from_cmc')
    try:
        pipe.get_all_files_from_cmc()
    except Exception as e:
        logger.exception('Get_all_files_from_cmc Failed {}'.format(e))

Logic -2

logger.info('Running get_all_files_from_cmc')
    try:
        pipe.get_all_files_from_cmc()
    except Exception as e:
        logger.exception('Get_all_files_from_cmc Failed' + e)

Logic - 3

logger.info('Running get_all_files_from_cmc')
        try:
            pipe.get_all_files_from_cmc()
        except Exception as e:
            logger.exception('Get_all_files_from_cmc Failed')

Which of the two logics is correct to implement as formatting inside the logging is it wrong?

Or Logic -3 because logging handles the e value automatically.?

Upvotes: 0

Views: 69

Answers (2)

Vinay Sajip
Vinay Sajip

Reputation: 99415

Logic 3 is best - because you are using exception(), the exception traceback will be stored in the log. You only need to indicate what operation failed, which your Logic 3 does.

Upvotes: 1

zipa
zipa

Reputation: 27879

.format() is proper way to do it because if you try to concatenate (+) 'Get_all_files_from_cmc Failed' with anything other than str it would cause another error.

Upvotes: 4

Related Questions