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
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.
Folder structure:
Config.yaml
Main.py
Auth_handler.py
Output in Local:
Getting logs in azure as well, check below: