Azure hide not mine logs - see only my logs

54 Views Asked by At

I write in python and use logging.info() to print my logs. When I enter log stream in azure functions I see many other logs among my logs. It makes the debugging harder.

For example I have those durable function logs: 2024-03-17T13:16:01Z [Information] TaskActiviyDispatcher-2618c7d-0: Delaying work item fetching because the current active work-item count (1) exceeds the configured maximum active work-item count (1) 2024-03-17T13:16:02Z [Information] linuxfunction1-control-00: Skipping ownership lease aquiring for linuxfunction1-control-00 2024-03-17T13:16:02Z [Information] linuxfunction1-control-02: Skipping ownership lease aquiring for linuxfunction1-control-02

I don't know if they are important (can assume it is) but I want to hide them so I can concentrate on my logs.

I'm new to Azure but experienced with AWS, so if I am missing something feel free to mention.

1

There are 1 best solutions below

0
Pavan On

Azure hide not mine logs - see only my logs

I have created the Http trigger function with runtime stack python and added Myfilter function to filter the unrequired logs.

The function starts by calling configure_logging() to set up the logging configuration.

  • Customize the log filter condition in the MyFilter class according to specific log messages provided in the below code, logs containing the string "MY_APPLICATION_IDENTIFIER" will be allowed.

Function code:

import logging
import azure.functions as func

class MyFilter(logging.Filter):
    def filter(self, record):
        # Customize this condition based on your logs format or criteria
        # In this example, we only allow logs that contain "MY_APPLICATION_IDENTIFIER"
        return "MY_APPLICATION_IDENTIFIER" in record.getMessage()

def configure_logging():
    # Get the root logger
    logger = logging.getLogger()

    # Clear any existing handlers to avoid duplicate logs
    for handler in logger.handlers:
        logger.removeHandler(handler)

    # Create a new StreamHandler to output logs
    handler = logging.StreamHandler()
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

    # Set the formatter for the handler
    handler.setFormatter(formatter)

    # Create a filter instance
    my_filter = MyFilter()

    # Add the filter to the handler
    handler.addFilter(my_filter)

    # Add the handler to the root logger
    logger.addHandler(handler)

# Initialize logging configuration
configure_logging()

app=func.FunctionApp()

@app.route(route="http_trigger")
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('MY_APPLICATION_IDENTIFIER: 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}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
            "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
            status_code=200
        )

Output in local:

enter image description here

The above function deployed successfully into portal and it ran successfully in portal as well. check below:

enter image description here

Output in portal:

I have observed the required information logs in log stream. check below:

enter image description here

  • Adjust the filtering criteria in the MyFilter class as needed to specific requirements.