Skip to content

Commit 4c44c4b

Browse files
committed
Test global init
1 parent 7a7972f commit 4c44c4b

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

functions-python/batch_process_dataset/src/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
from shared.helpers.utils import download_and_get_hash
3939
from sqlalchemy.orm import Session
4040

41-
# init_logger()
41+
init_logger()
4242

43-
logging.basicConfig(level=logging.INFO)
43+
# logging.basicConfig(level=logging.INFO)
4444

4545
@dataclass
4646
class DatasetFile:
@@ -314,7 +314,7 @@ def process_dataset(cloud_event: CloudEvent):
314314
}
315315
}
316316
"""
317-
Logger.init_logger()
317+
# init_logger()
318318
logging.info("Function Started")
319319
print("Function Started print")
320320
stable_id = "UNKNOWN"

functions-python/helpers/logger.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
# limitations under the License.
1515
#
1616
import os
17-
import threading
1817
import logging
18+
import threading
1919

2020
import google.cloud.logging
2121
from google.cloud.logging_v2 import Client
2222

2323
from shared.common.logging_utils import get_env_logging_level
2424

25+
def is_local_env():
26+
return os.getenv("K_SERVICE") is None
2527

2628
class StableIdFilter(logging.Filter):
2729
"""Add a stable_id to the log record"""
@@ -35,11 +37,28 @@ def filter(self, record):
3537
record.msg = f"[{self.stable_id}] {record.msg}"
3638
return True
3739

40+
lock = threading.Lock()
41+
_logging_initialized = False
3842
def init_logger():
3943
"""
4044
Initializes the logger
4145
"""
42-
logging.basicConfig(level=logging.INFO)
46+
logging.basicConfig(level=get_env_logging_level())
47+
global _logging_initialized
48+
if not is_local_env() and not _logging_initialized:
49+
with lock:
50+
if _logging_initialized:
51+
return
52+
try:
53+
client = google.cloud.logging.Client()
54+
client.get_default_handler()
55+
client.setup_logging()
56+
return client
57+
except Exception as error:
58+
# This might happen when the GCP authorization credentials are not available.
59+
# Example, when running the tests locally
60+
logging.error(f"Error initializing the logger: {error}")
61+
_logging_initialized = True
4362

4463

4564
def get_logger(name: str, stable_id: str = None):

0 commit comments

Comments
 (0)