Converting A Function Into A Singleton Class That Returns One Instance
I am doing this project on Python 2.7 and I am trying to convert this function that returns a logging object. I am trying to make sure that I can utilize the same object instance t
Solution 1:
I don't understand why different loggers are generated when using the same name.
To force a single logger instance, get the logger once and store it in a global variable.
logger = None# global variabledefsetLogger(file_name):
global logger
if logger == None:
print('Creating logger')
logger = logging.getLogger(__name__) # only run onceifnotgetattr(logger, 'handler_set', None):
........
return logger
+++++++++++++++++++++++++++++++++++++++++++++++
I set up some modules to narrow down the issue.
--- mylogger.py ---
import logging
logger = None# global variabledefsetLogger(file_name="LogOut.log"):
global logger
if logger == None:
print('Creating logger')
logger = logging.getLogger(__name__) # only run onceifnotgetattr(logger, 'handler_set', None):
logger.setLevel(logging.INFO)
stream_handler = logging.StreamHandler()
file_handler = logging.FileHandler(file_name)
formatter = logging.Formatter('%(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
logger.setLevel(logging.INFO)
logger.propagate = False
logger.handler_set = Truereturn logger
--- modc.py ---
import mylogger
def writelog():
print("Hello from " + __name__)
print(__name__, 'LogID:', id(mylogger.setLogger()))
mylogger.setLogger().warning("Hello from " + __name__)
--- modb.py ---
import mylogger
import modc
def writelog():
print("Hello from " + __name__)
print(__name__, 'LogID:', id(mylogger.setLogger()))
mylogger.setLogger().warning("Hello from " + __name__)
modc.writelog()
--- moda.py ---
import mylogger
def writelog():
print("Hello from " + __name__)
print(__name__, 'LogID:', id(mylogger.setLogger()))
mylogger.setLogger().warning("Hello from " + __name__)
import modb
import modc
writelog()
modb.writelog()
modc.writelog()
I ran moda.py
. Here is the output. The logger is created once and every module uses the same instance of the logger. All messages are saved to the same file. Note that I'm using python 3.7.
Hello from modc
Creating logger
modc LogID: 1764613056456
Hello from modc
Hello from __main__
__main__ LogID: 1764613056456
Hello from __main__
Hello from modb
modb LogID: 1764613056456
Hello from modb
Hello from modc
modc LogID: 1764613056456
Hello from modc
Post a Comment for "Converting A Function Into A Singleton Class That Returns One Instance"