【问题标题】:Ruby Log4r has issues printing twice the log messagesRuby Log4r 打印两次日志消息时出现问题
【发布时间】:2013-09-19 13:04:38
【问题描述】:

我使用 log4r 作为我的记录器。我有两个问题要问论坛。

  1. 我发现了一个神秘的行为,即日志消息在 STDOUT 中打印了两次。我不明白它为什么会发生以及如何解决。
  2. 我想为级别标签提供颜色编码(FATAL 为红色,ERROR 为紫色,WARN 为橙色,INFO 为绿色,等等)。这只需要对 STDOUT 有效,并且我的日志文件中不应出现特殊(元)字符(以便于阅读)。我尝试了几次尝试,但无法做到这一点。如何解决?

以下是我使用的代码的详细信息。请帮助解决此问题。

我的 YAML 配置如下: 档案:yml.cfg


    # *** YAML2LOG4R ***
    log4r_config:
      # define all pre config global log4r settings ...
      pre_config:
        custom_levels:
          - DEBUG2
          - DEBUG
          - PRINT
          - INFO
          - WARN
          - ERROR
          - FATAL
        global:
          level       : ALL 
        root  :
          level       : DEBUG2
          trace       : 'true'

      # define all outputters (stderr, stdout and logfile with custom formatters)
      outputters:
        - type        : StderrOutputter
          name        : stderr 
          level       : ERROR 
          only_at     :
            - ERROR
            - FATAL
          formatter:
            pattern   : '%-7l: %m'
            type      : PatternFormatter

        - type        : StdoutOutputter
          name        : stdout 
          level       : INFO 
          only_at     :
            - INFO
            - WARN
            - ERROR
            - FATAL
          formatter:
            pattern   : '%-7l: %m'
            type      : PatternFormatter

        - type        : FileOutputter
          filename    : debug.log
          name        : logfile
          level       : ALL 
          trace       : 'true'
          trunc       : 'false'
          formatter   :
            type      : MyLogFormatter

      loggers: 

      - name        : MyAppClass
        additive    : 'false'
        trace       : 'true'
        level       : ALL
        outputters  :
             - stderr
             - stdout
             - logfile

我有一个日志配置类 (logConfig.rb)

 require 'log4r'
 require 'log4r/lib/yamlconfigurator'

  class LogConfig
    class Log4r::MyLogFormatter < Log4r::Formatter
      def format(event)
        buff = "%-7s: " % Log4r::LNAMES[event.level]
        buff += "(Trace: %-30s): " % [event.tracer[0].split(File::SEPARATOR)[-1]] 
        if event.data.kind_of?(String) then buff += event.data
        elsif event.data.kind_of?(Array) then  buff += "\n\t\t%s : %s\n" % [event.data.class, event.data.inspect] 
        elsif event.data.kind_of?(Hash)  then  buff += "\n\t\t%s : %s\n" % [event.data.class, event.data.inspect] end
        return buff + "\n"
      end
    end # class MyLogFormatter

   def initialize
      cfg = Log4r::YamlConfigurator
      cfg.load_yaml_file('yml.cfg')
      Log4r::StderrOutputter.new 'console'
      Log4r::StdoutOutputter.new 'console'
    end # def initialize

  end # class LogConfig

在我的应用程序类(myClass.rb)中

  require 'logConfig'
  class MyAppClass
    ClassName = self
    def initialize
      LogConfig.new
      @log = Log4r::Logger["#{ClassName}"]
    end # initialize

    def print_logs
       @log.fatal 'this is fatal'
       @log.error 'this is error'
       @log.warn 'this is warning'
       @log.info 'this is info'
       @log.print 'this is print'
       @log.debug 'this is debug'
       @log.debug2 'this is debug2'
    end
end
MyAppClass.new().print_logs

在 debug.log 文件中,我正确地收到了预期的消息:

FATAL  : (Trace: myClass.rb:11:in `print_logs' ): this is fatal
ERROR  : (Trace: myClass.rb:12:in `print_logs' ): this is error
WARN   : (Trace: myClass.rb:13:in `print_logs' ): this is warning
INFO   : (Trace: myClass.rb:14:in `print_logs' ): this is info
PRINT  : (Trace: myClass.rb:15:in `print_logs' ): this is print
DEBUG  : (Trace: myClass.rb:16:in `print_logs' ): this is debug
DEBUG2 : (Trace: myClass.rb:17:in `print_logs' ): this is debug2

但是,在控制台(终端)中,我得到了两次打印的错误和致命消息。

>ruby myClass.rb
FATAL  : this is fatal
FATAL  : this is fatal
ERROR  : this is error
ERROR  : this is error
WARN   : this is warning
INFO   : this is info

请帮忙解决以上问题。

【问题讨论】:

    标签: ruby log4r


    【解决方案1】:

    我猜你在配置文件中也提到过。

    outputters:
            - type        : StderrOutputter
              name        : stderr 
              level       : ERROR 
              only_at     :
                - ERROR
                - FATAL
    
              - type        : StdoutOutputter
              name        : stdout 
              level       : INFO 
              only_at     :
                - INFO
                - WARN
                - ERROR
                - FATAL
    
    
    
              - type        : FileOutputter
              filename    : debug.log
              name        : logfile
              level       : ALL 
              trace       : 'true'
              trunc       : 'false'
    

    应该为 stdout 和 stderr 输出器打印错误和致命。

    在控制台中,您同时询问 stdout 和 stderr 打印

          Log4r::StderrOutputter.new 'console'
          Log4r::StdoutOutputter.new 'console'
    

    只需删除配置文件标准输出中的错误和致命错误,然后两个打印就会消失。因此,对于致命或错误,它将使用标准错误,而对于信息和警告,它将使用标准输出。

    就颜色而言,您可以使用http://rubygems.org/gems/log4r-color 而不仅仅是log4r。这是 log4r gem 的扩展。请看Class: Log4r::ColorOutputter

    【讨论】:

    • 谢谢@vamsi,我会试试你的建议。对于颜色输出,我使用 Ruby 1.8.6 并且我没有 gem 安装程序(没有安装权限)。它是否以 zip 格式在其他地方提供?
    • log4r-color 效果很好。但是,我有两个问题。 a) 我将控制台输出发送到日志文件。如果我这样做,着色就不会发生。如何在控制台同时输出颜色,以及将消息记录在文件中。我尝试添加:filename =&gt; 'console.log',但没有帮助。 B) 我如何只为关卡着色(而不是整个文本)?
    • 您可以将自己的颜色代码添加到日志消息格式中。然后这些也将被写入文件(这通常是不需要的)。您可以像这样通过 ack 为普通输出着色:powdahound.com/2009/10/colorize-log-output-with-ack
    猜你喜欢
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多