Skip to content
Permalink
development
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
 
 
Cannot retrieve contributors at this time
"""
A simplistic Logger class which wraps the Python logging class
@author Peter Zaffetti
@date 2018
"""
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 = "Micelle Shape Identifier"
logger = logging.getLogger(classname)
log_format = logging.Formatter("%(asctime)s: %(filename)s: %(funcName)s: %(levelname)s:\t %(message)s")
file_handler = logging.FileHandler("logs/identifier_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)
# PDZ- only add if there is a file handler- this was causing a duplicate
# handler to be added every time a logger was requested with this getter function
if len(logger.handlers) == 1:
logger.addHandler(console_handler)
logger.setLevel(logging.DEBUG)
return logger