尝试使用google logging 客户端库将日志写入 gcloud,具体来说,我有兴趣编写将附加到托管资源的日志,在本例中为Vertex AI端点:
代码示例:
import logging
from google.api_core.client_options import ClientOptions
import google.cloud.logging_v2 as logging_v2
from google.oauth2 import service_account
def init_module_logger(module_name: str) -> logging.Logger:
module_logger = logging.getLogger(module_name)
module_logger.setLevel(settings.LOG_LEVEL)
credentials= service_account.Credentials.from_service_account_info(json.loads(SA_KEY_JSON))
client = logging_v2.client.Client(
credentials=credentials,
client_options=ClientOptions(api_endpoint="us-east1-aiplatform.googleapis.com"),
)
handler = client.get_default_handler(
resource=Resource(
type="aiplatform.googleapis.com/Endpoint",
labels={"endpoint_id": "ENDPOINT_NUMBER_ID",
"location": "us-east1"},
)
)
#Assume we have the formatter
handler.setFormatter(ENRICHED_FORMATTER)
module_logger.addHandler(handler)
return module_logger
logger = init_module_logger(__name__)
logger.info("This Fails with 501")
我得到:
google.api_core.exceptions.MethodNotImplemented: 501 服务器上没有实现GRPC目标,主机:us-east1-aiplatform.googleapis.com,方法:/google.logging.v2.LoggingServiceV2/WriteLogEntries。发送所有待处理的日志。
我认为我们需要启用 api 并被告知它已启用,并且我们有:https://www.googleapis.com/auth/logging.write 可能导致错误的原因是什么?