File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515#
16+ import os
1617import threading
1718import logging
1819
20+ import google .cloud .logging
21+ from google .cloud .logging_v2 import Client
22+
1923from shared .common .logging_utils import get_env_logging_level
2024
2125
@@ -55,3 +59,39 @@ def get_logger(name: str, stable_id: str = None):
5559 ):
5660 logger .addFilter (StableIdFilter (stable_id ))
5761 return logger
62+
63+
64+ class Logger :
65+ """
66+ Util class for logging information, errors or warnings.
67+ This class uses the Google Cloud Logging API enhancing the logs with extra request information.
68+ """
69+
70+ def __init__ (self , name ):
71+ self .init_logger ()
72+ self .logger = self .init_logger ().logger (name )
73+
74+ @staticmethod
75+ def init_logger () -> Client | None :
76+ """
77+ Initializes the logger
78+ """
79+ if os .getenv ("DEBUG" , "False" ) == "True" :
80+ return None
81+ try :
82+ client = google .cloud .logging .Client ()
83+ client .get_default_handler ()
84+ client .setup_logging ()
85+ return client
86+ except Exception as error :
87+ # This might happen when the GCP authorization credentials are not available.
88+ # Example, when running the tests locally
89+ logging .error (f"Error initializing the logger: { error } " )
90+ return None
91+
92+ def get_logger (self ) -> Client :
93+ """
94+ Get the GCP logger instance
95+ :return: the logger instance
96+ """
97+ return self .logger
You can’t perform that action at this time.
0 commit comments