【问题标题】:Add process information to Rails logger将进程信息添加到 Rails 记录器
【发布时间】:2012-04-13 19:46:50
【问题描述】:

我目前有一个 Rails 应用程序,它有多个进程:由 Redis 触发的 Web 服务进程和后台工作进程。

问题有时很难检查日志文件并确定给定行为发生的位置 - 是在 Web 部分还是在 Resque 工作人员上?

有没有办法包含进程名称甚至进程 ID 或允许我按进程区分每个日志条目的内容?

【问题讨论】:

    标签: ruby-on-rails-3 logging


    【解决方案1】:

    看起来有几个选项可以解决这个问题:

    这是一篇相关的 SO 文章: - Rails 3.2.2 log files unordered, requests intertwined

    对我来说最好的选择似乎是改用 :uuid。当您有多个进程记录到同一个文件时,它传达相同的信息,以便您区分请求。

    【讨论】:

    • 这看起来很酷,感谢您的回答。我会接受它,因为它已经有一段时间了,没有其他人回复。
    【解决方案2】:

    如果您需要控制器上下文之外的进程 ID(例如延迟作业),您可以将其放入初始化程序中:

    class ActiveSupport::BufferedLogger
      def formatter=(formatter)
        @log.formatter = formatter
      end
    end
    
    class Formatter
      SEVERITY_TO_COLOR_MAP   = {'DEBUG'=>'0;37', 'INFO'=>'32', 'WARN'=>'33', 'ERROR'=>'31', 'FATAL'=>'31', 'UNKNOWN'=>'37'}
    
      def call(severity, time, progname, msg)
        formatted_severity = sprintf("%-5s","#{severity}")
    
        formatted_time = time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..6].ljust(6)
        color = SEVERITY_TO_COLOR_MAP[severity]
    
        "\033[0;37m#{formatted_time} (pid:#{$$})\033[0m [\033[#{color}m#{formatted_severity}\033[0m] #{msg.strip}\n"
      end
    
    end
    
    Rails.logger.formatter = Formatter.new
    

    更多:http://www.software-thoughts.com/2013/08/adding-process-id-and-timestamps-to.html

    原帖在这里:http://cbpowell.wordpress.com/2012/04/05/beautiful-logging-for-ruby-on-rails-3-2/

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 2020-05-17
      • 2017-03-16
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 2019-07-11
      • 2016-09-12
      相关资源
      最近更新 更多