How can I create a custom log formatter with ops?

I’m looking for help to create a custom log formatter (logging) with ops which I so far have failed to get working.

I’m following an example (failing) here:

But when I’m adding the code to my charm, only a few of my logs get properly formatted. I can’t understand what I’m doing wrong.

I remember I had some conversation about this with @jameinel a long time ago, but I can’t find it in the forum search.

Why I like to be able to color my logs is that it makes it alot easier to find the log messages related to my code within the onslaught of others.

Any help on this topic appreciated.

instead of coloring them, I’d recommend adding a tag/prefix that you can grep to filter out the logs you’re interested in. After that you could pipe the logs through lolcat if you want colors :stuck_out_tongue:

Also, I’m not sure what happens to the logs when they get forwarded to the controller, maybe the escape sequences are stripped at some point?

A tag is good enough. But how would you implement it?

logger = logging.getLogger(__name__)

    handler = next(filter(lambda h: isinstance(h, JujuLogHandler), logger.handlers))
except StopIteration:
    # we are not being executed by ops.main
    handler = logging.StreamHandler()

logger.error("hello world")

1 Like

Yeah, I think trying to colour them before logging (and sending to the Juju controller) doesn’t sound right. They should be coloured as a UI thing at the time of display. That said, I’m not sure what’s breaking for you now – it could be there’s a maximum line length, or that the control characters are being stripped as Pietro suggested. Using a custom prefix as he showed seems like a reasonable way to do it. Could you also go on the module, for example __main__ in the example above?