From 0864f4e69248101f468e2d248c1688c0086c20d1 Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Thu, 6 Oct 2016 14:14:43 -0700 Subject: [PATCH] Make --quiet reduce the logging level (#3593) * reduce logging level and ignore verbose flags in quiet mode * Simplify setup_logging parameters The extra parameters were there in the past when the letsencrypt-renewer was a separate executable that also used this function. This is cruft that can be removed. * Add basic tests for setup_logging --- certbot/constants.py | 3 +++ certbot/main.py | 20 +++++++++++++------ certbot/tests/main_test.py | 40 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/certbot/constants.py b/certbot/constants.py index ae998e15a..117301380 100644 --- a/certbot/constants.py +++ b/certbot/constants.py @@ -37,6 +37,9 @@ STAGING_URI = "https://acme-staging.api.letsencrypt.org/directory" """Defaults for CLI flags and `.IConfig` attributes.""" +QUIET_LOGGING_LEVEL = logging.WARNING +"""Logging level to use in quiet mode.""" + RENEWER_DEFAULTS = dict( renewer_enabled="yes", renew_before_expiry="30 days", diff --git a/certbot/main.py b/certbot/main.py index dd497d14d..35008fd62 100644 --- a/certbot/main.py +++ b/certbot/main.py @@ -625,14 +625,22 @@ def _cli_log_handler(config, level, fmt): return handler -def setup_logging(config, cli_handler_factory, logfile): - """Setup logging.""" - file_fmt = "%(asctime)s:%(levelname)s:%(name)s:%(message)s" +def setup_logging(config): + """Sets up logging to logfiles and the terminal. + + :param certbot.interface.IConfig config: Configuration object + + """ cli_fmt = "%(message)s" - level = -config.verbose_count * 10 + file_fmt = "%(asctime)s:%(levelname)s:%(name)s:%(message)s" + logfile = "letsencrypt.log" + if config.quiet: + level = constants.QUIET_LOGGING_LEVEL + else: + level = -config.verbose_count * 10 file_handler, log_file_path = setup_log_file_handler( config, logfile=logfile, fmt=file_fmt) - cli_handler = cli_handler_factory(config, level, cli_fmt) + cli_handler = _cli_log_handler(config, level, cli_fmt) # TODO: use fileConfig? @@ -738,7 +746,7 @@ def main(cli_args=sys.argv[1:]): os.geteuid(), config.strict_permissions) # Setup logging ASAP, otherwise "No handlers could be found for # logger ..." TODO: this should be done before plugins discovery - setup_logging(config, _cli_log_handler, logfile='letsencrypt.log') + setup_logging(config) cli.possible_deprecation_warning(config) logger.debug("certbot version: %s", certbot.__version__) diff --git a/certbot/tests/main_test.py b/certbot/tests/main_test.py index fab1065c5..f7a6c5896 100644 --- a/certbot/tests/main_test.py +++ b/certbot/tests/main_test.py @@ -7,8 +7,11 @@ import unittest import mock from certbot import cli +from certbot import colored_logging +from certbot import constants from certbot import configuration from certbot import errors +from certbot import log from certbot.plugins import disco as plugins_disco class MainTest(unittest.TestCase): @@ -80,6 +83,43 @@ class SetupLogFileHandlerTest(unittest.TestCase): self.config, "test.log", "%s") +class SetupLoggingTest(unittest.TestCase): + """Tests for certbot.main.setup_logging.""" + + def setUp(self): + self.config = mock.Mock( + logs_dir=tempfile.mkdtemp(), + noninteractive_mode=False, quiet=False, text_mode=False, + verbose_count=constants.CLI_DEFAULTS['verbose_count']) + + def tearDown(self): + shutil.rmtree(self.config.logs_dir) + + @classmethod + def _call(cls, *args, **kwargs): + from certbot.main import setup_logging + return setup_logging(*args, **kwargs) + + @mock.patch('certbot.main.logging.getLogger') + def test_defaults(self, mock_get_logger): + self._call(self.config) + + cli_handler = mock_get_logger().addHandler.call_args_list[0][0][0] + self.assertEqual(cli_handler.level, -self.config.verbose_count * 10) + self.assertTrue( + isinstance(cli_handler, log.DialogHandler)) + + @mock.patch('certbot.main.logging.getLogger') + def test_quiet_mode(self, mock_get_logger): + self.config.quiet = self.config.noninteractive_mode = True + self._call(self.config) + + cli_handler = mock_get_logger().addHandler.call_args_list[0][0][0] + self.assertEqual(cli_handler.level, constants.QUIET_LOGGING_LEVEL) + self.assertTrue( + isinstance(cli_handler, colored_logging.StreamHandler)) + + class MakeOrVerifyCoreDirTest(unittest.TestCase): """Tests for certbot.main.make_or_verify_core_dir."""