Open Hubro opened 7 months ago
Not sure what's going on with the logging callbacks, but generally functions like this (sending the logs somewhere) happen in Python with logging handlers.
This is an example of what I use to send the logs from a single app to Loki.
import asyncio
import logging.config
from logging import Handler, LogRecord
import aiohttp
from appdaemon.adapi import ADAPI
class LokiHandler(Handler):
loop: asyncio.BaseEventLoop
loki_url: str
level: int | str = 0
def __init__(self, loop: asyncio.BaseEventLoop, loki_url: str, level: int | str = 0) -> None:
self.loop: asyncio.BaseEventLoop = loop
self.loki_url: str = loki_url
super().__init__(level)
def emit(self, record: LogRecord) -> None:
self.loop.create_task(self.send_to_loki(record))
async def send_to_loki(self, record: LogRecord):
message = self.format(record)
ns = round(record.created * 1_000_000_000)
# https://grafana.com/docs/loki/latest/reference/loki-http-api/#ingest-logs
payload = {
'streams': [
{
'stream': {'appname': record.appname, 'level': record.levelname},
'values': [[str(ns), message]],
}
]
}
async with aiohttp.ClientSession() as session:
await session.post(self.loki_url, json=payload, timeout=3)
class LokiLogger(ADAPI):
async def initialize(self):
log_cfg = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'loki': {
'()': 'loki.LokiHandler',
'loop': self.AD.loop,
'loki_url': self.args['loki_url'],
}
},
'loggers': {self.logger.name: {'handlers': ['loki'], 'level': 'DEBUG'}},
}
# Use dictConfig in order to replace the logging configuration every time the app is reloaded
# https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schema
logging.config.dictConfig(log_cfg)
self.log('Logging from LokiLogger')
What happened?
I'm using
listen_log
to ship all my log messages to Loki. Since I have practically infinite storage and easy log filtering, I want to always enable debug logging.However, I can't get
listen_log
to receive any DEBUG messages. Here's myExternalLogging
app:I have
log_level: NOTSET
in the config of all my apps, and I'm also runningself.set_log_level("NOTSET")
in all my app initialize functions, just to be 100% sure. I've also tried "DEBUG" in place of "NOTSET" in all these places.The
print
statement in myon_log
callback shows up in the "Log" tab of the AppDaemon plugin, and it shows that the callback is only receiving INFO, WARNING, ERROR and CRITICAL log messages. It's not receiving any DEBUG or NOTSET log messages.Is this a bug or am I missing something?
Version
4.4.2
Installation type
Home Assistant add-on
Relevant log output
Relevant code in the app or config file that caused the issue
No response
Anything else?
No response