1
votes

I'm trying to log info to Application Insights at the module level of code as oppsosed to the function level.

I can successfully log INFO, WARNING etc when the logger is called from within a function (in any module of my project), but not when called outside a function, (eg initializing a module, want to log some settings)

For example, when running my HttpTrigger app in azure functions, this works, and logs info to app insights:

import logging

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('this message is logged successfully')
    do_something()

while this does not work:

import logging
logging.info('this message isnt logged anywhere')

def main(req: func.HttpRequest) -> func.HttpResponse:
    do_something()

I've tried using named loggers, changing logging settings eg:

import logging
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
log.info('This still isnt logged')

def main(req: func.HttpRequest) -> func.HttpResponse:
    do_something()

and changing config in host.json:

{
  "version": "2.0",
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  },
  "logging": {
    "fileLoggingMode": "always",
    "logLevel": {
      "default": "Information",
      "Host.Results": "Information",
      "Function": "Information",
      "Host.Aggregator": "Information"
    }
  }
}

When running code locally, info is printed to stdout correctly everywhere.

I assume I must be misunderstanding exactlty how logging works in az. If anyone can fill in my gaps here for why the logging isn't working as I expect that would be appreciated!

1
This is not your fault. The linux function on azure does not open app logging by default. - Cindy Pau

1 Answers

1
votes

This is the cause:

enter image description here

Problem comes from the app logging is not open by default on azure. This is the solution:

Go to Platform features -> All Settings.

enter image description here

Go to search app service logs -> app logging -> set time for saving log -> save the edit.

enter image description here

Then everything will be ok.

This is my code:

import logging

import azure.functions as func

logger = logging.getLogger('name')
logger.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)
logger.addHandler(sh)
logger.info('This will work.')

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

Before setting, I face the same problem as yours. But after setting, I can get the INFO:

enter image description here

This is the offcial doc:

https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#enable-application-logging-linuxcontainer