如何让 Rails 添加行号/时间戳来记录消息?
我在开发 Rails 应用程序时使用 tail -f
显示日志文件。 它显示日志消息(彩色!:),这很棒。
但是,由于 80 宽度的控制台中有如此多的信息,当我单击按钮获取资源时,很难跟踪特定“组”日志消息的开始位置。
如果每条日志消息/行的开头都有行号甚至时间戳,那就更容易了。这样我就可以记住我需要开始查看日志“第 2365 行之后”或“2010/10/10 23:33:23:45 之后”。
这可以吗? Rails 有一些内部选项吗?
I use tail -f
to display the log file when developing my Rails app.
It shows the log messages (in color! :), which is great.
But with so much information in the 80-width console, it becomes difficult to track where a certain "set" of log messages started when, say, I clicked on a button to GET a resource.
It would be easier if there was a line number or even a time stamp at the start of each log message/line. This way I could remember that I need to start looking at the log "after line number 2365" or "after 2010/10/10 23:33:23:45".
Is this possible to do? Is there some Rails internal option for this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你为什么不直接编辑你想要的环境的日志标签
development.rb
why don't you just edit your desired environment's log tags
development.rb
如果您想获得时间戳:
并按照您认为合适的方式格式化日期...
这适用于 Rails 2.1 +,之前您可以使用常量访问
ActiveSupport::Buffered
日志对象:RAILS_DEFAULT_LOGGER
使用
Rails.logger.instance_values["log"]
访问实际日志文件 获取行数很困难,因为记录器仅打开文件进行写入,可能是为了经济。当我尝试时,我收到一个
IOError: notopened for read
。`
If you wanted to get a time stamp:
And format the date however you see fit....
That would work for Rails 2.1 +, prior you could access the
ActiveSupport::Buffered
log object with the constant:RAILS_DEFAULT_LOGGER
Get access to the actual log file with
Rails.logger.instance_values["log"]
Getting the number of lines is difficult because the logger only opens the file for writing, probably for economy. I get an
IOError: not opened for reading
when I try.`
谢谢@scaney。
我在此处找到了解决方案。
我修改了该代码以添加我自己的着色突出显示(当然仅用于开发!),现在我可以在控制台中看到诸如黄色的“参数”之类的内容,我现在非常高兴!
如果有人感兴趣,这里是我放在
environment.rb
末尾的代码。这是我当前的(脏)实现。稍后可能会修复这个问题(也许会制作一个宝石,但现在这对我来说很好)
警告
肮脏的代码如下!使用风险自负!
Thanks @scaney.
I found a solution here.
I modified that code to add my own coloring highlights (for development only of course!) and now I can see things like 'parameters' in yellow in the console and I'm very pleased now!
In case someone is interested, here is the code I put at the end of
environment.rb
.Here is my current (dirty) implementation. Will probably fix this up later (maybe make a gem, but for now this serves me fine)
WARNING
DIRTY CODE FOLLOWS! Use at your own risk!