Python的“记录”到GCP的StackDriver连接问题

时间:2019-07-05 10:38:40

标签: python-3.x logging google-cloud-platform stackdriver

我可以通过“ google.cloud.logging”将日志条目直接推送到Stackdriver。现在,我正在尝试将Python的“日志记录”连接到GCP的Stackdriver,以便在使用python的“日志记录”时自动在Stackdriver中创建日志。连接部分是我遇到麻烦的地方。

我遇到这种情况:

这些条目被临时存储在Stackdriver中,也可以被获取,但是一旦程序结束它们就消失了。似乎它们存储在缓冲区中,但不会持久保存在Stackdriver日志中。

部分代码:

    import google.cloud.logging as gcp_logging
    from google.oauth2 import service_account
    cred= service_account.Credentials.from_service_account_file('credential/service/account/json/file')
    client = gcp_logging.Client(project = <project-id>, credentials=cred)

    import logging
    client.setup_logging(log_level=logging.DEBUG)   

    py_logger = get_python_logger()
    print("Making log entry here ...")
    py_logger.debug("log entry by lib 'logging' through Stackdriver in logger module")  

执行代码:
输出分为4个步骤:

  1. 它从Stackdriver中获取日志条目

  2. 我试图通过python的'logging'将日志输入到Stackdriver

  3. 它再次获取日志条目

  4. 根据级别过滤获取的日志

我期望从Stackdriver获取Python的“ logging”的新日志条目。但是,正在生成有关oauth2和http连接请求的其他条目。似乎是一些身份验证问题。我在GCP中检查了防火墙规则,它允许所有传入IP。

$python test.py

    fetching log entries for logname: edudata-log ...  
    [2019-07-04 17:24:43.226848][WARNING] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:25:58.156544][WARNING] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:26:19.780465][WARNING] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:27:24.196522][INFO] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:27:35.216711][INFO] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:27:44.895962][DEBUG] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:27:58.220334][DEBUG] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 19:13:51.840192][INFO] log entry through module edu_logger called in test_edulogger.py

    Making log entry here ...
    log entry by lib 'logging' through Stackdriver in edulogger module

    fetching log entries for logname: edudata-log ...
    Making request: POST https://oauth2.googleapis.com/token
    Making request: POST https://oauth2.googleapis.com/token
    Starting new HTTPS connection (1): oauth2.googleapis.com:443
    Starting new HTTPS connection (1): oauth2.googleapis.com:443
    https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 200 None
    https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 200 None
    Submitted 1 logs
    Submitted 4 logs
    [2019-07-04 17:24:43.226848][WARNING] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:25:58.156544][WARNING] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:26:19.780465][WARNING] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:27:24.196522][INFO] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:27:35.216711][INFO] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:27:44.895962][DEBUG] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:27:58.220334][DEBUG] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 19:13:51.840192][INFO] log entry through module edu_logger called in test_edulogger.py

    filtering log entries for logname = <logname>, level = DEBUG ...
    Making request: POST https://oauth2.googleapis.com/token   
    Starting new HTTPS connection (1): oauth2.googleapis.com:443
    https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 200 None
    Submitted 1 logs
    Submitted 1 logs
    [2019-07-04 17:27:44.895962][DEBUG] log entry through module edu_logger called in test_edulogger.py
    [2019-07-04 17:27:58.220334][DEBUG] log entry through module edu_logger called in test_edulogger.py

    Waiting up to 5 seconds.
    Background thread exited gracefully.
    Sent all pending logs.

其他详细信息:
1.已安装stackdriver
2.我在此处指定凭据:
os.environ [“ GOOGLE_APPLICATION_CREDENTIALS”] = SERVICE_ACCOUNT_FILE_PATH
3.我想将日志记录的日志输出存储在GCP Stackdriver日志中

0 个答案:

没有答案