File: log4jxmlformatter.rb

package info (click to toggle)
ruby-log4r 1.1.10-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 648 kB
  • sloc: ruby: 2,744; xml: 96; makefile: 5
file content (64 lines) | stat: -rw-r--r-- 1,793 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# :include: ../rdoc/log4jxmlformatter
#
# == Other Info
#
# Version:: $Id$

require "log4r/formatter/formatter"

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