nlapier2 / Metalign

Metalign: efficient alignment-based metagenomic profiling via containment min hash
MIT License
32 stars 7 forks source link

Use Python logging module? #17

Open peterk87 opened 4 years ago

peterk87 commented 4 years ago

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 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)'
nlapier2 commented 4 years ago

Thanks, I'm looking into this.