@@ -18,7 +18,7 @@ def quit?
1818 end
1919
2020 def handle ( watched_file )
21- logger . trace ( "handling: #{ watched_file . filename } " )
21+ logger . trace? && logger . trace ( "handling:" , :path => watched_file . path )
2222 unless watched_file . has_listener?
2323 watched_file . set_listener ( @observer )
2424 end
@@ -37,7 +37,7 @@ def update_existing_specifically(watched_file, sincedb_value)
3737
3838 def controlled_read ( watched_file , loop_control )
3939 changed = false
40- logger . trace ( "reading..." , " iterations" => loop_control . count , " amount" => loop_control . size , " filename" => watched_file . filename )
40+ logger . trace? && logger . trace ( __method__ . to_s , : iterations => loop_control . count , : amount => loop_control . size , : filename => watched_file . filename )
4141 # from a real config (has 102 file inputs)
4242 # -- This cfg creates a file input for every log file to create a dedicated file pointer and read all file simultaneously
4343 # -- If we put all log files in one file input glob we will have indexing delay, because Logstash waits until the first file becomes EOF
@@ -48,7 +48,7 @@ def controlled_read(watched_file, loop_control)
4848 loop_control . count . times do
4949 break if quit?
5050 begin
51- logger . debug ( "read_to_eof: get chunk")
51+ logger . debug? && logger . debug ( " #{ __method__ } get chunk")
5252 result = watched_file . read_extract_lines ( loop_control . size ) # expect BufferExtractResult
5353 logger . trace ( result . warning , result . additional ) unless result . warning . empty?
5454 changed = true
@@ -57,40 +57,42 @@ def controlled_read(watched_file, loop_control)
5757 # sincedb position is now independent from the watched_file bytes_read
5858 sincedb_collection . increment ( watched_file . sincedb_key , line . bytesize + @settings . delimiter_byte_size )
5959 end
60- rescue EOFError
60+ rescue EOFError => e
6161 # it only makes sense to signal EOF in "read" mode not "tail"
62+ logger . debug ( __method__ . to_s , exception_details ( watched_file . path , e , false ) )
6263 loop_control . flag_read_error
6364 break
64- rescue Errno ::EWOULDBLOCK , Errno ::EINTR
65+ rescue Errno ::EWOULDBLOCK , Errno ::EINTR => e
66+ logger . debug ( __method__ . to_s , exception_details ( watched_file . path , e , false ) )
6567 watched_file . listener . error
6668 loop_control . flag_read_error
6769 break
6870 rescue => e
69- logger . error ( "read_to_eof: general error reading #{ watched_file . path } " , "error" => e . inspect , "backtrace" => e . backtrace . take ( 4 ) )
71+ logger . error ( "#{ __method__ } general error reading" , exception_details ( watched_file . path , e ) )
7072 watched_file . listener . error
7173 loop_control . flag_read_error
7274 break
7375 end
7476 end
75- logger . debug ( "read_to_eof: exit due to quit" ) if quit?
77+ logger . debug ( "#{ __method__ } stopped loop due quit" ) if quit?
7678 sincedb_collection . request_disk_flush if changed
7779 end
7880
7981 def open_file ( watched_file )
8082 return true if watched_file . file_open?
81- logger . trace ( "opening #{ watched_file . filename } " )
83+ logger . trace? && logger . trace ( "open_file" , :filename => watched_file . filename )
8284 begin
8385 watched_file . open
84- rescue
86+ rescue => e
8587 # don't emit this message too often. if a file that we can't
8688 # read is changing a lot, we'll try to open it more often, and spam the logs.
8789 now = Time . now . to_i
88- logger . trace ( "open_file OPEN_WARN_INTERVAL is '#{ OPEN_WARN_INTERVAL } '" )
90+ logger . trace? && logger . trace ( "open_file OPEN_WARN_INTERVAL is '#{ OPEN_WARN_INTERVAL } '" )
8991 if watched_file . last_open_warning_at . nil? || now - watched_file . last_open_warning_at > OPEN_WARN_INTERVAL
90- logger . warn ( "failed to open #{ watched_file . path } : #{ $! . inspect } , #{ $! . backtrace . take ( 3 ) } " )
92+ logger . warn ( "failed to open file" , exception_details ( watched_file . path , e ) )
9193 watched_file . last_open_warning_at = now
9294 else
93- logger . trace ( " suppressed warning for `failed to open` #{ watched_file . path } : #{ $! . inspect } " )
95+ logger . debug ( "open_file suppressed warning `failed to open file`" , exception_details ( watched_file . path , e , false ) )
9496 end
9597 watched_file . watch # set it back to watch so we can try it again
9698 else
@@ -108,40 +110,38 @@ def add_or_update_sincedb_collection(watched_file)
108110 update_existing_sincedb_collection_value ( watched_file , sincedb_value )
109111 watched_file . initial_completed
110112 else
111- msg = "add_or_update_sincedb_collection: found sincedb record"
112- logger . trace ( msg ,
113- "sincedb key" => watched_file . sincedb_key ,
114- "sincedb value" => sincedb_value
115- )
113+ logger . trace? && logger . trace ( "add_or_update_sincedb_collection: found sincedb record" ,
114+ :sincedb_key => watched_file . sincedb_key , :sincedb_value => sincedb_value )
116115 # detected a rotation, Discoverer can't handle this because this watched file is not a new discovery.
117116 # we must handle it here, by transferring state and have the sincedb value track this watched file
118117 # rotate_as_file and rotate_from will switch the sincedb key to the inode that the path is now pointing to
119118 # and pickup the sincedb_value from before.
120- msg = "add_or_update_sincedb_collection: the found sincedb_value has a watched_file - this is a rename, switching inode to this watched file"
121- logger . trace ( msg )
119+ logger . debug ( "add_or_update_sincedb_collection: the found sincedb_value has a watched_file - this is a rename, switching inode to this watched file" )
122120 existing_watched_file = sincedb_value . watched_file
123121 if existing_watched_file . nil?
124122 sincedb_value . set_watched_file ( watched_file )
125- logger . trace ( "add_or_update_sincedb_collection: switching as new file" )
123+ logger . trace? && logger . trace ( "add_or_update_sincedb_collection: switching as new file" )
126124 watched_file . rotate_as_file
127125 watched_file . update_bytes_read ( sincedb_value . position )
128126 else
129127 sincedb_value . set_watched_file ( watched_file )
130- logger . trace ( "add_or_update_sincedb_collection: switching from... " , " watched_file details" => watched_file . details )
128+ logger . trace? && logger . trace ( "add_or_update_sincedb_collection: switching from: " , : watched_file => watched_file . details )
131129 watched_file . rotate_from ( existing_watched_file )
132130 end
133131 end
134132 sincedb_value
135133 end
136134
137135 def update_existing_sincedb_collection_value ( watched_file , sincedb_value )
138- logger . trace ( "update_existing_sincedb_collection_value: #{ watched_file . filename } , last value #{ sincedb_value . position } , cur size #{ watched_file . last_stat_size } " )
136+ logger . trace? && logger . trace ( "update_existing_sincedb_collection_value" , :position => sincedb_value . position ,
137+ :filename => watched_file . filename , :last_stat_size => watched_file . last_stat_size )
139138 update_existing_specifically ( watched_file , sincedb_value )
140139 end
141140
142141 def add_new_value_sincedb_collection ( watched_file )
143142 sincedb_value = get_new_value_specifically ( watched_file )
144- logger . trace ( "add_new_value_sincedb_collection" , "position" => sincedb_value . position , "watched_file details" => watched_file . details )
143+ logger . trace? && logger . trace ( "add_new_value_sincedb_collection" , :position => sincedb_value . position ,
144+ :watched_file => watched_file . details )
145145 sincedb_collection . set ( watched_file . sincedb_key , sincedb_value )
146146 sincedb_value
147147 end
@@ -153,5 +153,14 @@ def get_new_value_specifically(watched_file)
153153 watched_file . update_bytes_read ( position )
154154 value
155155 end
156+
157+ private
158+
159+ def exception_details ( path , e , trace = true )
160+ details = { :path => path , :exception => e . class , :message => e . message }
161+ details [ :backtrace ] = e . backtrace if trace && logger . debug?
162+ details
163+ end
164+
156165 end
157166end end end
0 commit comments