Tidy logging.

This commit is contained in:
Chris Berkhout 2021-05-31 15:10:15 +02:00
parent 229ea109ef
commit 9996354108
3 changed files with 40 additions and 12 deletions

View file

@ -8,19 +8,21 @@ from pricehist import __version__, outputs, sources
from pricehist.fetch import fetch from pricehist.fetch import fetch
from pricehist.format import Format from pricehist.format import Format
from pricehist.series import Series from pricehist.series import Series
from pricehist import logger
def cli(args=None, output_file=sys.stdout): def cli(args=None, output_file=sys.stdout):
start_time = datetime.now() start_time = datetime.now()
logging.basicConfig(format="%(message)s", level=logging.INFO)
logger.init()
parser = build_parser() parser = build_parser()
args = parser.parse_args() args = parser.parse_args()
if args.debug: if args.debug:
logging.getLogger().setLevel(logging.DEBUG) logger.show_debug()
elif args.verbose: elif args.verbose:
logging.getLogger().setLevel(logging.INFO) logger.show_info()
logging.debug(f"Began pricehist run at {start_time}.") logging.debug(f"Began pricehist run at {start_time}.")
@ -49,8 +51,8 @@ def cli(args=None, output_file=sys.stdout):
) )
if series.type not in source.types(): if series.type not in source.types():
logging.critical( logging.critical(
f"ERROR: The requested price type '{series.type}' is not " f"The requested price type '{series.type}' is not "
f"recognised by the {source.id()} source!" f"recognized by the {source.id()} source!"
) )
sys.exit(1) sys.exit(1)
fmt = Format.fromargs(args) fmt = Format.fromargs(args)

View file

@ -46,19 +46,19 @@ def _cov_description(
if start_uncovered == 0 and end_uncovered > 0: if start_uncovered == 0 and end_uncovered > 0:
return ( return (
f"starting as requested and ending {end_uncovered} " f"which ends {end_uncovered} day{s(end_uncovered)} earlier than "
f"day{s(end_uncovered)} earlier than requested" f"requested"
) )
elif start_uncovered > 0 and end_uncovered == 0: elif start_uncovered > 0 and end_uncovered == 0:
return ( return (
f"starting {start_uncovered} day{s(start_uncovered)} later " f"which starts {start_uncovered} day{s(start_uncovered)} later "
"than requested and ending as requested" "than requested"
) )
elif start_uncovered > 0 and end_uncovered > 0: elif start_uncovered > 0 and end_uncovered > 0:
return ( return (
f"starting {start_uncovered} day{s(start_uncovered)} later " f"which starts {start_uncovered} day{s(start_uncovered)} later "
f"and ending {end_uncovered} day{s(end_uncovered)} earlier " f"and ends {end_uncovered} day{s(end_uncovered)} earlier "
"than requested" f"than requested"
) )
else: else:
return "as requested" return "as requested"

26
src/pricehist/logger.py Normal file
View file

@ -0,0 +1,26 @@
import logging
import sys
class Formatter(logging.Formatter):
def format(self, record):
message = record.msg % record.args
if record.levelno == logging.INFO:
return message
else:
return f"{record.levelname} {message}"
def init():
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(Formatter())
logging.root.addHandler(handler)
logging.root.setLevel(logging.WARNING)
def show_debug():
logging.root.setLevel(logging.DEBUG)
def show_info():
logging.root.setLevel(logging.INFO)