Skip to content

Add an enumeration for log levels? #312

@mikeckennedy

Description

@mikeckennedy

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions