登录GCP和本地

Logging in GCP and locally(登录GCP和本地)

本文介绍了登录GCP和本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个旨在运行在Google Cloud平台中的虚拟机上的系统。但是,作为一种备份形式,它也可以在本地运行。这就是说,我目前的问题是关于日志。我有两个记录器,都在工作,一个是本地记录器,一个是云记录器。

云日志

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)

logging.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"仅在本地记录,"云"记录到GCP Stackdriver日志和本地。

现在,如果我没有在GCP上运行,这里会抛出一个错误:

CREDS = google.cloud.logging.Client(
        project=PROJECT, credentials=service_account.Credentials.from_service_account_file(CREDENTIAL_FILE))

解决此问题的最佳方法是什么?class GoogleLogger(CloudLoggingHandler):总是运行,如果不在GCP中,它就会中断。

一个想法是将类包装在try/except挡路中,但这听起来是个可怕的想法。如何使我的代码足够智能,以便自动选择哪个记录器?如果在本地运行,则完全忽略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

这篇关于登录GCP和本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:登录GCP和本地

基础教程推荐