Skip to content

logging: Make it possible to add cli colors to custom log levels #8804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 9, 2021

Conversation

terjr
Copy link
Contributor

@terjr terjr commented Jun 28, 2021

This PR adds the possibility to color custom log levels on the CLI:
pride

Not sure how this can be unit tested or where to add documentation. Please comment.

Closes #8803.

@Zac-HD Zac-HD added plugin: logging related to the logging builtin plugin topic: reporting related to terminal output and user-facing messages and errors labels Jul 4, 2021
@nicoddemus
Copy link
Member

Thanks @terjr for the contribution!

Sorry for the delay here, seems most of the contributors are busy (including me).

This might be a bit controversial because usually we don't recommend users to explicitly configure internal plugins like that, but I'm totally fine with this approach if we mark it as experimental.

Other than that it would be nice to update to update logging.rst with a section demonstrating how to customize logging colors. 👍

@terjr
Copy link
Contributor Author

terjr commented Aug 4, 2021

@nicoddemus Thanks for having a look!

I just wrote some documentation and marked the feature as experimental.

for level, color_opts in self.LOGLEVEL_COLOROPTS.items():
self.add_color_level(level, *color_opts)

def add_color_level(self, level, *color_opts):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do anything about the style color options here

every time we add a terminalwriter style into the api we lock this broken api i want to remove more into pytest

how about migrating from pylib color options to colorama options?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? The TerminalWriter color codes are just ANSI escape codes that shouldn't change too often...

@final
class TerminalWriter:
    _esctable = dict(
        black=30,
        red=31,
        green=32,
        yellow=33,
        blue=34,
        purple=35,
        cyan=36,
        white=37,
        Black=40,
        Red=41,
        Green=42,
        Yellow=43,
        Blue=44,
        Purple=45,
        Cyan=46,
        White=47,
        bold=1,
        light=2,
        blink=5,
        invert=7,
    )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i want to eventually remove the terminalwriter from pytest

which is why id like to avoid putting its apis into other places when possible,
unfortunately we have no alternative ready right now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, any timeline on when an alternative will be ready?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, any timeline on when an alternative will be ready?

Unfortunately we don't have a timeline.

I understand @RonnyPfannschmidt's feelings here, the terminal writer markup codes are something we want to move away from, but there are already other APIs which use them, and we don't have a clear clear definition of what we want to use instead.

Regardless, even if in the future we get around to replace the markup codes with something else, the same deprecation/upgrade path that we will need to use in other parts of pytest that use markup codes, would be applied here as well.

So I don't think this should be a blocker for the PR.

how about migrating from pylib color options to colorama options?

That's not a good idea either, because we are then introducing an alternative way to declare markup options which is already deprecated from the start, because I'm sure we don't want to lock the markup definition to colorama either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will have to agree, as i im unable commit to a timeline for anything alternative
its not right to stop this pr from going in just cause its using the api that is actually available

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @terjr!

I left some minor requests for changes related to documentation mostly.

for level, color_opts in self.LOGLEVEL_COLOROPTS.items():
self.add_color_level(level, *color_opts)

def add_color_level(self, level, *color_opts):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, any timeline on when an alternative will be ready?

Unfortunately we don't have a timeline.

I understand @RonnyPfannschmidt's feelings here, the terminal writer markup codes are something we want to move away from, but there are already other APIs which use them, and we don't have a clear clear definition of what we want to use instead.

Regardless, even if in the future we get around to replace the markup codes with something else, the same deprecation/upgrade path that we will need to use in other parts of pytest that use markup codes, would be applied here as well.

So I don't think this should be a blocker for the PR.

how about migrating from pylib color options to colorama options?

That's not a good idea either, because we are then introducing an alternative way to declare markup options which is already deprecated from the start, because I'm sure we don't want to lock the markup definition to colorama either.

def add_color_level(self, level, *color_opts):
"""Add or update color opts for a log level.

.. warning::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to describe color_opts, and even provide an example.

terjr and others added 4 commits August 9, 2021 08:24

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Co-authored-by: Bruno Oliveira <[email protected]>
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @terjr, this looks great!

Let's wait for @RonnyPfannschmidt to give it another look before merging. 👍

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@nicoddemus nicoddemus enabled auto-merge (squash) August 9, 2021 13:32
@nicoddemus nicoddemus disabled auto-merge August 9, 2021 13:33
@nicoddemus nicoddemus changed the title logging: Make it possible to add cli colors to custom log levels logging: Make it possible to add cli colors to custom log levels Aug 9, 2021
@nicoddemus nicoddemus enabled auto-merge (squash) August 9, 2021 13:33
@nicoddemus nicoddemus merged commit 2439729 into pytest-dev:main Aug 9, 2021
RonnyPfannschmidt pushed a commit to RonnyPfannschmidt/pytest that referenced this pull request Oct 1, 2021
Closes pytest-dev#8803
PR pytest-dev#8804

Co-authored-by: Bruno Oliveira <[email protected]>
Co-authored-by: Terje Runde <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: logging related to the logging builtin plugin topic: reporting related to terminal output and user-facing messages and errors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Configurable log level colors and coloring of custom log levels
4 participants