# Path: google/cloud/logging_v2/handlers/_monitored_resources.py
# _FUNCTION_ENV_VARS = [_FUNCTION_TARGET, _FUNCTION_SIGNATURE, _CLOUD_RUN_SERVICE_ID]
# 
# _GAE_ENV_VARS = [_GAE_SERVICE_ENV, _GAE_VERSION_ENV, _GAE_INSTANCE_ENV]


import logging
import unittest
import mock
import json
        import flask
        import logging
        import logging
        import logging
        import logging
        import logging
        import sys
        import io
from unittest.mock import patch
from google.cloud.logging_v2.handlers._monitored_resources import (
    _FUNCTION_ENV_VARS,
    _GAE_ENV_VARS,
)
        from google.cloud.logging.handlers import CloudLoggingFilter
        from google.cloud.logging.handlers import CloudLoggingHandler
        from google.cloud.logging_v2.handlers._monitored_resources import (
            _create_global_resource,
        )
        from google.cloud.logging_v2.handlers.handlers import DEFAULT_LOGGER_NAME
        from google.cloud.logging import Resource
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.resource import Resource
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging.handlers import setup_logging
        root_handlers = logging.getLogger().handlers
        self.assertIn(handler, root_handlers)

    def test_setup_logging_excludes(self):
        INCLUDED_LOGGER_NAME = "includeme"
        EXCLUDED_LOGGER_NAME = "excludeme"

        handler = _Handler(logging.INFO)
        self._call_fut(handler, (EXCLUDED_LOGGER_NAME,))

        included_logger = logging.getLogger(INCLUDED_LOGGER_NAME)
        self.assertTrue(included_logger.propagate)

        excluded_logger = logging.getLogger(EXCLUDED_LOGGER_NAME)
        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)


# Path: google/cloud/logging_v2/handlers/_monitored_resources.py
# _FUNCTION_ENV_VARS = [_FUNCTION_TARGET, _FUNCTION_SIGNATURE, _CLOUD_RUN_SERVICE_ID]
# 
# _GAE_ENV_VARS = [_GAE_SERVICE_ENV, _GAE_VERSION_ENV, _GAE_INSTANCE_ENV]


# Path: tests/unit/handlers/test_handlers.py
import logging
import unittest
import mock
import json
        import flask
        import logging
        import logging
        import logging
        import logging
        import logging
        import sys
        import io
from unittest.mock import patch
from google.cloud.logging_v2.handlers._monitored_resources import (
    _FUNCTION_ENV_VARS,
    _GAE_ENV_VARS,
)
        from google.cloud.logging.handlers import CloudLoggingFilter
        from google.cloud.logging.handlers import CloudLoggingHandler
        from google.cloud.logging_v2.handlers._monitored_resources import (
            _create_global_resource,
        )
        from google.cloud.logging_v2.handlers.handlers import DEFAULT_LOGGER_NAME
        from google.cloud.logging import Resource
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.resource import Resource
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging_v2.handlers.handlers import _format_and_parse_message
        from google.cloud.logging.handlers import setup_logging

        root_handlers = logging.getLogger().handlers
        self.assertIn(handler, root_handlers)

    def test_setup_logging_excludes(self):
        INCLUDED_LOGGER_NAME = "includeme"
        EXCLUDED_LOGGER_NAME = "excludeme"

        handler = _Handler(logging.INFO)
        self._call_fut(handler, (EXCLUDED_LOGGER_NAME,))

        included_logger = logging.getLogger(INCLUDED_LOGGER_NAME)
        self.assertTrue(included_logger.propagate)

        excluded_logger = logging.getLogger(EXCLUDED_LOGGER_NAME)
        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})