def test_log_handler_async(self): LOG_MESSAGE = "It was the worst of times" handler_name = self._logger_name("handler_async") handler = CloudLoggingHandler(Config.CLIENT, name=handler_name) # only create the logger to delete, hidden otherwise logger = Config.CLIENT.logger(handler_name) self.to_delete.append(logger) cloud_logger = logging.getLogger(handler.name) cloud_logger.addHandler(handler) cloud_logger.warn(LOG_MESSAGE) handler.flush() entries = _list_entries(logger) expected_payload = {"message": LOG_MESSAGE, "python_logger": handler.name} self.assertEqual(len(entries), 1) self.assertEqual(entries[0].payload, expected_payload)
def _initialize_cloud_clients(): """Initialize clients for Google Cloud Logging and Error reporting.""" assert not utils.is_local() global _log_client if _log_client: return _log_client = google.cloud.logging.Client() logging_handler = CloudLoggingHandler(_log_client) logging.getLogger().addHandler(logging_handler) global _error_reporting_client _error_reporting_client = error_reporting.Client()
def test_log_handler_async(self): LOG_MESSAGE = 'It was the worst of times' handler_name = 'gcp-async' + unique_resource_id('-') handler = CloudLoggingHandler(Config.CLIENT, name=handler_name) # only create the logger to delete, hidden otherwise logger = Config.CLIENT.logger(handler_name) self.to_delete.append(logger) cloud_logger = logging.getLogger(handler.name) cloud_logger.addHandler(handler) cloud_logger.warn(LOG_MESSAGE) handler.flush() entries = _list_entries(logger) expected_payload = { 'message': LOG_MESSAGE, 'python_logger': handler.name } self.assertEqual(len(entries), 1) self.assertEqual(entries[0].payload, expected_payload)
def setup_stackdriver(log_level, name, log_format, excluded_loggers=None): try: from google.cloud.logging import Client from google.cloud.logging import handlers as google_logging_handlers from google.cloud.logging.handlers.handlers import \ EXCLUDED_LOGGER_DEFAULTS, \ CloudLoggingHandler except ImportError: raise ValueError("google-cloud-logging is not properly installed") if not excluded_loggers: excluded_loggers = EXCLUDED_LOGGER_DEFAULTS client = Client() # the docstring of CloudLoggingHandler point to client instead of Client # noinspection PyTypeChecker handler = CloudLoggingHandler(client, name) handler.setFormatter(logging.Formatter(log_format, None, "%")) google_logging_handlers.setup_logging(handler, log_level=log_level, excluded_loggers=excluded_loggers)
def test_log_handler_async(self): LOG_MESSAGE = 'It was the worst of times' handler = CloudLoggingHandler(Config.CLIENT) # only create the logger to delete, hidden otherwise logger = Config.CLIENT.logger(handler.name) self.to_delete.append(logger) cloud_logger = logging.getLogger(handler.name) cloud_logger.addHandler(handler) cloud_logger.warn(LOG_MESSAGE) entries, _ = self._list_entries(logger) JSON_PAYLOAD = {'message': LOG_MESSAGE, 'python_logger': handler.name} self.assertEqual(len(entries), 1) self.assertEqual(entries[0].payload, JSON_PAYLOAD)
def test_log_root_handler(self): LOG_MESSAGE = 'It was the best of times.' handler = CloudLoggingHandler(Config.CLIENT, name=self._logger_name()) # only create the logger to delete, hidden otherwise logger = Config.CLIENT.logger(handler.name) self.to_delete.append(logger) google.cloud.logging.handlers.handlers.setup_logging(handler) logging.warn(LOG_MESSAGE) entries, _ = self._list_entries(logger) JSON_PAYLOAD = {'message': LOG_MESSAGE, 'python_logger': 'root'} self.assertEqual(len(entries), 1) self.assertEqual(entries[0].payload, JSON_PAYLOAD)
def test_log_root_handler(self): LOG_MESSAGE = "It was the best of times." handler = CloudLoggingHandler(Config.CLIENT, name=self._logger_name("handler_root")) # only create the logger to delete, hidden otherwise logger = Config.CLIENT.logger(handler.name) self.to_delete.append(logger) google.cloud.logging.handlers.handlers.setup_logging(handler) logging.warn(LOG_MESSAGE) entries = _list_entries(logger) expected_payload = {"message": LOG_MESSAGE, "python_logger": "root"} self.assertEqual(len(entries), 1) self.assertEqual(entries[0].payload, expected_payload)
def test_log_handler_sync(self): LOG_MESSAGE = 'It was the best of times.' handler = CloudLoggingHandler(Config.CLIENT, name=self._logger_name(), transport=SyncTransport) # only create the logger to delete, hidden otherwise logger = Config.CLIENT.logger(handler.name) self.to_delete.append(logger) LOGGER_NAME = 'mylogger' cloud_logger = logging.getLogger(LOGGER_NAME) cloud_logger.addHandler(handler) cloud_logger.warn(LOG_MESSAGE) entries, _ = self._list_entries(logger) JSON_PAYLOAD = {'message': LOG_MESSAGE, 'python_logger': LOGGER_NAME} self.assertEqual(len(entries), 1) self.assertEqual(entries[0].payload, JSON_PAYLOAD)
def test_log_handler_sync(self): LOG_MESSAGE = "It was the best of times." handler_name = self._logger_name("handler_sync") handler = CloudLoggingHandler( Config.CLIENT, name=handler_name, transport=SyncTransport ) # only create the logger to delete, hidden otherwise logger = Config.CLIENT.logger(handler.name) self.to_delete.append(logger) LOGGER_NAME = "mylogger" cloud_logger = logging.getLogger(LOGGER_NAME) cloud_logger.addHandler(handler) cloud_logger.warn(LOG_MESSAGE) entries = _list_entries(logger) expected_payload = {"message": LOG_MESSAGE, "python_logger": LOGGER_NAME} self.assertEqual(len(entries), 1) self.assertEqual(entries[0].payload, expected_payload)
def log_handler(self, log_name): try: from google.cloud.logging.handlers.handlers import CloudLoggingHandler from google.cloud.logging.resource import Resource from google.cloud.logging import Client except ImportError: return super(ComputeEngine, self).log_handler(log_name) instance_id = self.get_from_metadata('instance/id') zone = self.get_from_metadata('instance/zone') if not instance_id or not zone: return super(ComputeEngine, self).log_handler(log_name) resource = Resource(type='gce_instance', labels={ 'project_id': self.project_id, 'instance_id': instance_id, 'zone': zone.split('/')[-1], }) client = Client(_use_grpc=False) return CloudLoggingHandler(client, name=log_name, resource=resource)
import string import random worker_id = ''.join(random.choice(string.ascii_letters) for _ in range(10)) import google.cloud.logging from google.cloud.logging.handlers.handlers import CloudLoggingHandler, EXCLUDED_LOGGER_DEFAULTS google_logging_client = google.cloud.logging.Client() stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setFormatter( Formatter( f'%(asctime)s {worker_id} [%(name)s:%(lineno)-3d] %(levelname)-7s: %(message)s' )) google_handler = CloudLoggingHandler(google_logging_client) google_handler.setFormatter( Formatter( f'{worker_id} [%(name)s:%(lineno)-3d] %(levelname)-7s: %(message)s' )) root_logger = logging.getLogger() root_logger.setLevel(logging.INFO) root_logger.addHandler(stdout_handler) root_logger.addHandler(google_handler) for logger_name in EXCLUDED_LOGGER_DEFAULTS: logger = logging.getLogger(logger_name) logger.propagate = False logger.addHandler(stdout_handler)