I have a such logger initializing function:
def generate_logger():
import logging
LOG_FILENAME = os.path.join(PROJECT_DIR, "mylog.log")
FORMAT = "%(asctime)s : %(message)s"
logger = logging.getLogger()
logger.setLevel(logging.INFO)
fh = logging.FileHandler(LOG_FILENAME)
formatter = logging.Formatter(FORMAT)
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger
And at some part of my code I have such exception catching:
logger = generate_logger()
except AttributeError:
logger.error('Opps we got an error')
Weirdly I get same error written 2 times and it can be caugh only once, once I change logger.error('Opps we got an error')
with print "test"
, I get "test" printed once.
What can be the problem and the solution.
Regards
Multiple calls to getLogger() with the same name will always return a reference to the same Logger object". NOTE: even if you init a loggers from other modules with same name, you will still get the same logger, therefore calling i.e logger.info('somthing') will log as many times as you initiated the logger class.
The logging module can be used directly from multiple threads. The reason is because the logging module is thread-safe.
Levels of Severity- The logger module has several levels of severity. The default logging level is warning. Print- The only time when print() is a better option than logging is when the goal is to display a help statement for a command line application.
You are adding a new FileHandler to the root logger every time you call that function: the call to logger.getLogger()
without a name argument returns the same logger object every time.
You should call generate_logger()
only once and then simply get the same logger object by calling logger.getLogger()
:
generate_logger()
# .... some time later
log = logger.getLogger()
except AttributeError:
log.error('Opps we got an error')
(note that you do not need generate_logger()
to return a value now)
I also faced the same problem and came across this page. Yes, I was also creating multiple handlers. In generate_logger()
, you can check if there's any other handlers and delete them.
def generate_logger():
import logging
LOG_FILENAME = os.path.join(PROJECT_DIR, "mylog.log")
FORMAT = "%(asctime)s : %(message)s"
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Reset the logger.handlers if it already exists.
if logger.handlers:
logger.handlers = []
fh = logging.FileHandler(LOG_FILENAME)
formatter = logging.Formatter(FORMAT)
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With