Adding logs from AppDaemon to the main home_assistant.log

I’m reposting this over here because it seems like a more appropriate location. Original post: Combining AppDaemon log and main Hass log

My Hass.io setup is approaching 50% AppDaemon scripts, and I wish that it was easier to see AD logs. I love the real-time Log Viewer addon in Hass.io and use it all the time to monitor everything else, but the only entries that I see for AD are some calls back to the main Hass core. Logging into my Linux box to run a “tail -f” is a lot less friendly and practically unusable from a mobile device. I’d really like to be able to redirect all AppDaemon logs back to the main homeassistant.log, or be able to choose what to inject. If there’s no native capability, perhaps there could be an intermediary that reads from the AD log and injects into the Hass log.

The lack of hits in my search attempts makes me think that I’m the only one who wants this, but maybe I missed something. Is there anything like this?

In the absence of a better solution, I’m using this python script as a service called “log”:

component = data.get('component')
if not component:
  logger.error('No component provided')

message = data.get('message')
if not message:
  logger.error('No message provided')

complete_msg = "{}: {}".format(component, message)

logger.info(complete_msg)

And calling it like this in an AppDaemon app:

 self.call_service("python_script/log", component = "test",
   message = "Test Message")

Is there a better way? Or is this a bad idea for some reason?

1 Like

Hello @rccoleman,

I don’t use Hass.io, but your script is still not a bad idea with a slight modification.

If I were you, I will develop an app and use the listen_log api call. This way you can write your apps as normal, and in the log app, have it listen to AD’s logs and use your script to send the message to HA logs.

This method is quick (as its faster than writing to file. I use it for remote diagnostics over MQTT), and makes your apps still work normally added to the fact you will be able to receive normal AD logs.

One thing to note is that in the forthcoming AD 4.0, there is a breaking change to listen_log, so just have that in mind.

Example:

def initialize(self):
    self.listen_log(self.cb)

def cb(self, name, ts, level, message):
    msg = "{}: {}: {}: {}".format(ts, level, name, message)
    self.call_service("python_script/log",   message = msg)

So you can do away with the component side of things.

Regards

That’s a great idea! I looked pretty closely at the extensive API documentation and obviously missed listen_log(). I think that’ll be a great, general solution and better than what I was doing. I’ll keep an eye out for changes in AD 4.0.

Thanks!

Edit: And it’s working great! I had to play around with the logging levels to avoid getting two messages for every one (one about calling the script and the other printing the message), but I made it work.

Glad it worked for you :slight_smile: