Class: MQ::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/mq/logger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Logger

Returns a new instance of Logger.



3
4
5
6
7
8
9
10
11
# File 'lib/mq/logger.rb', line 3

def initialize *args, &block
  opts = args.pop if args.last.is_a? Hash
  opts ||= {}

  printer(block) if block

  @prop = opts
  @tags = ([:timestamp] + args).uniq
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mq/logger.rb', line 57

def log severity, *args
  opts = args.pop if args.last.is_a? Hash and args.size != 1
  opts ||= {}
  opts = @prop.clone.update(opts)

  data = args.shift

  data = {:type => :exception,
          :name => data.class.to_s.intern,
          :backtrace => data.backtrace,
          :message => data.message} if data.is_a? Exception

  (@tags + args).each do |tag|
    tag = tag.to_sym
    case tag
    when :timestamp
      opts.update :timestamp => Time.now
    when :hostname
      @hostname ||= { :hostname => `hostname`.strip }
      opts.update @hostname
    when :process
      @process_id ||= { :process_id => Process.pid,
                        :process_name => $0,
                        :process_parent_id => Process.ppid,
                        :thread_id => Thread.current.object_id }
      opts.update :process => @process_id
    else
      (opts[:tags] ||= []) << tag
    end
  end

  opts.update(:severity => severity,
              :msg => data)

  print(opts)
  unless Logger.disabled?
    MQ.fanout('logging', :durable => true).publish Marshal.dump(opts)
  end

  opts
end

Instance Attribute Details

#propObject (readonly) Also known as: base

Returns the value of attribute prop.



13
14
15
# File 'lib/mq/logger.rb', line 13

def prop
  @prop
end

Class Method Details

.disableObject



85
86
87
# File 'lib/mq/logger.rb', line 85

def self.disable
  @disabled = true
end

.disabled?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/mq/logger.rb', line 77

def self.disabled?
  !!@disabled
end

.enableObject



81
82
83
# File 'lib/mq/logger.rb', line 81

def self.enable
  @disabled = false
end

.printer(&block) ⇒ Object



72
73
74
75
# File 'lib/mq/logger.rb', line 72

def self.printer &block
  @printer = block if block
  @printer
end

Instance Method Details

#log(severity, *args) ⇒ Object Also known as: method_missing



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
# File 'lib/mq/logger.rb', line 16

def log severity, *args
  opts = args.pop if args.last.is_a? Hash and args.size != 1
  opts ||= {}
  opts = @prop.clone.update(opts)

  data = args.shift

  data = {:type => :exception,
          :name => data.class.to_s.intern,
          :backtrace => data.backtrace,
          :message => data.message} if data.is_a? Exception

  (@tags + args).each do |tag|
    tag = tag.to_sym
    case tag
    when :timestamp
      opts.update :timestamp => Time.now
    when :hostname
      @hostname ||= { :hostname => `hostname`.strip }
      opts.update @hostname
    when :process
      @process_id ||= { :process_id => Process.pid,
                        :process_name => $0,
                        :process_parent_id => Process.ppid,
                        :thread_id => Thread.current.object_id }
      opts.update :process => @process_id
    else
      (opts[:tags] ||= []) << tag
    end
  end

  opts.update(:severity => severity,
              :msg => data)

  print(opts)
  unless Logger.disabled?
    MQ.fanout('logging', :durable => true).publish Marshal.dump(opts)
  end

  opts
end


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mq/logger.rb', line 59

def print data = nil, &block
  if block
    @printer = block
  elsif data.is_a? Proc
    @printer = data
  elsif data
    (pr = @printer || self.class.printer) and pr.call(data)
  else
    @printer
  end
end