File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change 77# This output will write events to files on disk. You can use fields
88# from the event as parts of the filename and/or path.
99class LogStash ::Outputs ::File < LogStash ::Outputs ::Base
10+ FIELD_REF = /%\{ [^}]+\} /
1011
1112 config_name "file"
1213 milestone 2
@@ -126,7 +127,7 @@ def generate_filepath(event)
126127
127128 private
128129 def path_with_field_ref?
129- path =~ /% \{ [^}]+ \} /
130+ path =~ FIELD_REF
130131 end
131132
132133 def format_message ( event )
@@ -138,8 +139,8 @@ def format_message(event)
138139 end
139140
140141 def extract_file_root
141- extracted_path = File . expand_path ( path . gsub ( /%{.+/ , '' ) )
142- Pathname . new ( extracted_path ) . expand_path
142+ parts = File . expand_path ( path ) . split ( File :: SEPARATOR )
143+ parts . take_while { | part | part !~ FIELD_REF } . join ( File :: SEPARATOR )
143144 end
144145
145146 def teardown
Original file line number Diff line number Diff line change 186186 end
187187 end
188188
189+ it 'write the events to a file when some part of a folder or file is dynamic' do
190+ t = Time . now
191+ good_event = LogStash ::Event . new ( "@timestamp" => t )
192+
193+ Stud ::Temporary . directory do |path |
194+ dynamic_path = "#{ path } /failed_syslog-%{+YYYY-MM-dd}"
195+ expected_path = "#{ path } /failed_syslog-#{ t . strftime ( "%Y-%m-%d" ) } "
196+
197+ config = { "path" => dynamic_path }
198+ output = LogStash ::Outputs ::File . new ( config )
199+ output . register
200+ output . receive ( good_event )
201+
202+ expect ( File . exist? ( expected_path ) ) . to eq ( true )
203+ end
204+ end
205+
189206 it 'write the event to the generated filename with multiple deep' do
190207 good_event = LogStash ::Event . new
191208 good_event [ 'error' ] = '/inside/errors/42.txt'
You can’t perform that action at this time.
0 commit comments