Class: FlumeLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/flume-logger/logger.rb,
lib/flume-logger/version.rb

Defined Under Namespace

Classes: Eventserver

Constant Summary collapse

HOST =
::Socket.gethostname
PRIORITY =
{
  "FATAL"    => 0,
  "ERROR"    => 1,
  "WARN"     => 2,
  "INFO"     => 3,
  "DEBUG"    => 4,
  "TRACE"    => 5 
}
VERSION =
"0.1.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, flume_type = :ng, headers = {}) ⇒ FlumeLogger

Returns a new instance of FlumeLogger.



15
16
17
18
19
# File 'lib/flume-logger/logger.rb', line 15

def initialize(host, port, flume_type=:ng, headers={})
  super(::FlumeLogger::Eventserver.new(host, port, flume_type))
  @type = flume_type
  @headers = headers
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/flume-logger/logger.rb', line 3

def client
  @client
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/flume-logger/logger.rb', line 21

def add(severity, message = nil, progname = nil, &block)
  severity ||= UNKNOWN
  if severity < @level
    return true
  end
  progname ||= @progname
  if message.nil?
    if block_given?
      message = yield
    else
      message = progname
      progname = @progname
    end
  end
  @logdev.write(
    format_message(format_severity(severity), Time.now, progname, message))
  true
end

#format_message(severity, time, progname, message) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/flume-logger/logger.rb', line 40

def format_message(severity, time, progname, message)
  data = message
  body = case data
  when Hash
    @headers = data.clone
    if @headers.has_key?('message')
      @headers.delete('message')
    end
    data['message'] || "No 'message' in Hash"
  when String
    data
  end

  if progname
    @headers['application'] = progname
  end
  @headers['host'] ||= HOST

  event = case @type
  when :ng
    $:.push File.expand_path("../flume-ng", __FILE__)
    require 'thrift_source_protocol'

    @headers['pri'] = severity
    evt = ThriftFlumeEvent.new()
    evt.headers = @headers
    evt.body = body
    evt
  when :og
    $:.push File.expand_path("../flume-og", __FILE__)
    require 'thrift_flume_event_server'  

    evt = ThriftFlumeEvent.new()
    evt.timestamp = time.to_f.to_i * 1000
    evt.priority = PRIORITY[severity]
    evt.body = body
    evt.host = @headers['host']
    evt.nanos = time.usec * 1000
    @headers.delete('host')
    evt.fields = @headers
    evt
  end
  
  event
end