-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
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
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 |