Python App Insights logging via opencensus

Rajamannar A K 86 Reputation points
2022-02-25T09:58:32.147+00:00

I'm trying to log info and errors into app. insights using opencensus-ext-azure but I can't find the traces in the logs,and only some exceptions are getting logged. Checked after an hour as well couldn't find them. Below is the code which I'm using for logging.

   import logging  
   from opencensus.ext.azure.log_exporter import AzureLogHandler  
     
   def logger(ikey):  
       logs = logging.getLogger(__name__)  
       logs.addHandler(  
           AzureLogHandler(  
               connection_string='InstrumentationKey={}'.format(ikey)  
           )  
       )  
       return logs  
     
   ikey = 'ae52db35-5eeb-4eda-******'  
   logs = logger(ikey)  
     
   logs.info(f'APPINSIGHTS_INSTRUMENTATIONKEY is :{ikey}')  
   logs.info('Sample log message to check.')  
     
   logs.exception("RunTime error")  

but based on the documentation I should be able to see the info logs in traces but I don't, below is the snapshot of traces and exception logs.
177760-traces.png
177806-exceptions.png

when I check the vars(logs) i get the following

   {  
   'filters': [],  
    'name': '__main__',  
    'level': 0,  
    'parent': <RootLogger root (WARNING)>,  
    'propagate': True,  
    'handlers': [<AzureLogHandler (NOTSET)>],  
    'disabled': False,  
    '_cache': {},  
    'manager': <logging.Manager at 0x7f8b20239340>  
   }  
Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rahul Sarkar 6 Reputation points
    2022-04-04T02:37:32.533+00:00

    Hi !
    This is because of the following:

    The root logger is configured with the level of WARNING. That means any logs that you send that have less of a severity are ignored, and in turn, won't be sent to Azure Monitor. For more information, see documentation.

    Reference - https://learn.microsoft.com/en-us/azure/azure-monitor/app/opencensus-python

    To log info level logging you will need to explicitly set the logging level to Info, like :

    logger.setLevel(logging.INFO)

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.