Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KNAPSACK_PRO_LOG_DIR to set directory where to write logs #79

Merged
merged 5 commits into from
Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

### 1.7.0

* Add `KNAPSACK_PRO_LOG_DIR` to set directory where to write logs

https://github.com/KnapsackPro/knapsack_pro-ruby/pull/79

https://github.com/KnapsackPro/knapsack_pro-ruby/compare/v1.6.0...v1.7.0

### 1.6.0

* Retry request 3 times when API returns 5xx HTTP status
Expand Down
95 changes: 58 additions & 37 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions lib/knapsack_pro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def root
end

def logger
if KnapsackPro::Config::Env.log_dir
default_logger = Logger.new("#{KnapsackPro::Config::Env.log_dir}/knapsack_pro_node_#{KnapsackPro::Config::Env.ci_node_index}.log")
default_logger.level = KnapsackPro::Config::Env.log_level
self.logger = default_logger
end

unless @logger
default_logger = ::Logger.new(STDOUT)
default_logger.level = KnapsackPro::Config::Env.log_level
Expand Down
4 changes: 4 additions & 0 deletions lib/knapsack_pro/config/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ def log_level
LOG_LEVELS[ENV['KNAPSACK_PRO_LOG_LEVEL'].to_s.downcase] || ::Logger::DEBUG
end

def log_dir
ENV['KNAPSACK_PRO_LOG_DIR']
end

private

def required_env(env_name)
Expand Down
14 changes: 14 additions & 0 deletions spec/knapsack_pro/config/env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -701,4 +701,18 @@
it { should eql ::Logger::DEBUG }
end
end

describe '.log_dir' do
subject { described_class.log_dir }

context 'when ENV set to directory path' do
let(:log_dir) { 'log' }
before { stub_const('ENV', { 'KNAPSACK_PRO_LOG_DIR' => log_dir }) }
it { should eql 'log' }
end

context "when ENV doesn't exist" do
it { should be_nil }
end
end
end
33 changes: 33 additions & 0 deletions spec/knapsack_pro_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@
before { described_class.reset_logger! }
after { described_class.reset_logger! }

context 'when KNAPSACK_PRO_LOG_DIR is set' do
let(:logger) { instance_double(Logger) }

context 'when KNAPSACK_PRO_CI_NODE_INDEX is set' do
before do
stub_const('ENV', {
'KNAPSACK_PRO_LOG_DIR' => 'log',
'KNAPSACK_PRO_CI_NODE_INDEX' => 1,
})

expect(Logger).to receive(:new).with('log/knapsack_pro_node_1.log').and_return(logger)
expect(logger).to receive(:level=).with(Logger::DEBUG)
expect(KnapsackPro::LoggerWrapper).to receive(:new).with(logger).and_return(logger_wrapper)
end

it { should eql logger_wrapper }
end

context 'when KNAPSACK_PRO_CI_NODE_INDEX is not set' do
before do
stub_const('ENV', {
'KNAPSACK_PRO_LOG_DIR' => 'log',
})

expect(Logger).to receive(:new).with('log/knapsack_pro_node_0.log').and_return(logger)
expect(logger).to receive(:level=).with(Logger::DEBUG)
expect(KnapsackPro::LoggerWrapper).to receive(:new).with(logger).and_return(logger_wrapper)
end

it { should eql logger_wrapper }
end
end

context 'when default logger' do
let(:logger) { instance_double(Logger) }

Expand Down