Skip to content

Commit bc43e2e

Browse files
committed
Allow overriding the log path with environment variables
1 parent 8ec303c commit bc43e2e

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ Example.
4646

4747
Start your rails server as normal in the set environment. Navigate to /logput e.g. [http://localhost:3000/logput](http://localhost:3000/logput)
4848

49+
## Environment Variable Overrides
50+
51+
It is possible to overide the location of the log files by using the following environment variables:
52+
53+
* `LOG_NAME` - The name of the log file, e.g. `development` - `.log` will be appended to this. If unset logput will try to use `RAILS_ENV` or `RACK_ENV` as a fallback
54+
* `LOG_LOCATION_DIR` - The directory where the log file is located, e.g. `logs` - a `/` will be added when combined with the log name.
55+
56+
The overrides will only be used if both are present (or the fallbacks in the case of the log name).
57+
4958
## Contributing
5059

5160
1. Fork it

lib/logput/adapters/base.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ def self.handles?(_logger)
2929
def path
3030
raise NotImplementedError
3131
end
32+
33+
# Enable overriding of the path with an environment variable
34+
# @return [String] path
35+
def path_override
36+
return unless directory && filename
37+
38+
"#{directory}/#{filename}.log"
39+
end
40+
41+
private
42+
43+
def filename
44+
@filename ||= ENV['LOG_NAME'] || ENV['RAILS_ENV'] || ENV['RACK_ENV']
45+
end
46+
47+
def directory
48+
@directory ||= ENV['LOG_LOCATION_DIR']
49+
end
3250
end
3351
end
3452
end

lib/logput/adapters/logger.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def self.handles?(logger)
1515

1616
# @return [String] path
1717
def path
18+
return path_override if path_override
1819
@logger.instance_variable_get(:@logdev).filename
1920
end
2021
end

lib/logput/adapters/tagged_logging.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def self.handles?(logger)
1515

1616
# @return [String] path
1717
def path
18+
return path_override if path_override
1819
@logger.instance_variable_get(:@logger).instance_variable_get(:@log_dest).path
1920
end
2021
end

spec/adapters/base_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,25 @@ class UnRegisteredAdapter < Logput::Adapters::Base
3333
}.to raise_error(NotImplementedError)
3434
end
3535
end
36+
37+
describe '#path_override' do
38+
subject { described_class.new(:foo) }
39+
40+
after :each do
41+
ENV['LOG_NAME'] = nil
42+
ENV['LOG_LOCATION_DIR'] = nil
43+
end
44+
45+
it 'returns nil if environment variables are not set' do
46+
expect(subject.path_override).to eq(nil)
47+
end
48+
49+
it 'returns a path if the environment variables are set' do
50+
ENV['LOG_NAME'] = 'development'
51+
ENV['LOG_LOCATION_DIR'] = 'logs'
52+
expect(subject.path_override).to eq('logs/development.log')
53+
end
54+
55+
56+
end
3657
end

spec/adapters/logger_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,18 @@
2626
it 'returns the correct path' do
2727
expect(subject.path).to eq(path)
2828
end
29+
30+
context 'when environment variable overrides are present' do
31+
after :each do
32+
ENV['LOG_NAME'] = nil
33+
ENV['LOG_LOCATION_DIR'] = nil
34+
end
35+
36+
it 'returns the overridden path' do
37+
ENV['LOG_NAME'] = 'development'
38+
ENV['LOG_LOCATION_DIR'] = 'logs'
39+
expect(subject.path).to eq('logs/development.log')
40+
end
41+
end
2942
end
3043
end

spec/adapters/tagged_logging_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,18 @@
2929
it 'returns the correct path' do
3030
expect(subject.path).to eq(path)
3131
end
32+
33+
context 'when environment variable overrides are present' do
34+
after :each do
35+
ENV['LOG_NAME'] = nil
36+
ENV['LOG_LOCATION_DIR'] = nil
37+
end
38+
39+
it 'returns the overridden path' do
40+
ENV['LOG_NAME'] = 'development'
41+
ENV['LOG_LOCATION_DIR'] = 'logs'
42+
expect(subject.path).to eq('logs/development.log')
43+
end
44+
end
3245
end
3346
end

0 commit comments

Comments
 (0)