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

Zab*_*bba 6 logging customization ruby-on-rails ruby-on-rails-3

tail -f在开发Rails应用程序时用来显示日志文件.它显示了日志消息(颜色!:),这很棒.

但是,如果在80宽度控制台中有如此多的信息,那么当我点击按钮获取资源时,很难跟踪某些"设置"日志消息的开始位置.

如果在每条日志消息/行的开头有行号或甚至是时间戳,则会更容易.这样我就记得我需要开始查看"行号2365"或"2010/10/10 23:33:23:45之后"的日志.

这可能吗?是否有一些Rails内部选项?

mar*_*ery 8

为什么不编辑所需环境的日志标记

development.rb

config.log_tags [ lambda {|r| DateTime.now } ]
Run Code Online (Sandbox Code Playgroud)

  • rails 4.1.2需要'=',config.log_tags = [lambda {| r | DateTime.now}] (5认同)
  • 这将为您提供"请求开始时的时间戳",而不是"当前时间戳". (4认同)

Zab*_*bba 1

谢谢@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
Run Code Online (Sandbox Code Playgroud)