Skip to content

Commit adc5baa

Browse files
committed
Add app insights logging to function code
1 parent e89c013 commit adc5baa

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

code/function/fastapp/core/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from pydantic import BaseSettings
3+
from pydantic import BaseSettings, Field
44

55

66
class Settings(BaseSettings):
@@ -10,6 +10,9 @@ class Settings(BaseSettings):
1010
API_V1_STR: str = "/v1"
1111
LOGGING_LEVEL: int = logging.INFO
1212
DEBUG: bool = False
13+
APPLICATIONINSIGHTS_CONNECTION_STRING: str = Field(
14+
default="", env="APPLICATIONINSIGHTS_CONNECTION_STRING"
15+
)
1316

1417

1518
settings = Settings()

code/function/fastapp/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
from logging import Logger
33

44
from fastapp.core.config import settings
5+
from opencensus.ext.azure.log_exporter import AzureLogHandler
56

67

78
def setup_logging(module) -> Logger:
89
"""Setup logging and event handler.
10+
911
RETURNS (Logger): The logger object to log activities.
1012
"""
1113
logger = logging.getLogger(module)
@@ -17,6 +19,16 @@ def setup_logging(module) -> Logger:
1719
logger_stream_handler.setFormatter(
1820
logging.Formatter("[%(asctime)s] [%(levelname)s] [%(module)-8.8s] %(message)s")
1921
)
20-
2122
logger.addHandler(logger_stream_handler)
23+
24+
# Add azure event handler
25+
if settings.APPLICATIONINSIGHTS_CONNECTION_STRING:
26+
azure_log_handler = AzureLogHandler()
27+
azure_log_handler.setFormatter(
28+
logging.Formatter(
29+
"[%(asctime)s] [%(levelname)s] [%(module)-8.8s] %(message)s"
30+
)
31+
)
32+
logger.addHandler(azure_log_handler)
33+
2234
return logger

code/function/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
azure-functions~=1.14.0
66
fastapi~=0.96.1
77
aiohttp~=3.8.4
8+
opencensus~=0.11.0
9+
opencensus-ext-logging~=0.1.1
10+
opencensus-ext-azure~=1.1.9

0 commit comments

Comments
 (0)