How to ignore the exception that is thrown when registering in python?

I work in a large python module (Python 2.6.6) and log in thousands of places and I use the python logger module. Now I want to ignore the exception that is thrown during logging. One of the main possible scenarios 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)

, my_logger.debug('i = %d' % i) , - , .. . , n . , - (- , ..). , .

:   , , - python try-except, logger. , , python, ?

+2
2

, logging .

(, ..) try..except Exception. , logging.Handler.handeError() sys.stderr. logging.raiseExceptions False, :

import logging

logging.raiseExceptions = False

:

, emit(). raiseExceptions False, . , - , . , . , . ( raiseExceptions True, ).

, RotatingFileHandler, , , . , delay=True , :

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

, , , , LOG_FILENAME .

+7

, .

try - except

:

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

except , .

except , , continue pass, .

+1

Source: https://habr.com/ru/post/1679936/


All Articles