Class: Fluent::Logger::FluentLogger

Inherits:
LoggerBase show all
Defined in:
lib/fluent/logger/fluent_logger.rb,
lib/fluent/logger/fluent_logger/cui.rb

Defined Under Namespace

Modules: CUI

Constant Summary collapse

BUFFER_LIMIT =
8*1024*1024
RECONNECT_WAIT =
0.5
RECONNECT_WAIT_INCR_RATE =
1.5
RECONNECT_WAIT_MAX =
60
RECONNECT_WAIT_MAX_COUNT =
(1..100).inject(RECONNECT_WAIT_MAX / RECONNECT_WAIT) { |r, i|
  break i + 1 if r < RECONNECT_WAIT_INCR_RATE
  r / RECONNECT_WAIT_INCR_RATE
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from LoggerBase

open, #post

Constructor Details

#initialize(tag_prefix = nil, *args) ⇒ FluentLogger



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fluent/logger/fluent_logger.rb', line 73

def initialize(tag_prefix = nil, *args)
  super()

  options = {
    :host => 'localhost',
    :port => 24224,
    :use_nonblock => false
  }

  case args.first
  when String, Symbol
    # backward compatible
    options[:host] = args[0]
    options[:port] = args[1] if args[1]
  when Hash
    options.update args.first
  end

  @tag_prefix = tag_prefix
  @host = options[:host]
  @port = options[:port]
  @socket_path = options[:socket_path]
  @nanosecond_precision = options[:nanosecond_precision]
  @use_nonblock = options[:use_nonblock]
  @tls_options = options[:tls_options]

  @factory = MessagePack::Factory.new
  if @nanosecond_precision
    @factory.register_type(EventTime::TYPE, EventTime)
  end
  @packer = @factory.packer

  @mon = Monitor.new
  @pending = nil
  @connect_error_history = []

  @limit = options[:buffer_limit] || BUFFER_LIMIT
  @log_reconnect_error_threshold = options[:log_reconnect_error_threshold] || RECONNECT_WAIT_MAX_COUNT

  @buffer_overflow_handler = options[:buffer_overflow_handler]
  if options[:logger]
    @logger = options[:logger]
  else
    @logger = ::Logger.new(STDERR)
    if options[:debug]
      @logger.level = ::Logger::DEBUG
    else
      @logger.level = ::Logger::INFO
    end
  end

  @wait_writeable = true
  @wait_writeable = options[:wait_writeable] if options.key?(:wait_writeable)

  @timeout = options[:timeout] || 0

  @last_error = {}

  begin
    connect!
  rescue => e
    set_last_error(e)
    @logger.error "Failed to connect fluentd: #{$!}"
    @logger.error "Connection will be retried."
  end

  at_exit { close }
end

Instance Attribute Details

#limit ⇒ Object

Returns the value of attribute limit.



142
143
144
# File 'lib/fluent/logger/fluent_logger.rb', line 142

def limit
  @limit
end

#log_reconnect_error_threshold ⇒ Object

Returns the value of attribute log_reconnect_error_threshold.



142
143
144
# File 'lib/fluent/logger/fluent_logger.rb', line 142

def log_reconnect_error_threshold
  @log_reconnect_error_threshold
end

#logger ⇒ Object

Returns the value of attribute logger.



142
143
144
# File 'lib/fluent/logger/fluent_logger.rb', line 142

def logger
  @logger
end

Instance Method Details

#close ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fluent/logger/fluent_logger.rb', line 158

def close
  @mon.synchronize {
    if @pending
      begin
        send_data(@pending)
      rescue => e
        set_last_error(e)
        @logger.error("FluentLogger: Can't send logs to #{connection_string}: #{$!}")
        call_buffer_overflow_handler(@pending)
      end
    end
    @con.close if connect?
    @con = nil
    @pending = nil
  }
  self
end

#connect? ⇒ Boolean



176
177
178
# File 'lib/fluent/logger/fluent_logger.rb', line 176

def connect?
  @con && !@con.closed?
end

#connection_string ⇒ Object



217
218
219
# File 'lib/fluent/logger/fluent_logger.rb', line 217

def connection_string
  @socket_path ? "#{@socket_path}" : "#{@host}:#{@port}"
end

#create_socket! ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/fluent/logger/fluent_logger.rb', line 180

def create_socket!
  if @socket_path
    @con = UNIXSocket.new(@socket_path)
  else
    @con = Timeout.timeout(@timeout) do
      TCPSocket.new(@host, @port)
    end
    if @tls_options
      context = OpenSSL::SSL::SSLContext.new
      if @tls_options[:insecure]
        context.verify_mode = OpenSSL::SSL::VERIFY_NONE
      else
        context.set_params({})
        context.verify_mode = OpenSSL::SSL::VERIFY_PEER
        cert_store = OpenSSL::X509::Store.new
        if @tls_options[:use_default_ca]
          cert_store.set_default_paths
        end
        if @tls_options[:ca]
          cert_store.add_file(@tls_options[:ca])
        end

        context.cert = OpenSSL::X509::Certificate.new(File.read(@tls_options[:cert])) if @tls_options[:cert]
        context.key = OpenSSL::PKey::read(File.read(@tls_options[:key]), @tls_options[:key_passphrase]) if @tls_options[:key]
        context.ciphers = @tls_options[:ciphers] || "ALL:!aNULL:!eNULL:!SSLv2".freeze
        context.cert_store = cert_store
      end
      set_tls_version(context)

      @con = OpenSSL::SSL::SSLSocket.new(@con, context)
      @con.sync_close = true
      @con.connect
    end
    @con
  end
end

#last_error ⇒ Object



144
145
146
# File 'lib/fluent/logger/fluent_logger.rb', line 144

def last_error
  @last_error[Thread.current.object_id]
end

#pending_bytesize ⇒ Object



221
222
223
224
225
226
227
# File 'lib/fluent/logger/fluent_logger.rb', line 221

def pending_bytesize
  if @pending
    @pending.bytesize
  else
    0
  end
end

#post_with_time(tag, map, time) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/fluent/logger/fluent_logger.rb', line 148

def post_with_time(tag, map, time)
  @logger.debug { "event: #{tag} #{map.to_json}" rescue nil } if @logger.debug?
  tag = "#{@tag_prefix}.#{tag}" if @tag_prefix
  if @nanosecond_precision && time.is_a?(Time)
    write [tag, EventTime.new(time.to_i, time.nsec), map]
  else
    write [tag, time.to_i, map]
  end
end