Class: Fluent::Logger::FluentLogger
- Inherits:
-
LoggerBase
- Object
- LoggerBase
- Fluent::Logger::FluentLogger
- 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
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#log_reconnect_error_threshold ⇒ Object
Returns the value of attribute log_reconnect_error_threshold.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #close ⇒ Object
- #connect? ⇒ Boolean
- #connection_string ⇒ Object
- #create_socket! ⇒ Object
-
#initialize(tag_prefix = nil, *args) ⇒ FluentLogger
constructor
A new instance of FluentLogger.
- #post_with_time(tag, map, time) ⇒ Object
Methods inherited from LoggerBase
Constructor Details
#initialize(tag_prefix = nil, *args) ⇒ FluentLogger
Returns a new instance of FluentLogger.
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 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 |
# File 'lib/fluent/logger/fluent_logger.rb', line 37 def initialize(tag_prefix = nil, *args) super() = { :host => 'localhost', :port => 24224 } case args.first when String, Symbol # backward compatible [:host] = args[0] [:port] = args[1] if args[1] when Hash .update args.first end @tag_prefix = tag_prefix @host = [:host] @port = [:port] @socket_path = [:socket_path] @mon = Monitor.new @pending = nil @connect_error_history = [] @limit = [:buffer_limit] || BUFFER_LIMIT @log_reconnect_error_threshold = [:log_reconnect_error_threshold] || RECONNECT_WAIT_MAX_COUNT @buffer_overflow_handler = [:buffer_overflow_handler] if logger = [:logger] @logger = logger else @logger = ::Logger.new(STDERR) if [:debug] @logger.level = ::Logger::DEBUG else @logger.level = ::Logger::INFO end end @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
#last_error ⇒ Object (readonly)
Returns the value of attribute last_error.
92 93 94 |
# File 'lib/fluent/logger/fluent_logger.rb', line 92 def last_error @last_error end |
#limit ⇒ Object
Returns the value of attribute limit.
91 92 93 |
# File 'lib/fluent/logger/fluent_logger.rb', line 91 def limit @limit end |
#log_reconnect_error_threshold ⇒ Object
Returns the value of attribute log_reconnect_error_threshold.
91 92 93 |
# File 'lib/fluent/logger/fluent_logger.rb', line 91 def log_reconnect_error_threshold @log_reconnect_error_threshold end |
#logger ⇒ Object
Returns the value of attribute logger.
91 92 93 |
# File 'lib/fluent/logger/fluent_logger.rb', line 91 def logger @logger end |
Instance Method Details
#close ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/fluent/logger/fluent_logger.rb', line 104 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
122 123 124 |
# File 'lib/fluent/logger/fluent_logger.rb', line 122 def connect? @con && !@con.closed? end |
#connection_string ⇒ Object
134 135 136 |
# File 'lib/fluent/logger/fluent_logger.rb', line 134 def connection_string @socket_path ? "#{@socket_path}" : "#{@host}:#{@port}" end |
#create_socket! ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/fluent/logger/fluent_logger.rb', line 126 def create_socket! if @socket_path @con = UNIXSocket.new(@socket_path) else @con = TCPSocket.new(@host, @port) end end |
#post_with_time(tag, map, time) ⇒ Object
98 99 100 101 102 |
# File 'lib/fluent/logger/fluent_logger.rb', line 98 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 write [tag, time.to_i, map] end |