Skip to content

Commit

Permalink
Replace internal logger with rich logger (#2929)
Browse files Browse the repository at this point in the history
- Drops colorama from being a direct dependency
- Removes our custom log handlers with Rich handler
- This change modifies molecule output in a signifiant way, including
  the fact that logging now goes to stderr instead of stdout.
- Marked as major change
  • Loading branch information
ssbarnea committed Oct 30, 2020
1 parent 2a0f4ef commit 475b78d
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 232 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ jobs:
- tox_env: lint
- tox_env: docs
- tox_env: py36
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=417
- tox_env: py37
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=417
- tox_env: py38
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=417
- tox_env: py39
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=417
- tox_env: py36-devel
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=417
- tox_env: py39-devel
PREFIX: PYTEST_REQPASS=427
PREFIX: PYTEST_REQPASS=417
- tox_env: packaging
- tox_env: eco
- tox_env: dockerfile
Expand Down
93 changes: 3 additions & 90 deletions lib/molecule/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import sys
from typing import Any

import colorama
from rich.logging import RichHandler


# Based on Ansible implementation
Expand Down Expand Up @@ -108,95 +108,8 @@ def get_logger(name=None) -> logging.Logger:
logger = logging.getLogger(name) # type: logging.Logger
logger.setLevel(logging.DEBUG)

logger.addHandler(_get_info_handler())
logger.addHandler(_get_out_handler())
logger.addHandler(_get_warn_handler())
logger.addHandler(_get_error_handler())
logger.addHandler(_get_critical_handler())
logger.addHandler(_get_success_handler())
handler = RichHandler(show_time=False, show_path=False, rich_tracebacks=True)
logger.addHandler(handler)
logger.propagate = False

return logger


def _get_info_handler() -> logging.StreamHandler:
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.INFO)
handler.addFilter(LogFilter(logging.INFO)) # type: ignore
handler.setFormatter(
TrailingNewlineFormatter("--> {}".format(cyan_text("%(message)s")))
)

return handler


def _get_out_handler() -> logging.StreamHandler:
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(OUT)
handler.addFilter(LogFilter(OUT)) # type: ignore
handler.setFormatter(TrailingNewlineFormatter(" %(message)s"))

return handler


def _get_warn_handler() -> logging.StreamHandler:
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.WARN)
handler.addFilter(LogFilter(logging.WARN)) # type: ignore
handler.setFormatter(TrailingNewlineFormatter(yellow_text("%(message)s")))

return handler


def _get_error_handler() -> logging.StreamHandler:
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.ERROR)
handler.addFilter(LogFilter(logging.ERROR)) # type: ignore
handler.setFormatter(TrailingNewlineFormatter(red_text("%(message)s")))

return handler


def _get_critical_handler() -> logging.StreamHandler:
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.CRITICAL)
handler.addFilter(LogFilter(logging.CRITICAL)) # type: ignore
handler.setFormatter(TrailingNewlineFormatter(red_text("ERROR: %(message)s")))

return handler


def _get_success_handler() -> logging.StreamHandler:
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(SUCCESS)
handler.addFilter(LogFilter(SUCCESS)) # type: ignore
handler.setFormatter(TrailingNewlineFormatter(green_text("%(message)s")))

return handler


def red_text(msg) -> str:
"""Add red markers."""
return color_text(colorama.Fore.RED, msg)


def yellow_text(msg) -> str:
"""Add yellow markers."""
return color_text(colorama.Fore.YELLOW, msg)


def green_text(msg) -> str:
"""Add green markers."""
return color_text(colorama.Fore.GREEN, msg)


def cyan_text(msg) -> str:
"""Add cyan markers."""
return color_text(colorama.Fore.CYAN, msg)


def color_text(color, msg) -> str:
"""Add color markers."""
if should_do_markup():
return "{}{}{}".format(color, msg, colorama.Style.RESET_ALL)
return msg
3 changes: 0 additions & 3 deletions lib/molecule/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import click
import click_completion
import colorama
import pkg_resources
from click_help_colors import _colorize

Expand All @@ -33,11 +32,9 @@
from molecule.api import drivers
from molecule.command.base import click_group_ex
from molecule.config import MOLECULE_DEBUG, ansible_version
from molecule.logger import should_do_markup
from molecule.util import lookup_config_file

