Variable output verbosity via CLI parameter -v

This commit is contained in:
Gina Häußge 2015-10-28 19:18:40 +01:00
parent 55c0e2ca95
commit 9c56b2fd4e
4 changed files with 44 additions and 23 deletions

View file

@ -24,7 +24,7 @@ logging.basicConfig()
#~~ init methods to bring up platform
def init_platform(basedir, configfile, use_logging_file=True, logging_file=None,
logging_config=None, debug=False, uncaught_logger=None,
logging_config=None, debug=False, verbosity=0, uncaught_logger=None,
uncaught_handler=None, after_settings=None, after_logging=None):
settings = init_settings(basedir, configfile)
if callable(after_settings):
@ -35,6 +35,7 @@ def init_platform(basedir, configfile, use_logging_file=True, logging_file=None,
logging_file=logging_file,
default_config=logging_config,
debug=debug,
verbosity=verbosity,
uncaught_logger=uncaught_logger,
uncaught_handler=uncaught_handler)
if callable(after_logging):
@ -51,7 +52,7 @@ def init_settings(basedir, configfile):
return settings(init=True, basedir=basedir, configfile=configfile)
def init_logging(settings, use_logging_file=True, logging_file=None, default_config=None, debug=False, uncaught_logger=None, uncaught_handler=None):
def init_logging(settings, use_logging_file=True, logging_file=None, default_config=None, debug=False, verbosity=0, uncaught_logger=None, uncaught_handler=None):
"""Sets up logging."""
import os
@ -96,10 +97,13 @@ def init_logging(settings, use_logging_file=True, logging_file=None, default_con
"handlers": ["serialFile"],
"propagate": False
},
"tornado.application": {
"octoprint": {
"level": "INFO"
},
"tornado.general": {
"octoprint.util": {
"level": "INFO"
},
"octoprint.plugins": {
"level": "INFO"
}
},
@ -109,7 +113,11 @@ def init_logging(settings, use_logging_file=True, logging_file=None, default_con
}
}
if debug:
if debug or verbosity > 0:
default_config["loggers"]["octoprint"]["level"] = "DEBUG"
if verbosity > 1:
default_config["loggers"]["octoprint.plugins"]["level"] = "DEBUG"
if verbosity > 2:
default_config["root"]["level"] = "DEBUG"
if use_logging_file:
@ -134,8 +142,13 @@ def init_logging(settings, use_logging_file=True, logging_file=None, default_con
# make sure we log any warnings
logging.captureWarnings(True)
import warnings
warnings.simplefilter("always")
if verbosity > 2:
warnings.simplefilter("always")
elif debug or verbosity > 0:
warnings.simplefilter("default")
# make sure we also log any uncaught exceptions
if uncaught_logger is None:

View file

@ -13,10 +13,11 @@ import octoprint
class OctoPrintContext(object):
def __init__(self, configfile=None, basedir=None, debug=False):
def __init__(self, configfile=None, basedir=None, debug=False, verbosity=0):
self.configfile = configfile
self.basedir = basedir
self.debug = debug
self.verbosity = verbosity
pass_octoprint_ctx = click.make_pass_decorator(OctoPrintContext, ensure=True)
@ -62,12 +63,13 @@ def set_ctx_obj_option(ctx, param, value):
@click.group(name="octoprint", invoke_without_command=True, cls=click.CommandCollection,
sources=[server_commands, plugin_commands, devel_commands])
@click.option("--basedir", "-b", type=click.Path(), callback=set_ctx_obj_option, is_eager=True,
@click.option("--basedir", "-b", type=click.Path(), callback=set_ctx_obj_option, is_eager=True, expose_value=False,
help="Specify the basedir to use for uploads, timelapses etc.")
@click.option("--config", "-c", "configfile", type=click.Path(), callback=set_ctx_obj_option, is_eager=True,
@click.option("--config", "-c", "configfile", type=click.Path(), callback=set_ctx_obj_option, is_eager=True, expose_value=False,
help="Specify the config file to use.")
@click.option("--debug", "-d", is_flag=True, callback=set_ctx_obj_option, is_eager=True,
help="Enable debug mode")
@click.option("--verbose", "-v", "verbosity", count=True, callback=set_ctx_obj_option, is_eager=True, expose_value=False,
help="Increase logging verbosity")
@hidden_option("--debug", "-d", is_flag=True)
@hidden_option("--host", type=click.STRING)
@hidden_option("--port", type=click.INT)
@hidden_option("--logging", type=click.Path())
@ -76,7 +78,7 @@ def set_ctx_obj_option(ctx, param, value):
@hidden_option("--iknowwhatimdoing", "allow_root", is_flag=True)
@click.version_option(version=octoprint.__version__)
@click.pass_context
def octo(ctx, debug, host, port, basedir, configfile, logging, daemon, pid, allow_root):
def octo(ctx, debug, host, port, logging, daemon, pid, allow_root):
if ctx.invoked_subcommand is None:
# We have to support calling the octoprint command without any
@ -91,10 +93,10 @@ def octo(ctx, debug, host, port, basedir, configfile, logging, daemon, pid, allo
"\"octoprint daemon start|stop|restart\" from now on")
from octoprint.cli.server import daemon_command
ctx.invoke(daemon_command, pid=pid, daemon=daemon, allow_root=allow_root)
ctx.invoke(daemon_command, debug=debug, pid=pid, daemon=daemon, allow_root=allow_root)
else:
click.echo("Starting the server via \"octoprint\" is deprecated, "
"please use \"octoprint serve\" from now on.")
from octoprint.cli.server import serve_command
ctx.invoke(serve_command, host=host, port=port, logging=logging, allow_root=allow_root)
ctx.invoke(serve_command, debug=debug, host=host, port=port, logging=logging, allow_root=allow_root)

View file

@ -80,6 +80,6 @@ class OctoPrintPluginCommands(click.MultiCommand):
@pass_octoprint_ctx
def plugin_commands(obj):
"""Commands provided by plugins."""
logging.basicConfig(level=logging.DEBUG if obj.debug else logging.WARN)
logging.basicConfig(level=logging.DEBUG if obj.verbosity > 0 else logging.WARN)

View file

@ -11,7 +11,7 @@ import sys
from octoprint.cli import pass_octoprint_ctx
def run_server(basedir, configfile, host, port, debug, allow_root, logging_config):
def run_server(basedir, configfile, host, port, debug, allow_root, logging_config, verbosity):
from octoprint import init_platform, __display_version__
def log_startup(_):
@ -21,6 +21,7 @@ def run_server(basedir, configfile, host, port, debug, allow_root, logging_confi
configfile,
logging_file=logging_config,
debug=debug,
verbosity=verbosity,
uncaught_logger=__name__,
after_logging=log_startup)
@ -47,11 +48,13 @@ def server_commands(obj):
help="Specify the config file to use for configuring logging.")
@click.option("--iknowwhatimdoing", "allow_root", is_flag=True,
help="Allow OctoPrint to run as user root.")
@click.option("--debug", is_flag=True,
help="Enable debug mode")
@pass_octoprint_ctx
def serve_command(obj, host, port, logging, allow_root):
def serve_command(obj, host, port, logging, allow_root, debug):
"""Starts the OctoPrint server."""
run_server(obj.basedir, obj.configfile, host, port, obj.debug,
allow_root, logging)
run_server(obj.basedir, obj.configfile, host, port, debug,
allow_root, logging, obj.verbosity)
@server_commands.command(name="daemon")
@ -65,10 +68,12 @@ def serve_command(obj, host, port, logging, allow_root):
help="Specify the config file to use for configuring logging.")
@click.option("--iknowwhatimdoing", "allow_root", is_flag=True,
help="Allow OctoPrint to run as user root.")
@click.option("--debug", is_flag=True,
help="Enable debug mode")
@click.argument("command", type=click.Choice(["start", "stop", "restart"]),
metavar="start|stop|restart")
@pass_octoprint_ctx
def daemon_command(octoprint_ctx, pid, host, port, logging, allow_root, command):
def daemon_command(octoprint_ctx, pid, host, port, logging, allow_root, debug, command):
"""
Starts, stops or restarts in daemon mode.
@ -81,7 +86,7 @@ def daemon_command(octoprint_ctx, pid, host, port, logging, allow_root, command)
from octoprint.daemon import Daemon
class OctoPrintDaemon(Daemon):
def __init__(self, pidfile, basedir, configfile, host, port, debug, allow_root, logging_config):
def __init__(self, pidfile, basedir, configfile, host, port, debug, allow_root, logging_config, verbosity):
Daemon.__init__(self, pidfile)
self._basedir = basedir
@ -91,12 +96,13 @@ def daemon_command(octoprint_ctx, pid, host, port, logging, allow_root, command)
self._debug = debug
self._allow_root = allow_root
self._logging_config = logging_config
self._verbosity = verbosity
def run(self):
run_server(self._basedir, self._configfile, self._host, self._port, self._debug, self._allow_root, self._logging_config)
run_server(self._basedir, self._configfile, self._host, self._port, self._debug, self._allow_root, self._logging_config, self._verbosity)
octoprint_daemon = OctoPrintDaemon(pid, octoprint_ctx.basedir, octoprint_ctx.configfile,
host, port, octoprint_ctx.debug, allow_root, logging)
host, port, debug, allow_root, logging, octoprint_ctx.verbosity)
if command == "start":
octoprint_daemon.start()