File tree Expand file tree Collapse file tree 2 files changed +24
-5
lines changed
batch_process_dataset/src Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Original file line number Diff line number Diff line change 3838from shared .helpers .utils import download_and_get_hash
3939from 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
4646class 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"
Original file line number Diff line number Diff line change 1414# limitations under the License.
1515#
1616import os
17- import threading
1817import logging
18+ import threading
1919
2020import google .cloud .logging
2121from google .cloud .logging_v2 import Client
2222
2323from shared .common .logging_utils import get_env_logging_level
2424
25+ def is_local_env ():
26+ return os .getenv ("K_SERVICE" ) is None
2527
2628class 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
3842def 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
4564def get_logger (name : str , stable_id : str = None ):
You can’t perform that action at this time.
0 commit comments