Skip to main content
added 689 characters in body
Source Link

I could find no documentation on Logger.none() and could not get it working. However, see second code below - maybe that's what they meant.

In the TestModule.py, you can define a function like this:

def add_two_numbers(x, y, logger=None):
    logger.info("Start add_two_numbers:  x=" + str(x) + " y=" + str(y))
    sum = x + y
    logger.info("sum=" + str(sum))
    return sum

In this case, logger is an optional parameter that is set to None. However, I can still do logger.info without errors, and no trace created, which would be the desired result.

So if caller passes a good logger it uses, if caller passes no arguments without the new logger that was added; the function runs fine, just doesn't do the logging.

FYI, this is my standard_logging routine, so I don't have to repeat it in each program.

def standard_logging(arg_filename, arg_max_megabytes, arg_backup_count):

    logger = logging.getLogger('my_logger')
    logger.setLevel(logging.DEBUG)
    log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    # logger = logging.getLogger()

    # Centralize Logging here:
    log_path = r'c:\Users\All Users\WebsiteGarden\Logs'
    log_file = os.path.join(log_path, arg_filename)
    if not os.path.exists(log_path):
        os.makedirs(log_path)

    # Create a file handler to write logs to a file
    file_handler = RotatingFileHandler(log_file,
                                       maxBytes=arg_max_megabytes*1024*1024,
                                       backupCount=arg_backup_count)
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(log_formatter)

    # Create a stream handler to print logs to the console
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setLevel(logging.INFO)  # You can set the desired log level for console output
    console_handler.setFormatter(log_formatter)

    # Add the handlers to the logger
    logger.addHandler(file_handler)
    logger.addHandler(console_handler)

    return logger

I could find no documentation on Logger.none() and could not get it working.

I could find no documentation on Logger.none() and could not get it working. However, see second code below - maybe that's what they meant.

In the TestModule.py, you can define a function like this:

def add_two_numbers(x, y, logger=None):
    logger.info("Start add_two_numbers:  x=" + str(x) + " y=" + str(y))
    sum = x + y
    logger.info("sum=" + str(sum))
    return sum

In this case, logger is an optional parameter that is set to None. However, I can still do logger.info without errors, and no trace created, which would be the desired result.

So if caller passes a good logger it uses, if caller passes no arguments without the new logger that was added; the function runs fine, just doesn't do the logging.

FYI, this is my standard_logging routine, so I don't have to repeat it in each program.

def standard_logging(arg_filename, arg_max_megabytes, arg_backup_count):

    logger = logging.getLogger('my_logger')
    logger.setLevel(logging.DEBUG)
    log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
    # logger = logging.getLogger()

    # Centralize Logging here:
    log_path = r'c:\Users\All Users\WebsiteGarden\Logs'
    log_file = os.path.join(log_path, arg_filename)
    if not os.path.exists(log_path):
        os.makedirs(log_path)

    # Create a file handler to write logs to a file
    file_handler = RotatingFileHandler(log_file,
                                       maxBytes=arg_max_megabytes*1024*1024,
                                       backupCount=arg_backup_count)
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(log_formatter)

    # Create a stream handler to print logs to the console
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setLevel(logging.INFO)  # You can set the desired log level for console output
    console_handler.setFormatter(log_formatter)

    # Add the handlers to the logger
    logger.addHandler(file_handler)
    logger.addHandler(console_handler)

    return logger
Source Link

Here is code that demonstrates my understanding of the other posted answers.

The main differences in styles to me are:

  1. add_two_numbers - we have to build and pass a logger, but then the log gets written intertwined with that of the calling program, which could be a big advantage in debugging

  2. multiple_two_numbers - no logger passed, so it builds it's own. I'm assuming code below is in 'TestModule.py' and the caller module or logger filename is "'TestModuleCaller.log'. Thus you get two trace files, all the functions go to TestModule.log (if you set them up that way), and that would be a separate trace file from tTestModulerCaller.log

I could find no documentation on Logger.none() and could not get it working.

import logging
from CommonFunctions import standard_logging

# Testing different ways of logging in modules (shared functions)

def add_two_numbers(x, y, logger):
    logger.info("Start add_two_numbers:  x=" + str(x) + " y=" + str(y))
    sum = x + y
    logger.info("sum=" + str(sum))
    return sum

def multiply_two_numbers(x, y):
    log_file = 'TestModule.log'
    logger = standard_logging(
        log_file, arg_max_megabytes=1, arg_backup_count=2)
    logger.info("Start multiply_two_numbers:  x=" + str(x) + " y=" + str(y))
    product = x + y
    logger.info("sum=" + str(product))
    return product


if __name__ == "__main__":

    log_file = 'TestModuleCallingProgram.log'
    logger = standard_logging(
        log_file, arg_max_megabytes=1, arg_backup_count=2)

    logger.info("Start testing logger passed to function ")
    result = add_two_numbers(5,6, logger)
    logger.info("End  demo 1")

    logger.info("Start testing logger not passed to function ")
    result = multiply_two_numbers(5,6)
    logger.info("End  demo 2")

    #logger.info("Start testing pass none logger in function  ")
    #result = add_two_numbers(5,6, Logger.none())
    #logger.info("End  demo 3")