Skip to content
Permalink
8377d07783
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
…in a specific format. Fixed the logger creating a handler every time it is called.
1 contributor

Users who have contributed to this file

43 lines (32 sloc) 1.43 KB
"""
A simplistic Logger class which wraps the Python logging class
@author Peter Zaffetti
@date 10/10/2017
"""
import logging
import os
def get_logger(classname=None):
log_dir = "logs"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
root = logging.getLogger()
if root.handlers:
for handler in root.handlers:
root.removeHandler(handler)
# logging.basicConfig(format='%(asctime)s: %(filename)s: %(funcName)s: %(levelname)s:\t %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', filename="logs/generator_log.txt")
if classname is None:
classname = "Subdivision Generator"
logger = logging.getLogger(classname)
log_format = logging.Formatter("%(asctime)s: %(filename)s: %(funcName)s: %(levelname)s:\t %(message)s")
file_handler = logging.FileHandler("logs/generator_log.txt")
file_handler.setFormatter(log_format)
file_handler.setLevel(logging.DEBUG)
if len(logger.handlers) == 0: # PDZ- only add if there are no file handlers to begin with
logger.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_format)
console_handler.setLevel(logging.INFO)
if len(logger.handlers) == 1: # PDZ- only add if there is a file handler- this was causing a duplicate handler to be added every time a logger was gotten
logger.addHandler(console_handler)
logger.setLevel(logging.DEBUG)
return logger