rails 取前10条记录
If you’ve worked on Rails, you would’ve noticed that the default Rails logs are true to the name: They run too long and are spread across multiple lines.
如果您使用过Rails,您会注意到默认的Rails日志符合名称:它们运行时间过长,并且分布在多行中。
Even if the context of processing is just one simple controller action, the Rails logs generally tend to be verbose.
即使处理的上下文只是一个简单的控制器操作,Rails日志通常也趋于冗长。
Read on to know how one can sanitise the logs without losing out on information and how additional information could be added for log lines to completely utilise the querying features of the logging platform.
请继续阅读,以了解如何在不丢失信息的情况下清理日志,以及如何为日志行添加其他信息以完全利用日志记录平台的查询功能。
考虑这个例子 (Consider this example)
A simple health check controller which you would add to make the health check pass for your Rails app deployed on Kubernetes.
您将添加一个简单的运行状况检查控制器,以使Kubernetes上部署的Rails应用程序通过运行状况检查。
The config (shown for the development environment for this example):
配置(在此示例的开发环境中显示):
A simple route for the GET
verb, to call the #ping
action in the HealthCheckController
:
GET
动词的简单路由,用于在HealthCheckController
调用#ping
动作:
This is what the logs would look like for the route ping
:
这是路由ping
的日志内容:
我们的观察 (Our observations)
- The logs are spread over multiple lines, adding to the complexity in debugging the whole request or response flow. 日志分布在多行中,这增加了调试整个请求或响应流的复杂性。
Given you would be pushing to your logging platform, let’s say EFK, which would allow full-text search, the configuration to have the request-id for each log would come in handy — which is better than not having anything at all. (Have a look at BaritoLog, our in house EFK platform).
假设您要使用自己的日志记录平台,比方说EFK,它将允许全文搜索,那么配置每个日志的request-id会很方便-胜于一点都不做。 (看看 我们的内部EFK平台 BaritoLog ) 。
If nothing is being pushed to any logging platform, then you would be debugging this by tailing the logs of the rails app somewhere. If deployed to Kubernetes, doing a kubetail or if on VMs, and you tail the logs of each and every application server instance. But then, the first step here would be to have centralized logging.
如果什么都没有推送到任何日志记录平台,那么您将通过将rails应用程序的日志拖到某处进行调试。 如果部署到Kubernetes,执行kubetail或在VM上运行, 则将跟踪每个应用程序服务器实例的日志。 但是,这里的第一步将是进行集中日志记录。
- Extremely verbose logs which hinder debugging. 日志非常冗长,阻碍了调试。
No clear way to parse the logs as the logs format is not standard (if you don’t add
config.log_tags = [:request_id],
which is not present by default).没有明确的方法来解析日志,因为日志格式不是标准的(如果不添加
config.log_tags = [:request_id],
默认情况下不存在)。
改善Rails日志 (Improving the Rails Logs)
By using Lograge, we can make the log compact so it stays meaningful. There are a bunch of other alternatives like Semantic Logger and Logstasher. My reasoning behind going ahead with Lograge is that it has been around for some time now and has a larger adoption.
通过使用Lograge ,我们可以使日志紧凑,从而保持有意义。 还有很多其他选择,例如语义记录器和Logstasher 。 我继续使用Lograge的原因是它已经存在了一段时间,并且得到了更大的采用。
Now, add Lograge in your Gemfile
:
现在,在您的Gemfile
添加Gemfile
:
...
gem 'lograge', '~> 0.11.2'
...
Do a $ bundle
, after which you need to add setup the configuration as follows:
做一个$ bundle
,之后您需要添加设置,如下所示:
config.lograge.base_controller_class = ['ActionController::Base']
assumes that each controller will be inheriting from ActionController::Base.
Any other controller which doesn’t inherit from the Base controller can be included, in order for Lograge to pick it up.
config.lograge.base_controller_class = ['ActionController::Base']
假定每个控制器都将从ActionController::Base.
继承ActionController::Base.
可以包括任何其他不继承自Base控制器的控制器,以便Lograge可以将其拾取。
Do the following:
请执行下列操作:
And check the logs of your Rails app:
并检查您的Rails应用程序的日志:
Here, the whole log line captures a lot of metadata, so that each log line can be debugged. One of the keys present in the log line is request_id
, which is a unique trace ID for a particular request. This can do a full text search on the logging platform, if it supports it.
在这里,整个日志行捕获了大量元数据,因此可以调试每个日志行。 日志行中存在的键之一是request_id
,它是特定请求的唯一跟踪ID。 如果支持的话,可以在日志平台上进行全文搜索。
To extend this further, capture the request_id
, and pass it along the each controller’s flow. To capture it, you can simply do a request.request_id
. Not that you have the request_id
, if you are doing a Rails.logger.{info|debug}
you can use it to log the request_id
. This way, the request_ids generated would also be propagated to the custom logs which would be added to the application.
为了进一步扩展它,捕获request_id
,并将其沿着每个控制器的流程传递。 要捕获它,您只需执行request.request_id
。 这并不是说你有request_id
,如果你正在做一个Rails.logger.{info|debug}
你可以用它来记录request_id
。 这样,生成的request_ids也将传播到将添加到应用程序的自定义日志。
The biggest advantage here is this request_id
can be added to every core flow which would be logged. Just put the request_id
in the search param of the logging platform, and it will give all the logs which have this key in it.
这里最大的好处是可以将此request_id
添加到将要记录的每个核心流中。 只需将request_id
放在日志记录平台的搜索参数中,它将提供所有包含此关键字的日志。
Additional information like host, remote_ip, ip of the rails app servicing it would also be captured, by simply doing
request.host
,request.remote_ip
,request.ip
,request.request_id
.通过简单地执行
request.host
,request.remote_ip
,request.ip
,request.request_id
,也可以捕获其他信息,例如为其提供服务的rails应用程序的host,remote_ip,ip。
But remember, Lograge cannot be used for the custom logger because it doesn’t support this.
但是请记住,Lograge无法用于自定义记录器,因为它不支持 this 。
为自定义日志添加结构化日志 (Adding structured logging for custom logs)
A custom logger can be added for your application and initialised in the application config for the preferred environments.
可以为您的应用程序添加自定义记录器,并在首选环境的应用程序配置中对其进行初始化。
The only thing that needs to be added is:
唯一需要添加的是:
Initialise it in the application config (development, in this case):
在应用程序配置中初始化它(在这种情况下为开发):
And to test the above:
并测试以上内容:
此后日志将是什么样? (What will the logs look like after this?)
As you can see, the logs from
如您所见,日志来自
- the controller logs which the Lograge is showing 控制器记录Lograge显示的内容
the
Rails.logger.info
is spittingRails.logger.info
随地吐痰
have…
有…
- logs in json format 以json格式登录
log line has the
request_id
appended to it in the message key日志行在消息键中附加了
request_id
Hoping this blog helps you make your Rails logs contextual and less voluble.
希望该博客可以帮助您使Rails日志具有上下文相关性,并且不那么麻烦。
翻译自: https://blog.gojekengineering.com/structured-logging-in-rails-75e9a8c5370b
rails 取前10条记录