如何让 Rails 添加行号/时间戳来记录消息?

发布于 2024-10-09 11:41:48 字数 264 浏览 0 评论 0原文

我在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

¢好甜 2024-10-16 11:41:48

你为什么不直接编辑你想要的环境的日志标签

development.rb

config.log_tags [ lambda {|r| DateTime.now } ]

why don't you just edit your desired environment's log tags

development.rb

config.log_tags [ lambda {|r| DateTime.now } ]
硪扪都還晓 2024-10-16 11:41:48

如果您想获得时间戳:

class ApplicationController < ActionController::Base
  # ...
  before_filter :log_tracker

  def log_tracker
    Rails.logger.add(1, "Log Date: #{DateTime.now}")
  end
end

并按照您认为合适的方式格式化日期...

这适用于 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:

class ApplicationController < ActionController::Base
  # ...
  before_filter :log_tracker

  def log_tracker
    Rails.logger.add(1, "Log Date: #{DateTime.now}")
  end
end

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.
`

感受沵的脚步 2024-10-16 11:41:48

谢谢@scaney。

我在此处找到了解决方案。

我修改了该代码以添加我自己的着色突出显示(当然仅用于开发!),现在我可以在控制台中看到诸如黄色的“参数”之类的内容,我现在非常高兴!

如果有人感兴趣,这里是我放在 environment.rb 末尾的代码。
这是我当前的(脏)实现。稍后可能会修复这个问题(也许会制作一个宝石,但现在这对我来说很好)

警告

肮脏的代码如下!使用风险自负!

module ActiveSupport
  class BufferedLogger

    #define the ANSI escape codes for normal and bright colors
    $my_my_ansi_colors = {

      :normal => "\x1B[0m",

      :black => "\x1B[30m",
      :red => "\x1B[31m", #red
      :green => "\x1B[32m",
      :yellow => "\x1B[33m",
      :blue => "\x1B[34m",
      :magenta => "\x1B[35m",
      :cyan => "\x1B[36m",
      :white => "\x1B[37m",

      :bred => "\x1B[1m\x1B[31m", #bright red
      :bgreen => "\x1B[1m\x1B[32m",
      :byellow => "\x1B[1m\x1B[33m",
      :bblue => "\x1B[1m\x1B[34m",
      :bmagenta => "\x1B[1m\x1B[35m",
      :bcyan => "\x1B[1m\x1B[36m",
      :bwhite => "\x1B[1m\x1B[37m",
    }

    #take a string and using the keys in the hash, replace the keys in the 
    #string but surround the keys with ANSI color codes
    #No idea how to retain the case of the key!(TODO someday)
    def my_highlight msgx,hash
      return msgx if msgx.blank?
      return msgx if hash.empty?
      hash.each_pair do |k,v|
        if not k.nil?
          msgx.gsub! Regexp.new(k, Regexp::IGNORECASE), $my_my_ansi_colors[:normal]+$my_my_ansi_colors[v]+k.upcase+$my_my_ansi_colors[:normal]
        end
      end
      msgx
    end


    def add(severity, message = nil, progname = nil, &block)
      return if @level > severity

      message = (message || (block && block.call) || progname).to_s

      #INSERT BEGINS
      if not $myownglobalnumbercounter.nil?
        $myownglobalnumbercounter += 1
      else
        $myownglobalnumbercounter = 1
      end

      level = {
        0 => "DEBUG",
        1 => "INFO",
        2 => "WARN",
        3 => "ERROR",
        4 => "FATAL"
      }[severity] || "U"

      message = "\x1B[0m[%d %s] : %s" % [$myownglobalnumbercounter,level,message]
      message = my_highlight message, {
        "debug" => :white,
        "error" => :bred,
        "info" => :bwhite,
        "warning" => :byellow,
        "warn" => :byellow ,
        "parameters" => :byellow,
        "#" => :bgreen,
        "ms " => :bmagenta,
        "GET " => :bmagenta,
        "PUT " => :bmagenta,
        "POST " => :bmagenta,
        "DELETE " => :bmagenta
        }
      #INSERT ENDS

      message = "#{message}\n" unless message[-1] == ?\n
      buffer << message
      auto_flush
      message
    end
  end
end

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!

module ActiveSupport
  class BufferedLogger

    #define the ANSI escape codes for normal and bright colors
    $my_my_ansi_colors = {

      :normal => "\x1B[0m",

      :black => "\x1B[30m",
      :red => "\x1B[31m", #red
      :green => "\x1B[32m",
      :yellow => "\x1B[33m",
      :blue => "\x1B[34m",
      :magenta => "\x1B[35m",
      :cyan => "\x1B[36m",
      :white => "\x1B[37m",

      :bred => "\x1B[1m\x1B[31m", #bright red
      :bgreen => "\x1B[1m\x1B[32m",
      :byellow => "\x1B[1m\x1B[33m",
      :bblue => "\x1B[1m\x1B[34m",
      :bmagenta => "\x1B[1m\x1B[35m",
      :bcyan => "\x1B[1m\x1B[36m",
      :bwhite => "\x1B[1m\x1B[37m",
    }

    #take a string and using the keys in the hash, replace the keys in the 
    #string but surround the keys with ANSI color codes
    #No idea how to retain the case of the key!(TODO someday)
    def my_highlight msgx,hash
      return msgx if msgx.blank?
      return msgx if hash.empty?
      hash.each_pair do |k,v|
        if not k.nil?
          msgx.gsub! Regexp.new(k, Regexp::IGNORECASE), $my_my_ansi_colors[:normal]+$my_my_ansi_colors[v]+k.upcase+$my_my_ansi_colors[:normal]
        end
      end
      msgx
    end


    def add(severity, message = nil, progname = nil, &block)
      return if @level > severity

      message = (message || (block && block.call) || progname).to_s

      #INSERT BEGINS
      if not $myownglobalnumbercounter.nil?
        $myownglobalnumbercounter += 1
      else
        $myownglobalnumbercounter = 1
      end

      level = {
        0 => "DEBUG",
        1 => "INFO",
        2 => "WARN",
        3 => "ERROR",
        4 => "FATAL"
      }[severity] || "U"

      message = "\x1B[0m[%d %s] : %s" % [$myownglobalnumbercounter,level,message]
      message = my_highlight message, {
        "debug" => :white,
        "error" => :bred,
        "info" => :bwhite,
        "warning" => :byellow,
        "warn" => :byellow ,
        "parameters" => :byellow,
        "#" => :bgreen,
        "ms " => :bmagenta,
        "GET " => :bmagenta,
        "PUT " => :bmagenta,
        "POST " => :bmagenta,
        "DELETE " => :bmagenta
        }
      #INSERT ENDS

      message = "#{message}\n" unless message[-1] == ?\n
      buffer << message
      auto_flush
      message
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文