Ruby 2.3.6、Rails 4.2.6、Stackdriver 0.15.0
按照https://cloud.google.com/logging/docs/setup/ruby上的说明,我已将stackdrivergem 添加到Gemfile. 据我所知,我不需要做任何其他事情。
但是,我没有在 Stackdriver 中看到来自 Rails 的任何日志消息。我什至尝试exec在容器中运行一个 shell,运行bundle exec rails console,并使用Rails.logger.error "this is a log message".
config.google_cloud.use_logging = true和config.log_level = :debug, FWIW。
更新:我仔细查看了记录器实例pp Rails.logger,它包含以下内容:
@resource=
#<Google::Cloud::Logging::Resource:0x0000558b075d1f50
@labels={:cluster_name=>"onehq-cluster", :namespace_id=>"default"},
@type="container">,
我的容器在staging命名空间中运行,这可以解释为什么我没有看到日志消息。当我查看命名空间中的 Stackdriver 日志查看器时default,瞧,有日志。不幸的是,发送所有消息对default我没有任何好处,因为我的应用程序在两个命名空间中运行,我需要能够区分它们。
我尝试添加其他配置:
config.google_cloud.logging.monitored_resource.type = "container"
config.google_cloud.logging.monitored_resource.labels = { cluster_name: "my-cluster", namespace_id: "staging" }
现在Rails.logger.error "this is a log message"没有出现在任何地方。
更新 2:经过大量网络搜索后,我设法找到了https://github.com/GoogleCloudPlatform/google-cloud-ruby/issues/2025,这表明发送日志消息的命名空间由环境变量,并且说 env var 没有在 GKE 容器中自动设置。(据说它已由https://github.com/GoogleCloudPlatform/google-cloud-ruby/pull/2071解决,但我认为这还没有生效,至少不在我的集群中。)我会尝试强制env var 在我的部署文件中,如果可行,我会将其最后一部分转换为答案。