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

Question: how to use colors properly with custom levels/tags? #257

Open
vonpupp opened this issue Mar 23, 2018 · 3 comments
Open

Question: how to use colors properly with custom levels/tags? #257

vonpupp opened this issue Mar 23, 2018 · 3 comments

Comments

@vonpupp
Copy link

vonpupp commented Mar 23, 2018

Hello,

I am trying to have custom colors for custom message types. For instance:
Tag: event1 -> red
Tag: event2 -> blue
Tag: event3 -> white

I first tried to have a custom ColorizedStderrHandler class as shown in this example. However I later noticed on the core features that: custom log levels are not supported, instead we strongly recommend using logging subclasses or log processors that inject tagged information into the log record for this purpose.

I am not sure how to do that, is there any example on how to get different colors with something that emulates custom levels i.e. tags?

Thank you very much.

@vmalloc
Copy link
Collaborator

vmalloc commented Mar 24, 2018

It sounds like you will need to create a custom handler to do that. Once a custom handler is written, you can easily control its emit method to customize the color based on level, channel (a.k.a. logger name) or other information, such as the extra information passed in the extra argument to the logging functions...

@vonpupp
Copy link
Author

vonpupp commented Mar 26, 2018

Thank you for your time and answer. I still don't fully get how the parts play with each other.

I tried this using a TaggingLogger as the base logger:

    log = TaggingLogger(
        'Test',
        [
            'info',
            'warning',
            'debug',
            'event1',
            'event2'
        ]
    )
    my_handler = MyLogHandler({})
    log.handlers.append(my_handler)
    log.event1('Test event1 tag')
    log.event2('Test event2 tag')

When I try write the handler, I am using the TaggingHandler with the ColorizingStreamHandlerMixin to have both functionalities.

class MyLogHandler(ColorizingStreamHandlerMixin, TaggingHandler):

    def get_color(self, record):
        import ipdb; ipdb.set_trace() # BREAKPOINT

    def emit(self, record):
        import ipdb; ipdb.set_trace() # BREAKPOINT
        if 'event1' in record.extra.get('tags', ()):
            record.extra.update({'color': 'red'})

The emit is indeed being called. My initial thought was to add a color key to the extra dict depending on the current tags, so that the get_color would use that color. However, the get_color never gets called.

Also, I am confused on how to use the log method itself with the TaggingLogger, since tags seem to replace the levels, and I would like to have both, I would like something like this:

log.info(['event1'], 'Event1')                # Color red
log.info(['event2'], 'Event2')                # Color green
log.warning(['event1'], 'Warning event1')     # Color blue (any event)
log.warning(['event2'], 'Warning event2')     # Color blue (any event)
log.debug(['event1'], 'Debug event1')         # Color cyan (only event 1)
log.debug(['event2'], 'Debug event2')         # Color magenta (only event 2)

I am not sure how to achieve this with a TaggingLogger.

Thank you once again.

@vmalloc
Copy link
Collaborator

vmalloc commented Mar 31, 2018

Your code above doesn't seem to be using Logbook loggers... the "handlers.append" approach is the way the native logging library is usually used...

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

2 participants