[英]Logging in GCP and locally
I am building a system that is intended to run on Virtual Machines in Google Cloud Platform.我正在构建一个旨在在 Google Cloud Platform 中的虚拟机上运行的系统。 However, as a form of backup, it may be run locally as well.但是,作为一种备份形式,它也可以在本地运行。 That being said, my issue currently is with logging.话虽如此,我目前的问题是日志记录。 I have two loggers, both work, a local logger and a cloud logger.我有两个记录器,都可以工作,一个本地记录器和一个云记录器。
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)
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
I use setup_logging()
to set everything up like so:我使用setup_logging()
像这样设置一切:
setup_logging()
logger = logging.getLogger(<type_of_logger>)
can be "cloud"
or "__main__"
可以是"cloud"
或"__main__"
" main " only logs locally, "cloud" logs both to GCP Stackdriver Logs and locally. “ main ”仅在本地记录,“cloud”同时记录到 GCP Stackdriver Logs 和本地。
Now if I'm not running on GCP, an error gets thrown here:现在,如果我不在 GCP 上运行,则会在此处抛出错误:
CREDS = google.cloud.logging.Client(
project=PROJECT, credentials=service_account.Credentials.from_service_account_file(CREDENTIAL_FILE))
What is the best way around this?解决此问题的最佳方法是什么? The class GoogleLogger(CloudLoggingHandler):
always gets run, and if isn't in GCP it breaks. class GoogleLogger(CloudLoggingHandler):
总是运行,如果不在 GCP 中,它就会中断。
An idea is to wrap the class in a try/except
block, but that sounds like a horrible idea.一个想法是将类包装在try/except
块中,但这听起来是一个可怕的想法。 How do I make my code smart enough to choose which logger automatically?如何使我的代码足够智能以自动选择哪个记录器? And if running locally, completely ignore the GoogleLogger
?如果在本地运行,完全忽略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
i think problem is bad ingeritance我认为问题是不好的智慧
try this instead试试这个
class LoggingHandlerInherited(CloudLoggingHandler):
def __init__(self):
super().__init__(client=google.cloud.logging.Client())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.