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

时间:2010-12-24 00:15:06

标签: ruby-on-rails ruby-on-rails-3 customization logging

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

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

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

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

3 个答案:

答案 0 :(得分:8)

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

development.rb

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

答案 1 :(得分:3)

如果你想获得时间戳:

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: not opened for reading。 `

答案 2 :(得分:1)

谢谢@scaney。

我找到了解决方案here

我修改了代码以添加我自己的着色高亮(当然只用于开发!)现在我可以在控制台中看到黄色的“参数”之类的东西,我现在非常高兴!

如果有人感兴趣,这是我在environment.rb末尾的代码。 这是我目前的(脏)实现。可能会在以后解决这个问题(也许可以制作一个宝石,但是现在这对很好)

警告

DIRTY CODE跟随!使用风险自负!

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