You can import logging and logging.(debug|info|warning|error)(...) to log information. Log messages will be written to stderr instead of stdout, which is what print defaults to unless you specify file=sys.stderr when calling print.
You can also specify how you'd like to log things with a log format.
For example in metalign/log.py
import logging
# for more logging attributes, see https://docs.python.org/3.8/library/logging.html#logrecord-attributes
LOG_FORMAT = '{asctime} {levelname:<8} {message} [{name}:{funcName}:{lineno}]'
def init_logging(logging_verbosity=0):
logging_levels = [logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG]
if logging_verbosity > (len(logging_levels) - 1):
logging_verbosity = 3
lvl = logging_levels[logging_verbosity]
logging.basicConfig(format=LOG_FORMAT, level=lvl, style='{')
In your main command line entry function, init your logging with whatever verbosity you wish:
from metalign.log import init_logging
# where args.verbosity is `parser.add_argument('--verbosity', '-v', action='count', default=0)`
# only logging.error messages logged by default
# see https://docs.python.org/3/library/argparse.html#action
def main(...):
init_logging(args.verbosity) # where verbosity is a count (-vvv = 3, -v = 1)
In another module (e.g. metalign/do_stuff.py):
import logging
logger = logging.getLogger(__file__)
def calc_stuff(x):
logger.info(f'Doing stuff with {x}'
if x > 10:
logger.warning(f'x is too big! ({x}>10)'
The built-in logging module for Python is quite flexible so instead of this:
https://github.com/nlapier2/Metalign/blob/94b6820da1396dd3187dc96cdb9fbb0511636d91/map_and_profile.py#L11-L19
You can
import logging
andlogging.(debug|info|warning|error)(...)
to log information. Log messages will be written to stderr instead of stdout, which is whatprint
defaults to unless you specifyfile=sys.stderr
when callingprint
.You can also specify how you'd like to log things with a log format.
For example in
metalign/log.py
In your main command line entry function, init your logging with whatever verbosity you wish:
In another module (e.g.
metalign/do_stuff.py
):