Skip to content

Commit

Permalink
Add logger for MAmotif
Browse files Browse the repository at this point in the history
  • Loading branch information
hongduosun committed May 14, 2020
1 parent 8f80e02 commit 8a3c402
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions mamotif/logging.py
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)

0 comments on commit 8a3c402

Please sign in to comment.