Permalink
Cannot retrieve contributors at this time
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?
Micelle_Shape_Identifier/logger.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (35 sloc)
1.47 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |