@@ -128,13 +128,13 @@ async def content_file(path: str, auth_claims: Dict[str, Any]):
128
128
if path .find ("#page=" ) > 0 :
129
129
path_parts = path .rsplit ("#page=" , 1 )
130
130
path = path_parts [0 ]
131
- logging .info ("Opening file %s" , path )
131
+ current_app . logger .info ("Opening file %s" , path )
132
132
blob_container_client : ContainerClient = current_app .config [CONFIG_BLOB_CONTAINER_CLIENT ]
133
133
blob : Union [BlobDownloader , DatalakeDownloader ]
134
134
try :
135
135
blob = await blob_container_client .get_blob_client (path ).download_blob ()
136
136
except ResourceNotFoundError :
137
- logging .info ("Path not found in general Blob container: %s" , path )
137
+ current_app . logger .info ("Path not found in general Blob container: %s" , path )
138
138
if current_app .config [CONFIG_USER_UPLOAD_ENABLED ]:
139
139
try :
140
140
user_oid = auth_claims ["oid" ]
@@ -143,7 +143,7 @@ async def content_file(path: str, auth_claims: Dict[str, Any]):
143
143
file_client = user_directory_client .get_file_client (path )
144
144
blob = await file_client .download_file ()
145
145
except ResourceNotFoundError :
146
- logging .exception ("Path not found in DataLake: %s" , path )
146
+ current_app . logger .exception ("Path not found in DataLake: %s" , path )
147
147
abort (404 )
148
148
else :
149
149
abort (404 )
@@ -314,7 +314,7 @@ async def speech():
314
314
current_app .logger .error ("Unexpected result reason: %s" , result .reason )
315
315
raise Exception ("Speech synthesis failed. Check logs for details." )
316
316
except Exception as e :
317
- logging .exception ("Exception in /speech" )
317
+ current_app . logger .exception ("Exception in /speech" )
318
318
return jsonify ({"error" : str (e )}), 500
319
319
320
320
@@ -453,6 +453,7 @@ async def setup_clients():
453
453
# Set up authentication helper
454
454
search_index = None
455
455
if AZURE_USE_AUTHENTICATION :
456
+ current_app .logger .info ("AZURE_USE_AUTHENTICATION is true, setting up search index client" )
456
457
search_index_client = SearchIndexClient (
457
458
endpoint = f"https://{ AZURE_SEARCH_SERVICE } .search.windows.net" ,
458
459
credential = azure_credential ,
@@ -516,6 +517,7 @@ async def setup_clients():
516
517
openai_client : AsyncOpenAI
517
518
518
519
if USE_SPEECH_OUTPUT_AZURE :
520
+ current_app .logger .info ("USE_SPEECH_OUTPUT_AZURE is true, setting up Azure speech service" )
519
521
if not AZURE_SPEECH_SERVICE_ID or AZURE_SPEECH_SERVICE_ID == "" :
520
522
raise ValueError ("Azure speech resource not configured correctly, missing AZURE_SPEECH_SERVICE_ID" )
521
523
if not AZURE_SPEECH_SERVICE_LOCATION or AZURE_SPEECH_SERVICE_LOCATION == "" :
@@ -530,28 +532,36 @@ async def setup_clients():
530
532
if OPENAI_HOST .startswith ("azure" ):
531
533
api_version = os .getenv ("AZURE_OPENAI_API_VERSION" ) or "2024-03-01-preview"
532
534
if OPENAI_HOST == "azure_custom" :
535
+ current_app .logger .info ("OPENAI_HOST is azure_custom, setting up Azure OpenAI custom client" )
533
536
if not AZURE_OPENAI_CUSTOM_URL :
534
537
raise ValueError ("AZURE_OPENAI_CUSTOM_URL must be set when OPENAI_HOST is azure_custom" )
535
538
endpoint = AZURE_OPENAI_CUSTOM_URL
536
539
else :
540
+ current_app .logger .info ("OPENAI_HOST is azure, setting up Azure OpenAI client" )
537
541
if not AZURE_OPENAI_SERVICE :
538
542
raise ValueError ("AZURE_OPENAI_SERVICE must be set when OPENAI_HOST is azure" )
539
543
endpoint = f"https://{ AZURE_OPENAI_SERVICE } .openai.azure.com"
540
544
if api_key := os .getenv ("AZURE_OPENAI_API_KEY" ):
545
+ current_app .logger .info ("AZURE_OPENAI_API_KEY found, using as value for api_key for Azure OpenAI client" )
541
546
openai_client = AsyncAzureOpenAI (api_version = api_version , azure_endpoint = endpoint , api_key = api_key )
542
547
else :
548
+ current_app .logger .info ("Using Azure credential (passwordless authentication) for Azure OpenAI client" )
543
549
token_provider = get_bearer_token_provider (azure_credential , "https://cognitiveservices.azure.com/.default" )
544
550
openai_client = AsyncAzureOpenAI (
545
551
api_version = api_version ,
546
552
azure_endpoint = endpoint ,
547
553
azure_ad_token_provider = token_provider ,
548
554
)
549
555
elif OPENAI_HOST == "local" :
556
+ current_app .logger .info ("OPENAI_HOST is local, setting up local OpenAI client for OPENAI_BASE_URL with no key" )
550
557
openai_client = AsyncOpenAI (
551
558
base_url = os .environ ["OPENAI_BASE_URL" ],
552
559
api_key = "no-key-required" ,
553
560
)
554
561
else :
562
+ current_app .logger .info (
563
+ "OPENAI_HOST is not azure, setting up OpenAI client using OPENAI_API_KEY and OPENAI_ORGANIZATION environment variables"
564
+ )
555
565
openai_client = AsyncOpenAI (
556
566
api_key = OPENAI_API_KEY ,
557
567
organization = OPENAI_ORGANIZATION ,
@@ -660,6 +670,7 @@ def create_app():
660
670
app .register_blueprint (bp )
661
671
662
672
if os .getenv ("APPLICATIONINSIGHTS_CONNECTION_STRING" ):
673
+ app .logger .info ("APPLICATIONINSIGHTS_CONNECTION_STRING is set, enabling Azure Monitor" )
663
674
configure_azure_monitor ()
664
675
# This tracks HTTP requests made by aiohttp:
665
676
AioHttpClientInstrumentor ().instrument ()
@@ -670,13 +681,14 @@ def create_app():
670
681
# This middleware tracks app route requests:
671
682
app .asgi_app = OpenTelemetryMiddleware (app .asgi_app ) # type: ignore[assignment]
672
683
673
- # Level should be one of https://docs.python.org/3/library/logging.html#logging-levels
674
- default_level = "INFO" # In development, log more verbosely
675
- if os .getenv ("WEBSITE_HOSTNAME" ): # In production, don't log as heavily
676
- default_level = "WARNING"
677
- logging .basicConfig (level = os .getenv ("APP_LOG_LEVEL" , default_level ))
684
+ # Log levels should be one of https://docs.python.org/3/library/logging.html#logging-levels
685
+ # Set root level to WARNING to avoid seeing overly verbose logs from SDKS
686
+ logging .basicConfig (level = logging .WARNING )
687
+ # Set the app logger level to INFO by default
688
+ default_level = "INFO"
689
+ app .logger .setLevel (os .getenv ("APP_LOG_LEVEL" , default_level ))
678
690
679
691
if allowed_origin := os .getenv ("ALLOWED_ORIGIN" ):
680
- app .logger .info ("CORS enabled for %s" , allowed_origin )
692
+ app .logger .info ("ALLOWED_ORIGIN is set, enabling CORS for %s" , allowed_origin )
681
693
cors (app , allow_origin = allowed_origin , allow_methods = ["GET" , "POST" ])
682
694
return app
0 commit comments