ruby TorqueBox Logger尊重标记的日志记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby TorqueBox Logger尊重标记的日志记录相关的知识,希望对你有一定的参考价值。

# Copy of TorqueBox::Logger
# Try to unfrak it

require 'logger'

module TorqueBox

  # @api private
  class FallbackLogger < ::Logger

    attr_accessor :formatter

    def initialize name = nil
      super(ENV['TORQUEBOX_FALLBACK_LOGFILE'] || $stderr)
      @category = name || (TORQUEBOX_APP_NAME if defined? TORQUEBOX_APP_NAME) || "TorqueBox"
      @formatter = ::Logger::Formatter.new
    end

    def add(severity, message, progname, &block)
      if ( message.nil? && block.nil? )
        message = progname
        progname = @category
      end
      message = progname if message.nil?
      super( severity, message, @category, &block )
    end

    # Allow our logger to be used for env['rack.errors']
    def puts(message)
      info message.to_s
    end
    def write(message)
      info message.strip
    end
    def flush
    end
  end

  begin
    org.jboss.logging::Logger
  rescue ::NameError
    Logger = FallbackLogger
    return
  end

  class Logger
    include LoggerSilence

    attr_accessor :formatter

    # Torquebox has its own levels, but all Rails logging will respect this level and
    # if appropriate forward it on to TB
    attr_accessor :level

    module Severity
      DEBUG = 0
      INFO = 1
      WARN = 2
      ERROR = 3
      FATAL = 4
      UNKNOWN = 5
      SEVERITIES = ['debug', 'info', 'warn', 'error', 'fatal']
    end
    include Severity

    def initialize name = nil
      @progname = name || (TORQUEBOX_APP_NAME if defined? TORQUEBOX_APP_NAME) || "TorqueBox"
      @logger = org.jboss.logging::Logger.getLogger( @progname.to_s.gsub('::','.') )
      @formatter = ActiveSupport::Logger::SimpleFormatter.new
      @level = INFO
    end

    # Returns +true+ iff the current severity level allows for the printing of
    # +DEBUG+ messages.
    def debug?; @level <= DEBUG; end

    # Returns +true+ iff the current severity level allows for the printing of
    # +INFO+ messages.
    def info?; @level <= INFO; end

    # Returns +true+ iff the current severity level allows for the printing of
    # +WARN+ messages.
    def warn?; @level <= WARN; end

    # Returns +true+ iff the current severity level allows for the printing of
    # +ERROR+ messages.
    def error?; @level <= ERROR; end

    # Returns +true+ iff the current severity level allows for the printing of
    # +FATAL+ messages.
    def fatal?; @level <= FATAL; end

    def add(severity, message = nil, progname = nil, &block)
      # default to warn for unknown log level since jboss logger
      # doesn't support unknown
      severity = severity.to_i > (SEVERITIES.length - 1) ? WARN : severity.to_i
      if severity < @level
        return true
      end
      progname ||= @progname
      if message.nil?
        if block_given?
          message = yield || ""
        else
          message = progname
          progname = @progname
        end
      end
      delegate = SEVERITIES[severity]
      # The TB logger doesnt need timestamp or progname
      @logger.send(delegate, self.formatter.call(delegate, nil, nil, message))
    end

    def debug(progname = nil, &block)
      add(DEBUG, nil, progname, &block)
    end

    def info(progname = nil, &block)
      add(INFO, nil, progname, &block)
    end

    def warn(progname = nil, &block)
      add(WARN, nil, progname, &block)
    end

    def error(progname = nil, &block)
      add(ERROR, nil, progname, &block)
    end

    def fatal(progname = nil, &block)
      add(FATAL, nil, progname, &block)
    end


    # Allow our logger to be used for env['rack.errors']
    def puts(message)
      info message.to_s
    end
    def write(message)
      info message.strip
    end
    def flush
    end

  end
end

以上是关于ruby TorqueBox Logger尊重标记的日志记录的主要内容,如果未能解决你的问题,请参考以下文章

不尊重 zindex 的 Mapbox 标记

ruby open类的自定义rails日志:ActiveSupport :: Logger :: SimpleFormatter

HTML Anchor 标记是不是应该尊重 Disabled 属性?

扭矩箱服务:漂亮吗?

Ruby | 软装是一门艺术!

导致 ActiveRecord 记录器 IOError 的 Ruby 守护进程