Skip to content
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

Add an enumeration for log levels? #312

Open
mikeckennedy opened this issue Jun 9, 2020 · 0 comments
Open

Add an enumeration for log levels? #312

mikeckennedy opened this issue Jun 9, 2020 · 0 comments

Comments

@mikeckennedy
Copy link

Hi folks. I'm a big fan of this library! But I always end up creating something to store the log levels in a reliable way. I'd like to write code like this, using a clear class offering autocomplete (potentially docstrings, etc):

Screen Shot 2020-06-08 at 8 53 45 PM

Then we can use it like:

logbook.StreamHandler(sys.stdout, level=LogLevel.debug).push_application()

And the LogLevel class is a collection like this with a nice parse option for verification and canonicalization (e.g. "error " --> "ERROR"):

class LogLevel:
    debug = "DEBUG"
    info = "INFO"
    notice = "NOTICE"
    warning = "WARNING"
    error = "ERROR"
    critical = "CRITICAL"

    @staticmethod
    def parse(level_text: str) -> str:
        if not level_text:
            raise Exception("Level text must be specified")

        level_text = level_text.upper().strip()

        if level_text == LogLevel.debug:
            return LogLevel.debug
        elif level_text == LogLevel.info:
            return LogLevel.info
        elif level_text == LogLevel.notice:
            return LogLevel.notice
        elif level_text == LogLevel.warning:
            return LogLevel.warning
        elif level_text == LogLevel.error:
            return LogLevel.error
        elif level_text == LogLevel.critical:
            return LogLevel.critical
        else:
            raise Exception(f"The level text {level_text} is not a supported log level.")

Would you be willing to adopt something like this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant