Skip to content

Commit 5cea64d

Browse files
committed
Add back logger until all functions are refactored
1 parent d57eb83 commit 5cea64d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

functions-python/helpers/logger.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16+
import os
1617
import threading
1718
import logging
1819

20+
import google.cloud.logging
21+
from google.cloud.logging_v2 import Client
22+
1923
from 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

0 commit comments

Comments
 (0)