Alex Brodov
Alex Brodov

Reputation: 3515

python 2.7 - setting up 2 loggers

I'me trying to setup 2 loggers, unfortunately one of them doesn't write into the file, Here is a snippet of my code:

LOG_FILENAME = 'test.log'
LOG_FILENAME2 = 'test2.log'
error_counter = 0
error_logger = CustomLogger(LOG_FILENAME2, 'w', '%(asctime)s - %(levelname)s -  %(message)s',
                            '%d/%m/%Y %H:%M:%S')
error_logger.set_level('info')
error_logger.basic_config()
print "This is the first logger: {0}".format(error_logger.get_file_path)
error_logger.log_message("This is a test message of the first instance")

warning_logger = CustomLogger(LOG_FILENAME, 'w', '%(asctime)s - %(levelname)s -  %(message)s',
                              '%d/%m/%Y %H:%M:%S')

warning_logger.set_level('warning')
warning_logger.basic_config()
print "This is the the second logger: {0} ".format(warning_logger.get_file_path)
warning_logger.log_message("this is a test message of the second instance")

Here is the custom class that i've created:

class CustomLogger(object):

    LEVELS = {'debug': logging.DEBUG,
              'info': logging.INFO,
              'warning': logging.WARNING,
              'error': logging.ERROR,
              'critical': logging.CRITICAL}

    def __init__(self, i_file_path=None, i_filemode=None, i_format=None, i_date_format=None, i_log_level=None):

        self.__file_path = i_file_path
        self.__filemode = i_filemode
        self.__format = i_format
        self.__date_format = i_date_format
        self.__log_level = i_log_level

def basic_config(self):
        logging.basicConfig(
            filename=self.__file_path,
            filemode=self.__filemode,
            format=self.__format,
            datefmt=self.__date_format,
            level=self.__log_level
            )
def log_message(self, i_message):
        try:
            if None in (self.__file_path, self.__log_level, self.__filemode, self.__date_format, self.__format):
                raise ErrorLoggerPropertiesRequiredException()
        except ErrorLoggerPropertiesRequiredException as e:
            print "{0}".format(e.message)
        else:
            curr_logger = logging.getLogger(self.__file_path)
            print "writing to log {0}".format(i_message)
            curr_logger.log(self.__log_level, i_message)

It's creating and writing only to the first logger, i've tried many things i saw on Python Documentation that there is another property called disable_existing_loggers which is by default True, i;ve tried accessing this property using logging.config.fileConfig(fname, defaults=None, disable_existing_loggers=False), but it seems that there is no such method.

Upvotes: 3

Views: 283

Answers (1)

rkh
rkh

Reputation: 1791

I believe you are running into a problem I have run into many times:

logging.basicConfig()

is calling the module level configuration, and, according to the docs:

This function does nothing if the root logger already has handlers configured for it.

In other words, it will only have an effect the first time it is called. This function is only meant as a "last resort" config, if I understand it correctly.

You should instead configure each logger based on the "self" reference, rather than the global basic config...

A basic pattern I use for each module level logger is (note the import statement!):

import logging.config
try:
       logging.config.fileConfig('loggingpy.conf', disable_existing_loggers=False)
except Exception as e:
    # try to set up a default logger
    logging.basicConfig(level=logging.INFO,
                        format="%(asctime)s:%(name)s:%(lineno)d %(levelname)s : %(message)s")
main_logger = logging.getLogger(__name__)

I am sure I copied this from somewhere...

Upvotes: 3

Related Questions