我正在构建一个旨在在 Google Cloud Platform 中的虚拟机上运行的系统。但是,作为一种备份形式,它也可以在本地运行。话虽如此,我目前的问题是日志记录。我有两个记录器,都可以工作,一个本地记录器和一个云记录器。
云记录器
import google.cloud.logging
from google.cloud.logging.handlers import CloudLoggingHandler
from google.oauth2 import service_account
CREDS = google.cloud.logging.Client(
project=PROJECT, credentials=service_account.Credentials.from_service_account_file(CREDENTIAL_FILE))
class GoogleLogger(CloudLoggingHandler):
def __init__(self, client=CREDS):
super(GoogleLogger, self).__init__(client)
def setup_logging():
"""
This function can be invoked in order to setup logging based on a yaml config in the
root dir of this project
"""
try:
# with open('logging.yaml', 'rt') as f:
with open(LOGGING_CONFIG, 'rt') as f:
config = yaml.safe_load(f.read())
f.close()
logging.config.dictConfig(config)
except Exception:
print('Error in Logging Configuration. Using default configs')
print(traceback.format_exc())
logging.basicConfig(level=logging.INFO)
日志记录.yaml
version: 1
formatters:
simple:
format: "%(name)s - %(lineno)d - %(message)s"
complex:
format: "%(asctime)s - %(name)s | %(levelname)s | %(module)s : [%(filename)s: %(lineno)d] - %(message)s"
json:
class: logger.JsonFormatter
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: complex
cloud:
class: logger.GoogleLogger
formatter: json
level: INFO
loggers:
cloud:
level: INFO
handlers: [console,cloud]
propagate: yes
__main__:
level: DEBUG
handlers: [console]
propagate: yes
我使用 setup_logging()
像这样设置所有内容:
setup_logging()
logger = logging.getLogger(<type_of_logger>)
可以是 "cloud"
或 "__main__"
“main”仅记录本地日志,“cloud”日志同时记录到 GCP Stackdriver Logs 和本地。
现在,如果我不在 GCP 上运行,则会在此处抛出错误:
CREDS = google.cloud.logging.Client(
project=PROJECT, credentials=service_account.Credentials.from_service_account_file(CREDENTIAL_FILE))
解决这个问题的最佳方法是什么? class GoogleLogger(CloudLoggingHandler):
始终运行,如果不在 GCP 中,它就会中断。
一个想法是将类包装在 try/except
block 中,但这听起来很糟糕。如何使我的代码足够智能以自动选择哪个记录器?如果在本地运行,完全忽略 GoogleLogger
?
编辑(回溯)
File "import_test.py", line 2, in <module>
from logger import setup_logging
File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 16, in <module>
class GoogleLogger(CloudLoggingHandler):
File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 23, in GoogleLogger
project=PROJECT, credentials=service_account.Credentials.from_service_account_file(CREDENTIAL_FILE))
File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/client.py", line 123, in __init__
self._connection = Connection(self, client_info=client_info)
File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/_http.py", line 39, in __init__
super(Connection, self).__init__(client, client_info)
TypeError: __init__() takes 2 positional arguments but 3 were given
最佳答案
这是由于错误的继承。
尝试将客户端传递给父级的__init__
class LoggingHandlerInherited(CloudLoggingHandler):
def __init__(self):
super().__init__(client=google.cloud.logging.Client())
确保您的环境中有 GOOGLE_APPLICATION_CREDENTIALS
关于python - 登录 GCP 和本地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59545446/