1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Allow to define a File adapter using a file:// URI

This commit is contained in:
Holger Just 2018-01-23 23:54:48 +01:00
parent 39f12047cb
commit 577c065900
2 changed files with 38 additions and 2 deletions

View File

@ -57,11 +57,26 @@ module Rackstash
# file was moved, you don't need to create the new file there nor should you # file was moved, you don't need to create the new file there nor should you
# use the (potentially destructive) `copytruncate` option of logrotate. # use the (potentially destructive) `copytruncate` option of logrotate.
class File < BaseAdapter class File < BaseAdapter
register_for ::String, ::Pathname register_for ::String, ::Pathname, 'file'
# @return [String] the absolute path to the log file # @return [String] the absolute path to the log file
attr_reader :filename attr_reader :filename
def self.from_uri(uri)
uri = URI(uri)
if (uri.scheme || uri.opaque) == 'file'.freeze
file_options = parse_uri_options(uri)
if file_options[:auto_reopen] =~ /\A(:?false|0)?\z/i
file_options[:auto_reopen] = false
end
new(uri.path, **file_options)
else
raise ArgumentError, "Invalid URI: #{uri}"
end
end
# Create a new file adapter instance which writes logs to the log file # Create a new file adapter instance which writes logs to the log file
# specified in `filename`. # specified in `filename`.
# #

View File

@ -22,6 +22,27 @@ describe Rackstash::Adapter::File do
logfile.unlink logfile.unlink
end end
describe 'from_uri' do
it 'creates a File adapter instance' do
expect(described_class.from_uri('file:/tmp/file_spec.log'))
.to be_instance_of described_class
expect(described_class.from_uri('file:///tmp/file_spec.log'))
.to be_instance_of described_class
end
it 'sets the filename from the URI path' do
expect(described_class.from_uri('file:/tmp/file_spec.log').filename)
.to eql '/tmp/file_spec.log'
expect(described_class.from_uri('file:///tmp/file_spec.log').filename)
.to eql '/tmp/file_spec.log'
end
it 'sets optional attributes' do
expect(described_class.from_uri('file:/tmp/file_spec.log?auto_reopen=false').auto_reopen?)
.to eql false
end
end
describe '#initialize' do describe '#initialize' do
it 'accepts a String' do it 'accepts a String' do
expect(described_class.new(logfile.path).filename) expect(described_class.new(logfile.path).filename)
@ -35,7 +56,7 @@ describe Rackstash::Adapter::File do
.and be_a String .and be_a String
end end
it 'rejects non-IO objects' do it 'rejects other objects' do
expect { described_class.new(nil) }.to raise_error TypeError expect { described_class.new(nil) }.to raise_error TypeError
expect { described_class.new(Object.new) }.to raise_error TypeError expect { described_class.new(Object.new) }.to raise_error TypeError
expect { described_class.new(23) }.to raise_error TypeError expect { described_class.new(23) }.to raise_error TypeError