Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore the exception which are generated during the logging in python?

Tags:

python

logging

I am working in a big python module(Python 2.6.6) and doing logging in thousand of places and i am using logger module of python. Now, i want to ignore the exception, which are generated during the logging. One of the basic scenario which could be possible is

import glob
import logging
import logging.handlers

LOG_FILENAME = '/home/abc/logging_rotatingfile_example.out'
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
              LOG_FILENAME, maxBytes=20, backupCount=5)
my_logger.addHandler(handler)
# Log some messages
for i in range(20):
    my_logger.debug('i = %d' % i)

Now suppose, during the my_logger.debug('i = %d' % i) some how the file is deleted from the system or permissions are changed and logger couldn't able to log due to disk failure, disk full etc. on the log file and generate the exception on the screen. It is distributed system and n number of process are using logger module. Please note process should not be aborted due to logging exception(due to disk failure, disk full etc.). So i want that exception to be ignored.

NOTE: Now one of the way i know to make wrapper class on python logger and use try-except for the logger function. But i have different requirements and i dont want to use that way so ,is there any other way to do this or any attribute of python logger which can be useful for my condition?

like image 621
Shanu Pandit Avatar asked Dec 11 '22 18:12

Shanu Pandit


2 Answers

You do not need to ignore exceptions, because the logging module already handles these for you.

All file operations (log emitting, rotation, etc.) are guarded with try..except Exception blocks. If exceptions occur, the logging.Handler.handeError() method will write the exception to sys.stderr instead. You can set logging.raiseExceptions to False to silence these:

import logging

logging.raiseExceptions = False

Quoting the documentation:

This method should be called from handlers when an exception is encountered during an emit() call. If the module-level attribute raiseExceptions is False, exceptions get silently ignored. This is what is mostly wanted for a logging system - most users will not care about errors in the logging system, they are more interested in application errors. You could, however, replace this with a custom handler if you wish. The specified record is the one which was being processed when the exception occurred. (The default value of raiseExceptions is True, as that is more useful during development).

If the filename you passed to RotatingFileHandler cannot be created, then the exception raised doesn't happen during logging however, it takes place while creating the handler. You'll either have to handle that exception there and then, or set delay=True when creating the handler to postpone opening the file until logging time:

handler = logging.handlers.RotatingFileHandler(
    LOG_FILENAME, maxBytes=20, backupCount=5, delay=True)

What you use comes down to wether or not you see providing an incorrect path in LOG_FILENAME is an application error or not at that point.

like image 123
Martijn Pieters Avatar answered Jun 01 '23 01:06

Martijn Pieters


So after your edit this is not really valid.

You can use the try - except method

Example:

try:
    file_handler =open("not_a_file_for_sure","r")
except IOError:
    print "File not found"

You can except any kind of error, or even all of them, but that is not advised.

Just except the error you are getting and continue or pass it if you don't care about it.

like image 42
Gábor Erdős Avatar answered Jun 01 '23 00:06

Gábor Erdős