Skip to content

Commit fb78243

Browse files
samgoodmanSam Goodmankweinmeister
authored
Fix: Give aiplatform logging its own log namespace, let the user configure their own root logger (#1081)
* Don't throw exception when getting representation of GCA resources not yet created * Fixes for PR feedback * Linted changes * Fixes for PR feedback * Linted changes * Base.py no longer sets root logger config * Added logging tests * Removed dangerous pattern encouraging setting of custom logger namespace names * Blacken formatting changes * Added python 3.6 compatability for setting stream handler * Removed unused imports * Revert "Removed dangerous pattern encouraging setting of custom logger namespace names" This reverts commit 35fdda8. * Removed optional logger name param * Added explicit __name__ as param for test * Rerun tests Co-authored-by: Sam Goodman <[email protected]> Co-authored-by: Karl Weinmeister <[email protected]>
1 parent b5f42bd commit fb78243

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

google/cloud/aiplatform/base.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,26 @@
4747
from google.cloud.aiplatform.compat.types import encryption_spec as gca_encryption_spec
4848
from google.protobuf import json_format
4949

50-
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
51-
5250
# This is the default retry callback to be used with get methods.
5351
_DEFAULT_RETRY = retry.Retry()
5452

5553

5654
class Logger:
5755
"""Logging wrapper class with high level helper methods."""
5856

59-
def __init__(self, name: str = ""):
60-
"""Initializes logger with name.
57+
def __init__(self, name: str):
58+
"""Initializes logger with optional name.
6159
6260
Args:
6361
name (str): Name to associate with logger.
6462
"""
6563
self._logger = logging.getLogger(name)
64+
self._logger.setLevel(logging.INFO)
65+
66+
handler = logging.StreamHandler(sys.stdout)
67+
handler.setLevel(logging.INFO)
68+
69+
self._logger.addHandler(handler)
6670

6771
def log_create_with_lro(
6872
self,

tests/unit/aiplatform/test_logging.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
from google.cloud.aiplatform import base
19+
import logging
20+
21+
22+
class TestLogging:
23+
def test_no_root_logging_handler_override(self, caplog):
24+
# Users should be able to control the root logger in their apps
25+
# The aiplatform module import should not override their root logger config
26+
caplog.set_level(logging.DEBUG)
27+
28+
logging.debug("Debug level")
29+
logging.info("Info level")
30+
logging.critical("Critical level")
31+
32+
assert "Debug level\n" in caplog.text
33+
assert "Info level\n" in caplog.text
34+
assert "Critical level\n" in caplog.text
35+
36+
def test_log_level_coexistance(self, caplog):
37+
# The aiplatform module and the root logger can have different log levels.
38+
aip_logger = base.Logger(__name__)
39+
40+
caplog.set_level(logging.DEBUG)
41+
42+
logging.debug("This should exist")
43+
logging.info("This should too")
44+
45+
aip_logger.info("This should also exist")
46+
aip_logger.debug("This should NOT exist")
47+
48+
assert "This should exist\n" in caplog.text
49+
assert "This should too\n" in caplog.text
50+
assert "This should also exist\n" in caplog.text
51+
assert "This should NOT exist\n" not in caplog.text

0 commit comments

Comments
 (0)