azure logging wrapper import from another file - not working

66 Views Asked by At

i'm having some issues with a logging wrapper i createed. the repo structure is this one:

├───auth
│   └───auth_handler.py
└───logger.py

inside the auth_handler i have some functions that are used by some fastapi endpoints. the wrapper is this one:

def log_function_call(func):
    def wrapper(*args, **kwargs):
        logger = logging.getLogger(env_logger)
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        properties = {
            "custom_dimensions": {
                "func_name": func.__name__,
                "elapsed": end_time - start_time,
                "args": args,
                "kwargs": kwargs,
                "return": result,
            }
        }
        logger.log(
            logging.INFO,
            msg=f"Calling {func.__name__} elapsed: {end_time-start_time}",
            extra=properties,
        )
        return result

    return wrapper

and it is initialized with a yaml config file with a stream and a Azure handler (with opencensus). the problem i'm facing is that whenever i make a call to the endpoints using the decorated functions, the log is created in stream but not on Azure. if i just use the logger created it works fine.

i'm assuming the problem is the disposition of the files inside the folder, because if i try to call the decorator with a function at the same level as the logger.py file it works, like in this case:


├───auth
│   └───auth_handler.py
└───logger.py
└───test.py

where test.py is something like

import logger as lg


@lg.log_function_call
def func(test):
    return test

here the yaml configuration file:

version: 1
formatters:
  detailedFormatter:
    format: "%(asctime)s loglevel=%(levelname)-6s logger=%(name)s %(funcName)s() L%(lineno)-4d %(message)s   call_trace=%(pathname)s L%(lineno)-4d"
  detailedFormatterAzure:
    format: "loglevel=%(levelname)-6s logger=%(name)s %(funcName)s() L%(lineno)-4d %(message)s   call_trace=%(pathname)s L%(lineno)-4d"

handlers:
  consoleHandler:
    class: logging.StreamHandler
    formatter: detailedFormatter
    stream: ext://sys.stdout

  fileHandler:
    class: logging.FileHandler
    filename: log.log  
    formatter: detailedFormatter

  azureHandler:
    class: opencensus.ext.azure.log_exporter.AzureLogHandler
    connection_string: 
    formatter: detailedFormatterAzure
    level: INFO
    
loggers:
    development:
        level: INFO
        handlers: [azureHandler,consoleHandler]
        propagate: no

    staging:
        level: INFO
        handlers: [consoleHandler]
        propagate: no

    production:
        level: WARNING
        handlers: [consoleHandler]
        propagate: no

root:
  level: INFO
  handlers: [consoleHandler, azureHandler]
  • connection string is missing here for obvious reasons :D
1

There are 1 best solutions below

0
Pavan On

the problem i'm facing is that whenever i make a call to the endpoints using the decorated functions, the log is created in stream but not on Azure.

By using decorated functions to call the wrapper function and able to get the logs local and azure. I have configured application insights in yaml file by using appinsights connection string.

  • To ensure that give correct Import statements to import the functions into another file.

Folder structure:

enter image description here

  • Make sure to give correct connection string in config.yaml.

Config.yaml

handlers:
  consoleHandler:
    class: logging.StreamHandler
    formatter: detailedFormatter
    stream: ext://sys.stdout

  azureHandler:
    class: opencensus.ext.azure.log_exporter.AzureLogHandler
    connection_string: "your-conn-string"
    formatter: detailedFormatterAzure
    level: INFO
    
loggers:
    development:
        level: INFO
        handlers: [azureHandler, consoleHandler]
        propagate: no

root:
  level: INFO
  handlers: [consoleHandler, azureHandler]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

Main.py

from auth.auth_handler import authenticate_user
import logging.config
import yaml

def main():
    # Load logging configuration from config.yaml
    with open('config.yaml', 'r') as f:
        config = yaml.safe_load(f.read())
    logging.config.dictConfig(config)

    result = authenticate_user("test_user", "test_password")
    print("Authentication result:", result)

if __name__ == "__main__":
    main()

Auth_handler.py

from  logger  import  log_function_call

@log_function_call
def  authenticate_user(username, password):
# Perform authentication logic here
# For demonstration purposes, let's assume the authentication is successful
    return  True

Output in Local:

Getting logs in azure as well, check below: