Skip to content

Commit

Permalink
Add logger module (iss #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
astrochun committed Mar 5, 2020
1 parent 01a5d18 commit 66821d8
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions DataRepository_patrons/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sys
from os.path import join

import logging
formatter = logging.Formatter('%(asctime)s - %(levelname)8s: %(message)s', "%H:%M:%S")


class LogClass:
"""
Purpose:
Main class to log information to stdout and ASCII logfile
Note: This code is identical to the one used in DataRepository_research_themes:
https://github.com/ualibraries/DataRepository_research_themes
To use:
log = LogClass(log_dir, logfile).get_logger()
Parameters:
log_dir: Relative path for exported logfile directory
logfile: Filename for exported log file
"""

def __init__(self, log_dir, logfile):
self.LOG_FILENAME = join(log_dir, logfile)

def get_logger(self):
log_level = logging.INFO
log = logging.getLogger(self.LOG_FILENAME)
if not getattr(log, 'handler_set', None):
log.setLevel(logging.INFO)
sh = logging.StreamHandler(sys.stdout)
sh.setFormatter(formatter)
log.addHandler(sh)

fh = logging.FileHandler(self.LOG_FILENAME)
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
log.addHandler(fh)

log.setLevel(log_level)
log.handler_set = True
return log

0 comments on commit 66821d8

Please sign in to comment.