How to control logging for azure function app

Ashwin Venkatesha 165 Reputation points
2024-04-23T06:09:40.78+00:00

I have a function app with 3 triggers. My objective is to log custom logs that I have added as part of this azure function app, like

logging.info("some custom log")

In host.json, I have added the below

    "logLevel": {
      "logging:logLevel:Microsoft": "None",
      "logging:logLevel:Worker": "None",
      "AzureFunctionsJobHost:logging:logLevel:default": "None",
      "AzureFunctionsJobHost:logging:logLevel:Host.Function.Console": "Information",
      "Function.TimedSQSFunctionApp": "Information",
      "Function.QueueTriggerFuncApp": "Information",
      "Function.QueueManagerFunctionApp": "Information" ,
      "Function.QueueManagerFunctionApp.User": "Information" 
    }

But, I still do not see "some custom log" being outputted when function app is run.
I am running the function app from vs code.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,386 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kerry Bills 6 Reputation points
    2024-04-23T07:02:54.6466667+00:00

    The issue with your Azure Functions logging configuration seems to stem from how the log levels are set up in your host.json file, especially considering the identifiers you're using to specify log levels for your functions. To resolve this and ensure that your custom log statements are being outputted as expected, follow these steps:

    Ensure Correct Category Names for Logging: The category names in the logLevel section must match the categories used in your function code. Often, these should be set to the name of the function itself or the log categories that you define within your code.

    Modify host.json Configuration: Update your host.json to make sure the log level settings are correctly targeted. Here’s a simplified way to configure it, assuming your functions are named as such in your logging configuration:

    { 
    "version": "2.0", 
    "logging": 
    { "logLevel": { "default": "None", // Set default log level to None to minimize internal logs "Function": "Information", // Enable Information level logs for all functions generally "Function.TimedSQSFunctionApp": "Information", "Function.QueueTriggerFuncApp": "Information", "Function.QueueManagerFunctionApp": "Information", "Function.QueueManagerFunctionApp.User": "Information" 
    } 
    } 
    }
    
    
    import logging def main(): logging.info("some custom log") if __name__ == "__main__": main()
    

    Verify Application Insights is Connected (if used): If you are using Application Insights, make sure it is properly connected to your Azure Functions application to track the logs.

    Run and Test Locally: When running locally in Visual Studio Code, ensure that your local settings (local.settings.json) are not overriding any host.json settings. It should include the necessary configuration to capture logs at the desired level.

    Check Console Output in VS Code: When you run the function app in VS Code, make sure the output/console window is visible and configured to show information logs. Sometimes, VS Code may filter out logs based on its own settings.

    If after making these adjustments you still don't see the expected log outputs, double-check the actual function names and ensure they match exactly with what is specified in the host.json. Additionally, review the runtime version of Azure Functions being used, as logging configurations can slightly differ between runtime versions.

    0 comments No comments