Skip to content

Commit 6504450

Browse files
phjordansissel
authored andcommitted
Change the file root extract logic to allow folder to have dynamic part in it
Fixes #4
1 parent 5f235ff commit 6504450

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/logstash/outputs/file.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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.
99
class 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

spec/outputs/file_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,23 @@
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'

0 commit comments

Comments
 (0)