Skip to content

Commit 0452fca

Browse files
committed
Make CLI logging more sane
Dragonfly's setup_log() function is now called by default and the -q/--quiet option only suppresses loader-related informational log messages.
1 parent a972c1f commit 0452fca

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

dragonfly/__main__.py

+22-16
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@
3030

3131
from dragonfly import get_engine, MimicFailure, EngineError
3232
from dragonfly.loader import CommandModule, CommandModuleDirectory
33+
from dragonfly.log import setup_log
3334

3435
LOG = logging.getLogger("command")
3536

3637

3738
#---------------------------------------------------------------------------
3839
# CLI helper functions.
3940

40-
def _set_logging_level(args):
41-
if args.quiet:
42-
args.log_level = "WARNING"
41+
def _setup_logging(args):
42+
# Use the specified logging defaults.
43+
if args.log_level == "DFLY":
44+
setup_log() # Use Dragonfly defaults.
45+
else:
46+
logging.basicConfig(level=getattr(logging, args.log_level))
4347

44-
# Set up logging with the specified logging level.
45-
logging.basicConfig(level=getattr(logging, args.log_level))
48+
# Suppress loader-related informational messages.
49+
if args.quiet:
50+
for logger_name in ("command", "module", "directory"):
51+
logging.getLogger(logger_name).setLevel(logging.WARNING)
4652

4753

4854
def _init_engine(args):
@@ -156,8 +162,8 @@ def _do_recognition(engine, args):
156162
# Main CLI functions.
157163

158164
def cli_cmd_test(args):
159-
# Set the logging level.
160-
_set_logging_level(args)
165+
# Setup logging.
166+
_setup_logging(args)
161167

162168
# Initialise the specified engine. Return early if there was an error.
163169
engine = _init_engine(args)
@@ -217,8 +223,8 @@ def cli_cmd_test(args):
217223

218224

219225
def cli_cmd_load(args):
220-
# Set the logging level.
221-
_set_logging_level(args)
226+
# Setup logging.
227+
_setup_logging(args)
222228

223229
# Initialise the specified engine. Return early if there was an error.
224230
engine = _init_engine(args)
@@ -245,8 +251,8 @@ def cli_cmd_load(args):
245251

246252

247253
def cli_cmd_load_directory(args):
248-
# Set the logging level.
249-
_set_logging_level(args)
254+
# Setup logging.
255+
_setup_logging(args)
250256

251257
# Initialise the specified engine. Return early if there was an error.
252258
engine = _init_engine(args)
@@ -400,14 +406,14 @@ def make_arg_parser():
400406
"reading input from stdin or recognizing speech."
401407
)
402408
log_level_argument = _build_argument(
403-
"-l", "--log-level", default="INFO",
404-
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
405-
help="Log level to use."
409+
"-l", "--log-level", default="DFLY",
410+
choices=["DFLY", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
411+
help="Logging defaults/level to use. By default, Dragonfly's "
412+
"setup_log() function is called."
406413
)
407414
quiet_argument = _build_argument(
408415
"-q", "--quiet", default=False, action="store_true",
409-
help="Equivalent to '-l WARNING' -- suppresses INFO and DEBUG "
410-
"logging."
416+
help="Suppress loader-related informational messages."
411417
)
412418

413419
# Create the parser for the "test" command.

dragonfly/log.py

+2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@
9292
"context.match": (_warning, _info),
9393
"rule": (_warning, _info),
9494
"clipboard": (_warning, _info),
95+
"command": (_info, _info),
9596
"config": (_warning, _info),
9697
"module": (_info, _info),
98+
"directory": (_info, _info),
9799
"monitor.init": (_warning, _info),
98100
"dfly.test": (_debug, _debug),
99101
"accessibility": (_info, _info),

0 commit comments

Comments
 (0)