Skip to content

Commit 9b75259

Browse files
jaycee-licopybara-github
authored andcommitted
fix: duplicate logs in Colab
PiperOrigin-RevId: 571173374
1 parent 1d65347 commit 9b75259

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

google/cloud/aiplatform/base.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,6 @@ def __init__(self, name: str):
6969
super().__init__(name)
7070
self.setLevel(logging.INFO)
7171

72-
# To avoid writing duplicate logs, skip adding the new handler if
73-
# StreamHandler already exists in logger hierarchy.
74-
logger = self
75-
while logger:
76-
for handler in logger.handlers:
77-
if isinstance(handler, logging.StreamHandler):
78-
return
79-
logger = logger.parent
80-
81-
handler = logging.StreamHandler(sys.stdout)
82-
handler.setLevel(logging.INFO)
83-
84-
self.addHandler(handler)
85-
8672
def log_create_with_lro(
8773
self,
8874
cls: Type["VertexAiResourceNoun"],
@@ -196,7 +182,22 @@ def Logger(name: str) -> VertexLogger: # pylint: disable=invalid-name
196182
old_class = logging.getLoggerClass()
197183
try:
198184
logging.setLoggerClass(VertexLogger)
199-
return logging.getLogger(name)
185+
logger = logging.getLogger(name)
186+
187+
# To avoid writing duplicate logs, skip adding the new handler if
188+
# StreamHandler already exists in logger hierarchy.
189+
parent_logger = logger
190+
while parent_logger:
191+
for handler in parent_logger.handlers:
192+
if isinstance(handler, logging.StreamHandler):
193+
return logger
194+
parent_logger = parent_logger.parent
195+
196+
handler = logging.StreamHandler(sys.stdout)
197+
handler.setLevel(logging.INFO)
198+
logger.addHandler(handler)
199+
200+
return logger
200201
finally:
201202
logging.setLoggerClass(old_class)
202203

tests/unit/aiplatform/test_base.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright 2020 Google LLC
3+
# Copyright 2023 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
#
1717

1818
from importlib import reload
19+
import logging
1920
import pytest
2021
import time
2122
from typing import Optional
@@ -24,6 +25,9 @@
2425
from google.cloud.aiplatform import initializer
2526

2627

28+
_TEST_LOGGER_NAME = "test_logger"
29+
30+
2731
class _TestClass(base.FutureManager):
2832
def __init__(self, x):
2933
self.x = x
@@ -199,3 +203,12 @@ def test_create_and_add_return_arg(self, sync):
199203
assert isinstance(a, _TestClass)
200204
assert isinstance(b, _TestClassDownStream)
201205
assert isinstance(c, _TestClass)
206+
207+
208+
class TestLogger:
209+
def test_logger_handler(self):
210+
logger = base.Logger(_TEST_LOGGER_NAME)
211+
212+
assert logger.level == logging.INFO
213+
# the logger won't have a StreamHandler because root logger already has one
214+
assert not logger.handlers

0 commit comments

Comments
 (0)