Skip to content

Commit 2be5523

Browse files
Add functions to delay AzureLogHandler instantiation. (#137)
1 parent bdfa2c3 commit 2be5523

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

datareservoirio/_logging.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import os
3-
from functools import wraps
3+
from functools import wraps, cache
44

55
from opencensus.ext.azure.log_exporter import AzureLogHandler
66

@@ -9,18 +9,20 @@
99
from ._constants import ENV_VAR_ENABLE_APP_INSIGHTS, ENV_VAR_ENGINE_ROOM_APP_ID
1010
from .globalsettings import environment
1111

12-
exceptions_logger = logging.getLogger(__name__ + "_exception_logger")
13-
exceptions_logger.setLevel(logging.DEBUG)
14-
15-
if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None:
16-
enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower()
17-
if enable_app_insights == "true" or enable_app_insights == "1":
18-
app_insight_handler = AzureLogHandler(
19-
connection_string=environment._application_insight_connectionstring
20-
)
21-
app_insight_handler.setLevel("WARNING")
22-
exceptions_logger.addHandler(app_insight_handler)
23-
12+
@cache
13+
def get_exceptions_logger() -> logging.Logger:
14+
exceptions_logger = logging.getLogger(__name__ + "_exception_logger")
15+
exceptions_logger.setLevel(logging.DEBUG)
16+
17+
if os.getenv(ENV_VAR_ENABLE_APP_INSIGHTS) is not None:
18+
enable_app_insights = os.environ[ENV_VAR_ENABLE_APP_INSIGHTS].lower()
19+
if enable_app_insights == "true" or enable_app_insights == "1":
20+
app_insight_handler = AzureLogHandler(
21+
connection_string=environment._application_insight_connectionstring
22+
)
23+
app_insight_handler.setLevel("WARNING")
24+
exceptions_logger.addHandler(app_insight_handler)
25+
return exceptions_logger
2426

2527
def log_decorator(log_level):
2628
def decorator(func):
@@ -39,7 +41,7 @@ def wrapper(self, *args, **kwargs):
3941
ENV_VAR_ENGINE_ROOM_APP_ID
4042
)
4143

42-
log_function = getattr(exceptions_logger, log_level)
44+
log_function = getattr(get_exceptions_logger(), log_level)
4345
log_function(e, extra=properties)
4446
raise e
4547

datareservoirio/client.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections import defaultdict
55
from concurrent.futures import ThreadPoolExecutor
66
from datetime import datetime
7-
from functools import wraps
7+
from functools import wraps, cache
88
from operator import itemgetter
99
from urllib.parse import urlencode
1010
from uuid import uuid4
@@ -29,11 +29,14 @@
2929

3030
log = logging.getLogger(__name__)
3131

32-
metric = logging.getLogger(__name__ + "_metric_appinsight")
33-
metric.setLevel(logging.DEBUG)
34-
metric.addHandler(
35-
AzureLogHandler(connection_string=environment._application_insight_connectionstring)
36-
)
32+
@cache
33+
def metric() -> logging.Logger:
34+
logger = logging.getLogger(__name__ + "_metric_appinsight")
35+
logger.setLevel(logging.DEBUG)
36+
logger.addHandler(
37+
AzureLogHandler(connection_string=environment._application_insight_connectionstring)
38+
)
39+
return logger
3740

3841
# Default values to push as start/end dates. (Limited by numpy.datetime64)
3942
_END_DEFAULT = 9214646400000000000 # 2262-01-01
@@ -329,7 +332,7 @@ def wrapper(self, series_id, start=None, end=None, **kwargs):
329332
"number-of-samples": number_of_samples,
330333
}
331334
}
332-
metric.info("Timer", extra=properties)
335+
metric().info("Timer", extra=properties)
333336
return result
334337

335338
return wrapper

0 commit comments

Comments
 (0)