-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add loggin * create log file in tmp path * add logger file * remove file and line description in log file * fix stdout output to pass pytests * added print function along with logging function; added the logging.exception function to catch the raise exception. * logger fix to remove redundant prints * fix handlers * Refactored logger and cli * Test -w x -d args * Decoupled stdout from file log * Use tempfile for log * Fixed logic for delOutputFiles() Co-authored-by: lkagami <[email protected]>
- Loading branch information
1 parent
3e0a978
commit 2f1ddc4
Showing
9 changed files
with
173 additions
and
77 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# from https://packaging.python.org/guides/single-sourcing-package-version/ | ||
# using option 2 | ||
# updated automatically via pre-commit git-hook | ||
__version__ = "2021.12.23" | ||
__version__ = "2021.12.24" |
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
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,71 @@ | ||
import os | ||
from shutil import move | ||
import sys | ||
import logging | ||
from tempfile import NamedTemporaryFile | ||
|
||
tmpLogFile = NamedTemporaryFile().name | ||
|
||
|
||
class LogFormatter(logging.Formatter): | ||
|
||
""" | ||
Define log formatter | ||
""" | ||
|
||
err_fmt = "ERROR: %(msg)s" | ||
warn_fmt = "WARNING: %(msg)s" | ||
dbg_fmt = "DEBUG: %(msg)s" | ||
info_fmt = "%(msg)s" | ||
|
||
def __init__(self): | ||
super().__init__(fmt="%(levelno)d: %(msg)s", datefmt=None, style="%") | ||
|
||
def format(self, record): | ||
|
||
# Save the original format configured by the user | ||
# when the logger formatter was instantiated | ||
format_orig = self._style._fmt | ||
|
||
# Replace the original format with one customized by logging level | ||
if record.levelno == logging.DEBUG: | ||
self._style._fmt = LogFormatter.dbg_fmt | ||
elif record.levelno == logging.INFO: | ||
self._style._fmt = LogFormatter.info_fmt | ||
elif record.levelno == logging.ERROR: | ||
self._style._fmt = LogFormatter.err_fmt | ||
elif record.levelno == logging.WARNING: | ||
self._style._fmt = LogFormatter.warn_fmt | ||
# Call the original formatter class to do the grunt work | ||
result = logging.Formatter.format(self, record) | ||
# Restore the original format configured by the user | ||
self._style._fmt = format_orig | ||
return result | ||
|
||
|
||
def copy_log(molecule): | ||
local_log = os.path.join(molecule.absHomeDir, "acpype.log") | ||
if os.path.exists(local_log): | ||
os.remove(local_log) | ||
if os.path.exists(tmpLogFile): | ||
move(tmpLogFile, local_log) | ||
|
||
|
||
def set_logging_conf(level=20): | ||
# Setting logging configurations | ||
logging.root.setLevel(0) | ||
logger = logging.getLogger(__name__) | ||
if logger.handlers: | ||
logger.handlers.pop() | ||
|
||
fmt = LogFormatter() | ||
file_handler = logging.FileHandler(filename=tmpLogFile) | ||
stdout_handler = logging.StreamHandler(sys.stdout) | ||
file_handler.setLevel(logging.DEBUG) | ||
stdout_handler.setLevel(level) | ||
stdout_handler.setFormatter(fmt) | ||
file_handler.setFormatter(fmt) | ||
if not logger.handlers: | ||
logger.addHandler(file_handler) | ||
logger.addHandler(stdout_handler) | ||
return logger |
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
Oops, something went wrong.