From 21e0c5a228d83fd23d7863f5c625bc11c5a83523 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Tue, 6 Feb 2018 20:30:30 +0100 Subject: [PATCH] Rename the filename to paht in file adapter --- lib/rackstash/adapter/file.rb | 25 ++++++++++++------------- spec/rackstash/adapter/file_spec.rb | 12 ++++++------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/rackstash/adapter/file.rb b/lib/rackstash/adapter/file.rb index e7e0a3d..b30157e 100644 --- a/lib/rackstash/adapter/file.rb +++ b/lib/rackstash/adapter/file.rb @@ -65,7 +65,7 @@ module Rackstash register_for ::String, ::Pathname, 'file' # @return [String] the absolute path to the log file - attr_reader :filename + attr_reader :path def self.from_uri(uri) uri = URI(uri) @@ -83,21 +83,21 @@ module Rackstash end # 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 # according to the current working directory. See # [`::File.expand_path`](https://ruby-doc.org/core/File.html#method-c-expand_path) # 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 # 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 # logrotate run - def initialize(filename, auto_reopen: true) - @filename = ::File.expand_path(filename).freeze + def initialize(path, auto_reopen: true) + @path = ::File.expand_path(path).freeze @auto_reopen = !!auto_reopen @mutex = Mutex.new @@ -149,7 +149,7 @@ module Rackstash end # 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. # @return [nil] @@ -162,24 +162,23 @@ module Rackstash 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) def auto_reopen return unless @auto_reopen return if @file.closed? - return if ::File.identical?(@file, @filename) + return if ::File.identical?(@file, @path) reopen_file end def open_file - unless ::File.exist? ::File.dirname(@filename) - FileUtils.mkdir_p ::File.dirname(@filename) - end + dirname = ::File.dirname(path) + FileUtils.mkdir_p(dirname) unless ::File.exist?(dirname) file = ::File.new( - filename, + path, ::File::WRONLY | ::File::APPEND | ::File::CREAT ) file.binmode diff --git a/spec/rackstash/adapter/file_spec.rb b/spec/rackstash/adapter/file_spec.rb index 03fff41..3c7dd88 100644 --- a/spec/rackstash/adapter/file_spec.rb +++ b/spec/rackstash/adapter/file_spec.rb @@ -30,10 +30,10 @@ describe Rackstash::Adapter::File do .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) + it 'sets the path from the URI path' do + expect(described_class.from_uri('file:/tmp/file_spec.log').path) .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' end @@ -53,13 +53,13 @@ describe Rackstash::Adapter::File do describe '#initialize' do it 'accepts a String' do - expect(described_class.new(logfile.path).filename) + expect(described_class.new(logfile.path).path) .to eql(logfile.path) .and be_a String end 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) .and be_a String end @@ -76,7 +76,7 @@ describe Rackstash::Adapter::File do 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.file?(File.join(base, 'dir', 'sub', 'test.log'))).to be true end