Streaming Logs from Azure Functions
Azure Functions provides real-time logging capabilities, allowing you to monitor the execution and output of your functions as they happen. This is invaluable for debugging, troubleshooting, and understanding the behavior of your serverless applications.
Real-time Log Streaming
The Azure portal offers a dedicated log stream experience that connects to your function app and displays log messages in near real-time. This stream aggregates logs from all instances of your function app, providing a consolidated view.
How to Access Log Streaming
- Navigate to your Function App in the Azure portal.
- In the left-hand menu, under the "Monitoring" section, select "Log stream".
- The log stream will automatically start showing messages. You can connect and disconnect from the stream as needed.
Understanding Log Messages
Log messages typically include information such as:
- Timestamp of the event.
- The specific Function App and Function name.
- The log level (e.g., Information, Warning, Error).
- The log message content, which can include console output, exceptions, or custom logs.
- Instance ID (if running on a scaled-out app).
Custom Logging
You can add custom logging to your functions to track specific business logic or application states. The way you do this depends on your function's language:
Node.js Example
                            module.exports = async function (context, req) {
                                context.log('JavaScript HTTP trigger function processed a request.');
                                const name = (req.query.name || (req.body && req.body.name));
                                const responseMessage = name
                                    ? 'Hello, ' + name + '. This HTTP triggered function executed successfully.'
                                    : 'This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.';
                                context.log(`Processing request for: ${name}`); // Custom log
                                context.res = {
                                    body: responseMessage
                                };
                            };
                        
                    Python Example
                            import logging
                            import azure.functions as func
                            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:
                                    message = f"Hello, {name}. This HTTP triggered function executed successfully."
                                    logging.info(f"Processing request for: {name}") # Custom log
                                else:
                                    message = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                                return func.HttpResponse(
                                    message,
                                    status_code=200
                                )
                        
                    Log Analytics Integration
For long-term storage and powerful querying of your logs, you can configure your Function App to send logs to a Log Analytics workspace. This allows you to use Kusto Query Language (KQL) to analyze your function execution data.
To set this up:
- Ensure your Function App is configured with an Application Insights instance.
- Within your Application Insights resource, navigate to "Usage and estimated costs" -> "Diagnostic settings".
- Add a diagnostic setting and select "Send to Log Analytics workspace".
- Choose your desired Log Analytics workspace.
- Select the relevant log categories, such as "FunctionAppLogs", "FunctionAppEvents", etc.
Once configured, you can query your logs in the Log Analytics workspace using queries like:
                            AppTraces
                            | where TimeGenerated > ago(1h)
                            | where AppName == "your-function-app-name"
                            | order by TimeGenerated desc