【问题标题】:Stackdriver logging misses log entriesStackdriver 日志记录遗漏日志条目
【发布时间】:2019-07-26 07:01:35
【问题描述】:

我们的团队使用 python 来记录一些用户访问活动。

我们创建了本地日志记录和谷歌云日志记录 (Stackdriver) 来捕获异常。

Local log shows 5 entries

Our team's Stackdriver log shows 2 entries

我还使用自己的谷歌云堆栈驱动程序日志进行了测试。它显示了 5 次尝试。

代码如下:

local_logger = local_logging.getLogger(__name__)
local_logger.setLevel(local_logging.INFO)

handler = local_logging.FileHandler('Azure-user-access-audit-log.log')
handler.setLevel(local_logging.CRITICAL)


local_logging.Formatter.converter = time.gmtime
formatter = local_logging.Formatter('%(asctime)s | %(levelname)s | %(message)s')
handler.setFormatter(formatter)

local_logger.addHandler(handler)

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]  = "my-credential.json"
logging_client = cloud_logging.Client()
log_name = 'Azure-user-access-audit-log'
cloud_logger = logging_client.logger(log_name)

............

   if item['subcriptionType'] == 'Hacker':
        user_log = str(item['cloudName'] + " - " + item['tenantId'] + " | " +
            item['subcriptionType'] + " " + item['principalName'] + " has access to " + item['subscriptionName'] + " as "
            + item['roleDefinitionName'])
        local_logger.critical(user_log)

        # The data to log to Google Cloud
        google_log_message = item['subcriptionType'] + " " + item['principalName'] + " has access to " + item['subscriptionName'] + " as " + item['roleDefinitionName']
        google_log_severity = 'CRITICAL'
        google_log_insert_id = item['cloudName'] + " - " + item['tenantId']
        print(google_log_message)
        print(google_log_severity)
        print(google_log_insert_id)

        # Writes the log entry
        # cloud_logger.log_text(str(google_log_message), severity = str(google_log_severity), insert_id = str(google_log_insert_id))
        #             # cloud_logger.log_struct({
        #             #     'subcriptionType': item['subcriptionType'],
        #             #     'principalName': item['principalName'],
        #             #     'subscriptionName': item['subscriptionName']
        #             # }, severity = str(google_log_severity), insert_id = str(google_log_insert_id))
        cloud_logger.log_text(str(google_log_message))

如果我为严重性和插入 ID 添加了注释掉的代码,则什么都不会通过。我很确定语法很好。

请帮帮我。 非常感谢大家

【问题讨论】:

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


    【解决方案1】:

    您错误地使用了insertId。 Stackdriver 日志记录 API considers all log entries in the same project, with the same timestamp, and with the same insertId to be duplicates which can be removed。你所有的insertId 值似乎都是一样的。您在 Stackdriver Logging 中看到两个条目而不是一个条目的唯一原因是确实通过的两个条目具有不同的时间戳。

    您可以省略 insertId 字段。 API 会自动设置一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 2018-07-29
      • 2022-07-03
      • 1970-01-01
      • 2020-08-21
      • 2014-09-27
      相关资源
      最近更新 更多