Add-on development: how log errors/warnings from Python?

I’m working on a simple add-on in Python and I would like to print errors and warnings to the add-on log that is visible from the HA web interface. I would like them to be color-coded and timestamped.

From a Bash script (with the included bashio) this is easy:

bashio::log.error "This is an error!"
bashio::log.notice "Just a notice"

But I can’t figure out how to do this from my Python application. I could just print() stuff and while that appears in the log in the web interface it’s not color-coded or timestamped. Does any have some insight in how to do this?

Addon logs are just docker logs. So anything you dump to stdout and stderr becomes visible there. Best practice is to use the python logging library like normal.

1 Like

Ah I get it now, bashio just has a function to add timestamp/color to the message and then echo’s it. I see the coloredlogs pip package basically does the same thing so I can just use that to add timestamps and color to the default Python logging library. :slight_smile:

1 Like