Skip to content

Commit

Permalink
Removed custom logger
Browse files Browse the repository at this point in the history
Molecule no longer uses a custom logger class and we make use of the
standard python logger class.

This change replaces logger.success() and logger.out() with
logger.info() calls.
  • Loading branch information
ssbarnea committed Nov 18, 2020
1 parent a438300 commit cce2151
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/molecule/command/idempotence.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def execute(self):
idempotent = self._is_idempotent(output)
if idempotent:
msg = "Idempotence completed successfully."
LOG.success(msg)
LOG.info(msg)
else:
msg = (
"Idempotence test failed because of the following tasks:\n" u"{}"
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/command/init/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def execute(self):

role_directory = os.path.join(role_directory, role_name)
msg = "Initialized role in {} successfully.".format(role_directory)
LOG.success(msg)
LOG.info(msg)


@command_base.click_command_ex()
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/command/init/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def execute(self):

role_directory = os.path.join(role_directory, role_name)
msg = "Initialized scenario in {} successfully.".format(scenario_directory)
LOG.success(msg)
LOG.info(msg)


def _role_exists(ctx, param, value): # pragma: no cover
Expand Down
4 changes: 2 additions & 2 deletions src/molecule/dependency/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def execute_with_retries(self):
# print(555, self._sh_command)
util.run_command(self._sh_command, debug=self._config.debug)
msg = "Dependency completed successfully."
LOG.success(msg)
LOG.info(msg)
return
except Exception:
pass
Expand All @@ -72,7 +72,7 @@ def execute_with_retries(self):
try:
util.run_command(self._sh_command, debug=self._config.debug)
msg = "Dependency completed successfully."
LOG.success(msg)
LOG.info(msg)
return
except Exception as _exception:
exception = _exception
Expand Down
28 changes: 0 additions & 28 deletions src/molecule/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
from enrich.console import Console
from enrich.logging import RichHandler

from molecule.console import console
from molecule.text import chomp

SUCCESS = 100
OUT = 101

Expand All @@ -45,29 +42,6 @@ def filter(self, logRecord): # pragma: no cover
return logRecord.levelno <= self.__level


class CustomLogger(logging.getLoggerClass()): # type: ignore # see https://sam.hooke.me/note/2020/03/mypy-and-verbose-logging/
"""
A custom logging class which adds additional methods to the logger.
These methods serve as syntactic sugar for formatting log messages.
"""

def __init__(self, name, level=logging.NOTSET):
"""Construct CustomLogger."""
super(CustomLogger, self).__init__(name, level=level)
logging.addLevelName(SUCCESS, "SUCCESS")
logging.addLevelName(OUT, "OUT")

def success(self, msg, *args, **kwargs):
if self.isEnabledFor(SUCCESS):
self._log(SUCCESS, msg, args, **kwargs)

def out(self, msg, *args, **kwargs):
msg = chomp(msg)
if self.isEnabledFor(OUT):
console.print(msg, args, **kwargs)


class TrailingNewlineFormatter(logging.Formatter):
"""A custom logging formatter which removes additional newlines from messages."""

Expand All @@ -86,8 +60,6 @@ def get_logger(name=None) -> logging.Logger:
name, ``__name__``.
:return: logger object
"""
logging.setLoggerClass(CustomLogger)

logger = logging.getLogger(name) # type: logging.Logger
logger.setLevel(logging.DEBUG)

Expand Down
9 changes: 1 addition & 8 deletions src/molecule/provisioner/ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
"""Ansible-Playbook Provisioner Module."""

from molecule import logger, util

LOG = logger.get_logger(__name__)
Expand All @@ -27,24 +26,18 @@
class AnsiblePlaybook(object):
"""Privisioner Playbook."""

def __init__(self, playbook, config, out=LOG.out, err=LOG.error):
def __init__(self, playbook, config):
"""
Set up the requirements to execute ``ansible-playbook`` and returns \
None.
:param playbook: A string containing the path to the playbook.
:param config: An instance of a Molecule config.
:param out: An optional function to process STDOUT for underlying
:func:``sh`` call.
:param err: An optional function to process STDERR for underlying
:func:``sh`` call.
:returns: None
"""
self._ansible_command = None
self._playbook = playbook
self._config = config
self._out = out
self._err = err
self._cli = {}
self._env = self._config.provisioner.env

Expand Down
2 changes: 1 addition & 1 deletion src/molecule/test/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def with_scenario(request, scenario_to_test, driver_name, scenario_name, skip_te
yield
if scenario_name:
msg = "CLEANUP: Destroying instances for all scenario(s)"
LOG.out(msg)
LOG.info(msg)
cmd = ["molecule", "destroy", "--driver-name", driver_name, "--all"]
assert run_command(cmd).returncode == 0

Expand Down
10 changes: 0 additions & 10 deletions src/molecule/test/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,6 @@ def patched_logger_debug(mocker):
return mocker.patch("logging.Logger.debug")


@pytest.fixture
def patched_logger_out(mocker):
return mocker.patch("molecule.logger.CustomLogger.out")


@pytest.fixture
def patched_logger_warning(mocker):
return mocker.patch("logging.Logger.warning")
Expand All @@ -187,11 +182,6 @@ def patched_logger_critical(mocker):
return mocker.patch("logging.Logger.critical")


@pytest.fixture
def patched_logger_success(mocker):
return mocker.patch("molecule.logger.CustomLogger.success")


@pytest.fixture
def patched_run_command(mocker):
m = mocker.patch("molecule.util.run_command")
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/verifier/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def execute(self):
self._config.provisioner.verify()

msg = "Verifier completed successfully."
log.success(msg)
log.info(msg)

def schema(self):
return {
Expand Down
2 changes: 1 addition & 1 deletion src/molecule/verifier/testinfra.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def execute(self):
result = util.run_command(self._testinfra_command, debug=self._config.debug)
if result.returncode == 0:
msg = "Verifier completed successfully."
LOG.success(msg)
LOG.info(msg)
else:
util.sysexit(result.returncode)

Expand Down

0 comments on commit cce2151

Please sign in to comment.