Skip to content

Commit 838d416

Browse files
authored
Initialize logger before accessing it in python code (#1810)
1 parent 89a8128 commit 838d416

File tree

12 files changed

+45
-14
lines changed

12 files changed

+45
-14
lines changed

pkg/cortex/serve/cortex_internal/lib/api/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
from cortex_internal.lib.api import Predictor
2424
from cortex_internal.lib.exceptions import CortexException
2525
from cortex_internal.lib.storage import S3, GCS
26-
from cortex_internal.lib.log import logger
26+
from cortex_internal.lib.log import configure_logger
27+
28+
logger = configure_logger("cortex", os.environ["CORTEX_LOG_CONFIG_FILE"])
2729

2830

2931
class API:

pkg/cortex/serve/cortex_internal/lib/api/predictor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@
5858
# misc
5959
from cortex_internal.lib.storage import S3, GCS
6060
from cortex_internal.lib import util
61-
from cortex_internal.lib.log import logger
6261
from cortex_internal.lib.exceptions import CortexException, UserException, UserRuntimeException
6362
from cortex_internal import consts
63+
from cortex_internal.lib.log import configure_logger
64+
65+
logger = configure_logger("cortex", os.environ["CORTEX_LOG_CONFIG_FILE"])
6466

6567

6668
class Predictor:

pkg/cortex/serve/cortex_internal/lib/api/task.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import inspect
1818
import dill
1919

20-
from cortex_internal.lib.log import logger
2120
from cortex_internal.lib.exceptions import CortexException, UserException, UserRuntimeException
21+
from cortex_internal.lib.log import configure_logger
22+
23+
logger = configure_logger("cortex", os.environ["CORTEX_LOG_CONFIG_FILE"])
2224

2325

2426
class TaskAPI:

pkg/cortex/serve/cortex_internal/lib/client/onnx.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
onnx_dependencies_installed = False
2727
import numpy as np
2828

29-
from cortex_internal.lib.log import logger
3029
from cortex_internal.lib import util
3130
from cortex_internal.lib.exceptions import (
3231
UserRuntimeException,
@@ -45,6 +44,9 @@
4544
)
4645
from cortex_internal.lib.concurrency import LockedFile
4746
from cortex_internal import consts
47+
from cortex_internal.lib.log import configure_logger
48+
49+
logger = configure_logger("cortex", os.environ["CORTEX_LOG_CONFIG_FILE"])
4850

4951

5052
class ONNXClient:

pkg/cortex/serve/cortex_internal/lib/client/python.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import multiprocessing as mp
1919
from typing import Any, Optional, Callable
2020

21-
from cortex_internal.lib.log import logger
2221
from cortex_internal.lib.exceptions import (
2322
UserRuntimeException,
2423
CortexException,
@@ -36,6 +35,9 @@
3635
)
3736
from cortex_internal.lib.concurrency import LockedFile
3837
from cortex_internal import consts
38+
from cortex_internal.lib.log import configure_logger
39+
40+
logger = configure_logger("cortex", os.environ["CORTEX_LOG_CONFIG_FILE"])
3941

4042

4143
class PythonClient:

pkg/cortex/serve/cortex_internal/lib/client/tensorflow.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
LockedModelsTree,
3333
get_models_from_api_spec,
3434
)
35-
from cortex_internal.lib.log import logger
3635
from cortex_internal import consts
36+
from cortex_internal.lib.log import configure_logger
37+
38+
logger = configure_logger("cortex", os.environ["CORTEX_LOG_CONFIG_FILE"])
3739

3840

3941
class TensorFlowClient:

pkg/cortex/serve/cortex_internal/lib/log.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import http
2020
import datetime as dt
2121
import yaml
22+
import threading
2223
from pythonjsonlogger.jsonlogger import JsonFormatter
2324

2425

@@ -55,12 +56,17 @@ def format(self, record):
5556

5657

5758
logger = None
59+
_logger_initializer_mutex = threading.Lock()
5860

5961

6062
def configure_logger(name: str, config_file: str):
61-
global logger
62-
logger = retrieve_logger(name, config_file)
63-
return logger
63+
with _logger_initializer_mutex:
64+
global logger
65+
if logger is not None:
66+
return logger
67+
68+
logger = retrieve_logger(name, config_file)
69+
return logger
6470

6571

6672
def retrieve_logger(name: str, config_file: str):

pkg/cortex/serve/cortex_internal/lib/model/cron.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from typing import Dict, List, Tuple, Any, Union, Callable, Optional
2727

2828
from cortex_internal.lib import util
29-
from cortex_internal.lib.log import logger
3029
from cortex_internal.lib.concurrency import LockedFile, get_locked_files
3130
from cortex_internal.lib.storage import S3, GCS
3231
from cortex_internal.lib.exceptions import CortexException, WithBreak
@@ -53,6 +52,9 @@
5352
ModelsTree,
5453
LockedModelsTree,
5554
)
55+
from cortex_internal.lib.log import configure_logger
56+
57+
logger = configure_logger("cortex", os.environ["CORTEX_LOG_CONFIG_FILE"])
5658

5759

5860
class AbstractLoopingThread(td.Thread):

pkg/cortex/serve/cortex_internal/lib/model/model.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import threading as td
2020
from typing import Dict, List, Any, Tuple, Callable, AbstractSet, Optional
2121

22-
from cortex_internal.lib.log import logger
2322
from cortex_internal.lib.concurrency import ReadWriteLock
2423
from cortex_internal.lib.exceptions import WithBreak, CortexException
2524
from cortex_internal.lib.type import PredictorType
25+
from cortex_internal.lib.log import configure_logger
26+
27+
logger = configure_logger("cortex", os.environ["CORTEX_LOG_CONFIG_FILE"])
2628

2729

2830
class ModelsHolder:

pkg/cortex/serve/cortex_internal/lib/model/tfs.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
from typing import Any, Optional, Dict, List, Tuple
2020

2121
from cortex_internal.lib.exceptions import CortexException, UserException
22-
from cortex_internal.lib.log import logger
22+
from cortex_internal.lib.log import configure_logger
23+
24+
logger = configure_logger("cortex", os.environ["CORTEX_LOG_CONFIG_FILE"])
25+
2326

2427
# TensorFlow types
2528
def _define_types() -> Tuple[Dict[str, Any], Dict[str, str]]:

0 commit comments

Comments
 (0)