Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
eb8d523
added test checking for duplicates
daniel-sanche Mar 9, 2021
812d69c
updated submodule
daniel-sanche Mar 9, 2021
78af961
Merge branch 'master' into test-duplicates
daniel-sanche Mar 9, 2021
5198424
updated submodule
daniel-sanche Mar 9, 2021
4e9d394
added structured log handler
daniel-sanche Mar 9, 2021
c65ccf0
use structured log resource type for cloud functions
daniel-sanche Mar 9, 2021
e003453
fixed import path
daniel-sanche Mar 9, 2021
100cb9a
fixed import issues
daniel-sanche Mar 9, 2021
2ec7241
fixed StructuredLogHandler
daniel-sanche Mar 9, 2021
a330440
remove default handlers for AppEngine Flex
daniel-sanche Mar 9, 2021
cd4d1b1
updated submodule
daniel-sanche Mar 9, 2021
4c556b9
add insertId to structured logs
daniel-sanche Mar 9, 2021
2e718db
changed where handlers are removed
daniel-sanche Mar 9, 2021
a4b975d
only use structured logging for GCF >= 3.8
daniel-sanche Mar 9, 2021
407215d
added default value for project
daniel-sanche Mar 9, 2021
827171c
cleaned up resource detection
daniel-sanche Mar 9, 2021
96166eb
removed insertId field
daniel-sanche Mar 9, 2021
fb8a4bb
added import
daniel-sanche Mar 10, 2021
a5a7d35
fixed lint issues
daniel-sanche Mar 10, 2021
4733d20
removed structuredloghandler from this PR
daniel-sanche Mar 11, 2021
2d65eb8
added tests
daniel-sanche Mar 11, 2021
75e9b60
fixed lint issue
daniel-sanche Mar 11, 2021
77fcb0d
patch environment variables in test
daniel-sanche Mar 11, 2021
447b6c5
removed unneeded import
daniel-sanche Mar 11, 2021
7a8336d
updated submodule
daniel-sanche Mar 11, 2021
f4c0170
undid last commit
daniel-sanche Mar 11, 2021
7c6f1b9
updated submodule
daniel-sanche Mar 13, 2021
034a498
updated submodule
daniel-sanche Mar 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions google/cloud/logging_v2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

_GAE_RESOURCE_TYPE = "gae_app"
_GKE_RESOURCE_TYPE = "k8s_container"
_GCF_RESOURCE_TYPE = "cloud_function"


class Client(ClientWithProject):
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/logging_v2/handlers/_monitored_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def _create_global_resource(project):
return Resource(type="global", labels={"project_id": project})


def detect_resource(project):
def detect_resource(project=""):
"""Return the default monitored resource based on the local environment.
Args:
project (str): The project ID to pass on to the resource
Expand Down
7 changes: 7 additions & 0 deletions google/cloud/logging_v2/handlers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

EXCLUDED_LOGGER_DEFAULTS = ("google.cloud", "google.auth", "google_auth_httplib2")

_CLEAR_HANDLER_RESOURCE_TYPES = ("gae_app", "cloud_function")


class CloudLoggingHandler(logging.StreamHandler):
"""Handler that directly makes Cloud Logging API calls.
Expand Down Expand Up @@ -160,6 +162,11 @@ def setup_logging(
"""
all_excluded_loggers = set(excluded_loggers + EXCLUDED_LOGGER_DEFAULTS)
logger = logging.getLogger()

# remove built-in handlers on App Engine or Cloud Functions environments
if detect_resource().type in _CLEAR_HANDLER_RESOURCE_TYPES:
logger.handlers.clear()

logger.setLevel(log_level)
logger.addHandler(handler)
for logger_name in all_excluded_loggers:
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/handlers/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@

import logging
import unittest
from unittest.mock import patch
import mock

from google.cloud.logging_v2.handlers._monitored_resources import (
_FUNCTION_ENV_VARS,
_GAE_ENV_VARS,
)


class TestCloudLoggingHandler(unittest.TestCase):

Expand Down Expand Up @@ -165,6 +171,49 @@ def test_setup_logging_excludes(self):
self.assertNotIn(handler, excluded_logger.handlers)
self.assertFalse(excluded_logger.propagate)

@patch.dict("os.environ", {envar: "1" for envar in _FUNCTION_ENV_VARS})
def test_remove_handlers_gcf(self):
logger = logging.getLogger()
# add fake handler
added_handler = logging.StreamHandler()
logger.addHandler(added_handler)

handler = _Handler(logging.INFO)
self._call_fut(handler)
self.assertNotIn(added_handler, logger.handlers)
# handler should be removed from logger
self.assertEqual(len(logger.handlers), 1)

@patch.dict("os.environ", {envar: "1" for envar in _GAE_ENV_VARS})
def test_remove_handlers_gae(self):
logger = logging.getLogger()
# add fake handler
added_handler = logging.StreamHandler()
logger.addHandler(added_handler)

handler = _Handler(logging.INFO)
self._call_fut(handler)
self.assertNotIn(added_handler, logger.handlers)
# handler should be removed from logger
self.assertEqual(len(logger.handlers), 1)

def test_keep_handlers_others(self):
# mock non-cloud environment
patch = mock.patch(
"google.cloud.logging_v2.handlers._monitored_resources.retrieve_metadata_server",
return_value=None,
)
with patch:
# add fake handler
added_handler = logging.StreamHandler()
logger = logging.getLogger()
logger.addHandler(added_handler)

handler = _Handler(logging.INFO)
self._call_fut(handler)
# added handler should remain in logger
self.assertIn(added_handler, logger.handlers)

def setUp(self):
self._handlers_cache = logging.getLogger().handlers[:]

Expand Down