Skip to content

Commit

Permalink
Make the log level configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
YtvwlD committed Sep 19, 2017
1 parent 81b51f6 commit 85c5567
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
19 changes: 13 additions & 6 deletions metecli/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
import logging
log = logging.getLogger(__name__)

def setup_logging(log_level):
numeric_log_level = getattr(logging, log_level.upper(), None)
if not numeric_log_level:
raise Exception("Invalid log level: {}".format(log_level))
logging.basicConfig(level=numeric_log_level)

def do():
parser = argparse.ArgumentParser(description="A command line interface to mete.")
subparsers = parser.add_subparsers(help="commands")
Expand All @@ -14,22 +20,23 @@ def do():
audits.setup_cmdline(subparsers)
drinks.setup_cmdline(subparsers)
config.setup_cmdline(subparsers)
parser.add_argument("--loglevel", type=str, help="{debug, info, *warning*, error, critical}", default="warning")
parser.add_argument("--log_level", type=str, help="{debug, info, warning, error, critical}")
parser.add_argument("--configpath", type=str, help="the path where to place the config file(s)")
parser.add_argument("--configname", type=str, help="the name of the config to use")

args = parser.parse_args()
numeric_log_level = getattr(logging, args.loglevel.upper(), None)
if not numeric_log_level:
print("Invalid log level.")
return
logging.basicConfig(level=numeric_log_level)
if args.log_level:
setup_logging(args.log_level)

log.debug("Parsed args: %s", args)
if(not hasattr(args, "func")):
print("You must provide a topic. Please see --help.")
return

conf = config.Config(path=args.configpath, name=args.configname)

if not args.log_level:
setup_logging(conf["display"]["log_level"])

test_terminal_utf8()
args.func(args, conf)
8 changes: 7 additions & 1 deletion metecli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
log = logging.getLogger(__name__)

DEFAULT_SETTINGS = {
"version": 1,
"version": 2,
"connection": {},
"display": {
"table_format": "grid",
"log_level": "warning",
}
}

Expand Down Expand Up @@ -146,6 +147,11 @@ def _migrate(self):
log.info("Configuration doesn't have a version. Asssuming v1.")
self["version"] = 1
self.save()
if self["version"] == 1: # v1 -> v2
log.info("Migrating to v2: Adding display.log_level.")
self["display"]["log_level"] = "warning"
self["version"] = 2
self.save()

def save(self):
log.debug("Saving config....")
Expand Down

0 comments on commit 85c5567

Please sign in to comment.