2

I am having significant difficulty with emitting logs to the Azure Application Insights. When the AzureLogHandler is initialised and added to a child logger within app.py, it works fine. However, the issue begins outside of app.py when I go to create a new logger instance, the logs are not emitted to Azure. Sample:

# app.py
logger = logging.getLogger(__name__)
logger.addHandler(AzureLogHandler(connection_string=""))

logger.info("This is emitted")


# main.py
logger = logging.getLogger(__name__)
logger.addHandler(AzureLogHandler(connection_string=""))

logger.info("This is NOT emitted")

The only way I have managed to get it to work is by importing the logger instance I created in app.py which is suboptimal:

# sub-optimal - does not permit me to create a log hierarchy
# main.py
from app.py import logger

logger.info("This works but restricts me")

Ideally, I would like to implement opencensus AzureLogHandler with as little interference with the project as possible, for example adding the AzureLogHandler to the root logger at the start and then deriving all child loggers without need to attach the handler seperately:

# app.py
logging.basicConfig(handlers=[AzurelogHandler(connection_string=""))
logger = logging.getLogger(__name__)
logger.debug("so simple!")
# main.py
logger = logging.getLogger(__name__)
logger.debug("so simple!")

Note: The solution must work with Asyncronous FastAPI. This seems to complicate things as I have implemented this for Flask and other services just fine.

Thanks!

RoryP
  • 21
  • 2
  • You can refer to [Logging to Application Insights](https://github.com/tiangolo/fastapi/issues/433#issuecomment-814959428) – Ecstasy Jan 06 '22 at 09:25
  • @RoryP did you manage to get this to work? – djangonaut Jul 21 '22 at 15:00
  • @djangonaut yes although the amount of boilerplate code required in the end is insane. Also has to succumb to deriving every logger from the root logger using: ‘logger = logging.getLogger(“rootlogger.” + __name__)’ . Happy to share the code if you need but it’s definitely not optimised. – RoryP Jul 22 '22 at 16:12
  • @RoryP if it is not a hassle for you, then I would certainly like to see your logging setup :) – djangonaut Jul 22 '22 at 16:28

0 Answers0