登录到GCP或本地 [英] Logging on either GCP or local

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

问题描述

假设有一个在GCP上运行的系统,但是作为备份,可以在本地运行.

Suppose there is a system that is run on GCP, but as a backup, can be run locally.

在云上运行时,stackdriver非常简单.

When running on the cloud, stackdriver is pretty straightforward.

但是,如果在云上,或者如果不在云上,我需要我的系统推送到stackdriver,使用本地python记录器.

However, I need my system to push to stackdriver if on the cloud, and if not on the cloud, use the local python logger.

我也不想包含任何逻辑,这应该是自动的.

I also don't want to include any logic to do so, and this should be automatic.

  1. 记录时,直接登录到Python/本地记录器.
  2. 如果在GCP上->将其推送到stackdriver.

我可以编写可以实现此目的的逻辑,但这是不好的做法.当然,有一种直接的方法可以使它起作用.

I can write logic that could implement this but that is bad practice. There surely is a direct way of getting this to work.

import google.cloud.logging

client = google.cloud.logging.Client()
client.setup_logging()


import logging
cl = logging.getLogger()
file_handler = logging.FileHandler('file.log')
cl.addHandler(file_handler)
logging.info("INFO!")

这基本上将登录到python记录器,然后始终"上传到云记录器.我如何拥有它,这样我就不需要显式添加import google.cloud.logging了,基本上,如果安装了stackdriver,它将直接获取日志?那有可能吗?如果不能,那么可以从最佳实践的角度来解释这将如何处理?

This will basically log to python logger, and then 'always' upload to cloud logger. How can I have it so that I don't need to explicitly add import google.cloud.logging and basically if stackdriver is installed, it directly gets the logs? Is that even possible? If not can someone explain how this would be handled from a best practices perspective?

创建的/etc/google-fluentd/config.d/workflow_log.conf

<source>
    @type tail
    format none
    path /home/daudn/this_log.log
    pos_file /var/lib/google-fluentd/pos/this_log.pos
    read_from_head true
    tag workflow-log
</source>

创建的/var/log/this_log.log

pos_file /var/lib/google-fluentd/pos/this_log.pos存在

pos_file /var/lib/google-fluentd/pos/this_log.pos exists

import logging
cl = logging.getLogger()
file_handler = logging.FileHandler('/var/log/this_log.log')
file_handler.setFormatter(logging.Formatter("%(asctime)s;%(levelname)s;%(message)s"))
cl.addHandler(file_handler)

logging.info("info_log")
logging.error("error_log")

这有效!查找特定虚拟机的日志,而不是global> python

This works! Look for your logs for the specific VM and not global>python

推荐答案

您无需在程序中专门启用或使用Stackdriver.您可以使用Python记录器并写入所需的任何文件.但是,Stackdriver只记录特定的日志文件.这意味着您将需要手动设置Stackdriver来记录您的"日志文件.

You do not need to specifically enable or use Stackdriver in your program. You can use the Python logger and write to any file you want. However, Stackdriver only logs specific log files. This means that you would need to manually set up Stackdriver to log "your" log files.

在您的示例中,您正在写file.log.修改/etc/google-fluentd/config.d/mylogfile.conf以包括以下内容.您将需要指定file.log的完整路径,而不仅仅是文件名.在此示例中,我将其命名为/var/log/mylogfile.log.此示例还假设您的日志的每一行都以日期开头.

In your example, you are writing to file.log. Modify /etc/google-fluentd/config.d/mylogfile.conf to include the following. You will need to specify the full path for file.log and not just the file name. In this example, I named it /var/log/mylogfile.log. This example also assumes that your logs start each line with a date.

<source>
  @type tail

  # Parse the timestamp, but still collect the entire line as 'message'
  format /^(?<message>(?<time>[^ ]*\s*[^ ]* [^ ]*) .*)$/

  path /var/log/mylogfile.log
  pos_file /var/lib/google-fluentd/pos/mylogfile.log.pos
  read_from_head true
  tag auth
</source>

有关更多信息,请阅读以下文档:

For more information read the following document:

堆栈驱动程序-配置代理

现在,您的程序将在GCP外部运行,并且在配置的实例上运行时,登录到Stackdriver.

Now your program will run outside GCP and when running on a configured instance, log to Stackdriver.

注意:我会做与您要求相反的操作.我将始终使用Stackdriver.当不在GCP中运行时,我会在台式机,本地服务器等上手动设置Stackdriver,然后继续登录到Stackdriver.

Note: I would do the opposite of what you have asked. I would always use Stackdriver. When not running in GCP I would manually set up Stackdriver on my desktop, local server, etc and continue to log to Stackdriver.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