-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
8f80e02
commit 8a3c402
Showing
1 changed file
with
42 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,42 @@ | ||
""" | ||
mamotif.logging | ||
----------------- | ||
Logging configurations for MAmotif. | ||
""" | ||
|
||
import logging | ||
import sys | ||
|
||
# Initialize the default logger with a NullHandler | ||
logger = logging.getLogger('mamotif') | ||
logger.addHandler(logging.NullHandler()) | ||
|
||
|
||
class CleanFormatter(logging.Formatter): | ||
"""Clean logging formatter, omit the level label for `logging.INFO`.""" | ||
|
||
def format(self, record): | ||
if record.levelno != logging.INFO: | ||
record.msg = f"{record.levelname}: {record.msg}" | ||
return super().format(record) | ||
|
||
|
||
def setup_logger(verbose=False): | ||
"""Setup logger with a stderr stream handler.""" | ||
logger.setLevel(logging.DEBUG) | ||
# clear all handlers | ||
for handler in logger.handlers: | ||
logger.removeHandler(handler) | ||
|
||
sh = logging.StreamHandler(stream=sys.stderr) | ||
if verbose: | ||
sh.setLevel(logging.DEBUG) | ||
formatter = logging.Formatter( | ||
"%(asctime)s %(name)-28s %(lineno)-4d %(levelname)-8s %(message)s", | ||
datefmt="%Y-%m-%d %H:%M") | ||
else: | ||
sh.setLevel(logging.INFO) | ||
formatter = CleanFormatter() | ||
sh.setFormatter(formatter) | ||
logger.addHandler(sh) |