def get_default_handler(self, **kw): """Return the default logging handler based on the local environment. Args: kw (dict): keyword args passed to handler constructor Returns: logging.Handler: The default log handler based on the environment """ monitored_resource = kw.pop("resource", detect_resource(self.project)) if isinstance(monitored_resource, Resource): if monitored_resource.type == _GAE_RESOURCE_TYPE: return CloudLoggingHandler(self, resource=monitored_resource, **kw) elif monitored_resource.type == _GKE_RESOURCE_TYPE: return StructuredLogHandler(**kw, project_id=self.project) elif monitored_resource.type == _GCF_RESOURCE_TYPE: # __stdout__ stream required to support structured logging on Python 3.7 kw["stream"] = kw.get("stream", sys.__stdout__) return StructuredLogHandler(**kw, project_id=self.project) elif monitored_resource.type == _RUN_RESOURCE_TYPE: return StructuredLogHandler(**kw, project_id=self.project) return CloudLoggingHandler(self, resource=monitored_resource, **kw)
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 get_handler(logName): kwargs = {} try: kwargs['resource'] = get_log_resource_for_gce_instance() except: # Probably not on GCE ;-) pass # TODO: When we launched celery workers using prefork (multiprocessing: separate process per worker) # we found that from google.cloud.logging.handlers.transports.background_thread.BackgroundThreadTransport # stopped transmitting logs to GCP. We're not sure why, but as a workaround we switched to using # a SyncTransport sub-class. handler = CloudLoggingHandler(Client(), name=logName, transport=StructlogTransport, **kwargs) handler.setFormatter(jsonlogger.JsonFormatter()) return handler
def init(log_id="Unknown"): # pragma: no cover resource = Resource( type="cloud_function", labels={ "function_name": gcloud.get_function_name(), "project_id": gcloud.get_project(), "region": gcloud.get_function_region(), }, ) client = google.cloud.logging.Client() logging.getLogger().handlers = [] handler = CloudLoggingHandler(client, resource=resource, labels={"log_id": str(log_id)}) logging.getLogger().addHandler(handler)
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 get_default_handler(self, **kw): """Return the default logging handler based on the local environment. Args: kw (dict): keyword args passed to handler constructor Returns: logging.Handler: The default log handler based on the environment """ gke_cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME) if (_APPENGINE_FLEXIBLE_ENV_VM in os.environ or _APPENGINE_INSTANCE_ID in os.environ): return AppEngineHandler(self, **kw) elif gke_cluster_name is not None: return ContainerEngineHandler(**kw) else: return CloudLoggingHandler(self, **kw)
def get_default_handler(self, **kw): """Return the default logging handler based on the local environment. Args: kw (dict): keyword args passed to handler constructor Returns: logging.Handler: The default log handler based on the environment """ monitored_resource = kw.pop("resource", detect_resource(self.project)) if (isinstance(monitored_resource, Resource) and monitored_resource.type == _GAE_RESOURCE_TYPE): return AppEngineHandler(self, **kw) elif (isinstance(monitored_resource, Resource) and monitored_resource.type == _GKE_RESOURCE_TYPE): return ContainerEngineHandler(**kw) else: return CloudLoggingHandler(self, resource=monitored_resource, **kw)
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 get_default_handler(self, **kw): """Return the default logging handler based on the local environment. Args: kw (dict): keyword args passed to handler constructor Returns: logging.Handler: The default log handler based on the environment """ monitored_resource = kw.pop("resource", detect_resource(self.project)) if isinstance(monitored_resource, Resource): if monitored_resource.type == _GAE_RESOURCE_TYPE: return AppEngineHandler(self, **kw) elif monitored_resource.type == _GKE_RESOURCE_TYPE: return ContainerEngineHandler(**kw) elif (monitored_resource.type == _GCF_RESOURCE_TYPE and sys.version_info[0] == 3 and sys.version_info[1] >= 8): # Cloud Functions with runtimes > 3.8 supports structured logs on standard out # 3.7 should use the standard CloudLoggingHandler, which sends logs over the network. return StructuredLogHandler(**kw, project_id=self.project) elif monitored_resource.type == _RUN_RESOURCE_TYPE: return StructuredLogHandler(**kw, project_id=self.project) return CloudLoggingHandler(self, resource=monitored_resource, **kw)
def get_logger(logger_name='stocker') -> Logger: foramt = Formatter("%(asctime)s %(levelname)s: %(message)s") s_handler = StreamHandler() s_handler.setFormatter(foramt) s_handler.setLevel(INFO) # client = Client(project=ProjectConfig.ID, # credentials=ProjectConfig.CREDENTIAL) client = Client() g_handler = CloudLoggingHandler(client) g_handler.setFormatter(foramt) g_handler.setLevel(DEBUG) logger = getLogger(logger_name) logger.setLevel(DEBUG) logger.addHandler(g_handler) logger.propagate = False return logger