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

Rename the filename to paht in file adapter

This commit is contained in:
Holger Just 2018-02-06 20:30:30 +01:00
parent 4ee7a3f967
commit 21e0c5a228
2 changed files with 18 additions and 19 deletions

View File

@ -65,7 +65,7 @@ module Rackstash
register_for ::String, ::Pathname, 'file' 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 :path
def self.from_uri(uri) def self.from_uri(uri)
uri = URI(uri) uri = URI(uri)
@ -83,21 +83,21 @@ module Rackstash
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 `path`.
# #
# We will always resolve `filename` to an absolute path once during # We will always resolve the `path` to an absolute path once during
# initialization. When passing a relative path, it will be resolved # initialization. When passing a relative path, it will be resolved
# according to the current working directory. See # according to the current working directory. See
# [`::File.expand_path`](https://ruby-doc.org/core/File.html#method-c-expand_path) # [`::File.expand_path`](https://ruby-doc.org/core/File.html#method-c-expand_path)
# for details. # for details.
# #
# @param filename [String, Pathname] the path to the logfile # @param path [String, Pathname] the path to the logfile
# @param auto_reopen [Boolean] set to `true` to automatically reopen the # @param auto_reopen [Boolean] set to `true` to automatically reopen the
# log file (and potentially create a new one) if we detect that the # log file (and potentially create a new one) if we detect that the
# current log file was moved or deleted, e.g. due to an external # current log file was moved or deleted, e.g. due to an external
# logrotate run # logrotate run
def initialize(filename, auto_reopen: true) def initialize(path, auto_reopen: true)
@filename = ::File.expand_path(filename).freeze @path = ::File.expand_path(path).freeze
@auto_reopen = !!auto_reopen @auto_reopen = !!auto_reopen
@mutex = Mutex.new @mutex = Mutex.new
@ -149,7 +149,7 @@ module Rackstash
end end
# Reopen the logfile. We will open the file located at the original # Reopen the logfile. We will open the file located at the original
# {#filename} or create a new one if it does not exist. # {#path} or create a new one if it does not exist.
# #
# If the file can not be opened, an exception will be raised. # If the file can not be opened, an exception will be raised.
# @return [nil] # @return [nil]
@ -162,24 +162,23 @@ module Rackstash
private private
# Reopen the log file if the original filename does not reference the # Reopen the log file if the original {#path} does not reference the
# opened file anymore (e.g. because it was moved or deleted) # opened file anymore (e.g. because it was moved or deleted)
def auto_reopen def auto_reopen
return unless @auto_reopen return unless @auto_reopen
return if @file.closed? return if @file.closed?
return if ::File.identical?(@file, @filename) return if ::File.identical?(@file, @path)
reopen_file reopen_file
end end
def open_file def open_file
unless ::File.exist? ::File.dirname(@filename) dirname = ::File.dirname(path)
FileUtils.mkdir_p ::File.dirname(@filename) FileUtils.mkdir_p(dirname) unless ::File.exist?(dirname)
end
file = ::File.new( file = ::File.new(
filename, path,
::File::WRONLY | ::File::APPEND | ::File::CREAT ::File::WRONLY | ::File::APPEND | ::File::CREAT
) )
file.binmode file.binmode

View File

@ -30,10 +30,10 @@ describe Rackstash::Adapter::File do
.to be_instance_of described_class .to be_instance_of described_class
end end
it 'sets the filename from the URI path' do it 'sets the path from the URI path' do
expect(described_class.from_uri('file:/tmp/file_spec.log').filename) expect(described_class.from_uri('file:/tmp/file_spec.log').path)
.to eql '/tmp/file_spec.log' .to eql '/tmp/file_spec.log'
expect(described_class.from_uri('file:///tmp/file_spec.log').filename) expect(described_class.from_uri('file:///tmp/file_spec.log').path)
.to eql '/tmp/file_spec.log' .to eql '/tmp/file_spec.log'
end end
@ -53,13 +53,13 @@ describe Rackstash::Adapter::File do
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).path)
.to eql(logfile.path) .to eql(logfile.path)
.and be_a String .and be_a String
end end
it 'accepts a Pathname' do it 'accepts a Pathname' do
expect(described_class.new(Pathname.new(logfile.path)).filename) expect(described_class.new(Pathname.new(logfile.path)).path)
.to eql(logfile.path) .to eql(logfile.path)
.and be_a String .and be_a String
end end
@ -76,7 +76,7 @@ describe Rackstash::Adapter::File do
adapter = described_class.new File.join(base, 'dir', 'sub', 'test.log') adapter = described_class.new File.join(base, 'dir', 'sub', 'test.log')
expect(adapter.filename).to eql File.join(base, 'dir', 'sub', 'test.log') expect(adapter.path).to eql File.join(base, 'dir', 'sub', 'test.log')
expect(File.directory?(File.join(base, 'dir'))).to be true expect(File.directory?(File.join(base, 'dir'))).to be true
expect(File.file?(File.join(base, 'dir', 'sub', 'test.log'))).to be true expect(File.file?(File.join(base, 'dir', 'sub', 'test.log'))).to be true
end end