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