Sha256: fe587595bcf09ed93bfbc09abda41e60948df758dd727929fcbd46e75b9b5fd9

Contents?: true

Size: 1.77 KB

Versions: 62

Compression:

Stored size: 1.77 KB

Contents

# :include: ../rdoc/log4jxmlformatter
#
# == Other Info
#
# Version:: $Id$

require "log4r/formatter/formatter"

require "rubygems"
begin
  require "builder"
rescue LoadError
  puts "builder gem is required to use log4jxmlformatter, i.e. gem install builder"
end

module Log4r

  class Log4jXmlFormatter < BasicFormatter
    
    def format(logevent)
      logger = logevent.fullname.gsub('::', '.')
      timestamp = (Time.now.to_f * 1000).to_i
      level = LNAMES[logevent.level]
      message = format_object(logevent.data)
      exception = message if logevent.data.kind_of? Exception
      file, line, method = parse_caller(logevent.tracer[0]) if logevent.tracer
      
      builder = Builder::XmlMarkup.new
      xml = builder.log4j :event, :logger => logger,
                                  :timestamp => timestamp,
                                  :level => level,
                                  :thread => '' do |e|
              e.log4j :NDC, NDC.get
              e.log4j :message, message
              e.log4j :throwable, exception if exception
              e.log4j :locationInfo, :class => '',
                                     :method => method,
                                     :file => file,
                                     :line => line
              e.log4j :properties do |p|
                MDC.get_context.each do |key, value|
                  p.log4j :data, :name => key, :value => value
                end
              end
            end
      xml
    end

    #######
    private
    #######

    def parse_caller(line)
      if /^(.+?):(\d+)(?::in `(.*)')?/ =~ line
        file = Regexp.last_match[1]
        line = Regexp.last_match[2].to_i
        method = Regexp.last_match[3]
        [file, line, method]
      else
        []
      end
    end
  end

end

Version data entries

62 entries across 54 versions & 13 rubygems

Version Path
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.6.2 vendor/bundle/ruby/2.6.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.6.1 vendor/bundle/ruby/2.6.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.6.0 vendor/bundle/ruby/2.6.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.6.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.6.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/log4r-1.1.10/lib/log4r/formatter/log4jxmlformatter.rb