click_completion.init()
colorama.init(autoreset=True, strip=not should_do_markup())

LOCAL_CONFIG_SEARCH = ".config/molecule/config.yml"
LOCAL_CONFIG = lookup_config_file(LOCAL_CONFIG_SEARCH)
Expand Down
129 changes: 0 additions & 129 deletions lib/molecule/test/unit/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,139 +21,10 @@
from __future__ import print_function

import logging
import sys

import colorama

from molecule import logger


def test_info(capsys, monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
log = logger.get_logger(__name__)
log.info("foo")
stdout, _ = capsys.readouterr()

print(
"--> {}{}{}".format(
colorama.Fore.CYAN, "foo".rstrip(), colorama.Style.RESET_ALL
)
)
x, _ = capsys.readouterr()

assert x == stdout
monkeypatch.setenv("PY_COLORS", "0")


def test_out(capsys):
log = logger.get_logger(__name__)
log.out("foo")

stdout, _ = capsys.readouterr()

assert " foo\n" == stdout


def test_warn(capsys, monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
log = logger.get_logger(__name__)
log.warning("foo")

stdout, _ = capsys.readouterr()

print(
"{}{}{}".format(colorama.Fore.YELLOW, "foo".rstrip(), colorama.Style.RESET_ALL)
)
x, _ = capsys.readouterr()

assert x == stdout
monkeypatch.setenv("PY_COLORS", "0")


def test_error(capsys, monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
log = logger.get_logger(__name__)
log.error("foo")

_, stderr = capsys.readouterr()

print(
"{}{}{}".format(colorama.Fore.RED, "foo".rstrip(), colorama.Style.RESET_ALL),
file=sys.stderr,
)
_, x = capsys.readouterr()

assert x in stderr
monkeypatch.setenv("PY_COLORS", "0")


def test_critical(capsys, monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
log = logger.get_logger(__name__)
log.critical("foo")

_, stderr = capsys.readouterr()

print(
"{}ERROR: {}{}".format(
colorama.Fore.RED, "foo".rstrip(), colorama.Style.RESET_ALL
),
file=sys.stderr,
)
_, x = capsys.readouterr()

assert x in stderr
monkeypatch.setenv("PY_COLORS", "0")


def test_success(capsys, monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
log = logger.get_logger(__name__)
log.success("foo")

stdout, _ = capsys.readouterr()

print(
"{}{}{}".format(colorama.Fore.GREEN, "foo".rstrip(), colorama.Style.RESET_ALL)
)
x, _ = capsys.readouterr()

assert x == stdout
monkeypatch.setenv("PY_COLORS", "0")


def test_red_text(monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
x = "{}{}{}".format(colorama.Fore.RED, "foo", colorama.Style.RESET_ALL)

assert x == logger.red_text("foo")
monkeypatch.setenv("PY_COLORS", "0")


def test_yellow_text(monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
x = "{}{}{}".format(colorama.Fore.YELLOW, "foo", colorama.Style.RESET_ALL)

assert x == logger.yellow_text("foo")
monkeypatch.setenv("PY_COLORS", "0")


def test_green_text(monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
x = "{}{}{}".format(colorama.Fore.GREEN, "foo", colorama.Style.RESET_ALL)

assert x == logger.green_text("foo")
monkeypatch.setenv("PY_COLORS", "0")


def test_cyan_text(monkeypatch):
monkeypatch.setenv("PY_COLORS", "1")
x = "{}{}{}".format(colorama.Fore.CYAN, "foo", colorama.Style.RESET_ALL)

assert x == logger.cyan_text("foo")
monkeypatch.setenv("PY_COLORS", "0")


def test_markup_detection_pycolors0(monkeypatch):
monkeypatch.setenv("PY_COLORS", "0")
assert not logger.should_do_markup()
Expand Down
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ ignore_missing_imports = True
[mypy-click_help_colors]
ignore_missing_imports = True

[mypy-colorama]
ignore_missing_imports = True

[mypy-cookiecutter.*]
ignore_missing_imports = True

Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ install_requires =
click >= 7.0
click-completion >= 0.5.1
click-help-colors >= 0.6
colorama >= 0.3.9
cookiecutter >= 1.6.0, != 1.7.1
Jinja2 >= 2.10.1
packaging
Expand Down

0 comments on commit 475b78d

Please sign in to comment.