logput 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7cbacb1dd5d03161a1b94546de8454b44fd66084
4
- data.tar.gz: cbcc16f6c0207a1b9d47f6c2e51d9a166a9520c4
3
+ metadata.gz: 19a7457eddef652b545880acae4704d2b6f238da
4
+ data.tar.gz: b2a1859fcc2bdb935192a177ce82de6f1234ef46
5
5
  SHA512:
6
- metadata.gz: 9f2af5f8a8eca97c8e94c465c558087467340feb61aaaa975102ee9949c611f4e7c15464d2ee370ed1e574bcb2167d1684aa10d6b4d0e40a6d4724146fc47962
7
- data.tar.gz: 4d9ba7d77cecfcb8e4c52c6237a9dbf73de61a0d4b30b369bf9f4d1437b30bcb10f8cae10af233d7fbaa158a0e553c07a0a18da9176f61578fbd524b16f47076
6
+ metadata.gz: b910ed111410a841bcf269d45731b026cd75d0a8beb6c64b9ec087032a25f82bf810c57114982fb315893ce58460474f4c4d18fcf32a23a0e9cdf4304826422f
7
+ data.tar.gz: 06e122d67f32127abee46d988ddcb388c4ec761acf6873df197680b15c81cbfce814f6b404006f4cd05210ed12f9ffbecb7764c755bd1a511a895dfda2d30868
data/Fudgefile CHANGED
@@ -15,7 +15,7 @@ task_group :duplication do
15
15
  end
16
16
 
17
17
  task_group :complexity do
18
- task :flog, :exclude => '^\.\/spec\/', :max => 7.7, :average => 5.7, :methods => true
18
+ task :flog, :exclude => '^\.\/spec\/', :methods => true
19
19
  end
20
20
 
21
21
  build :default do
data/README.md CHANGED
@@ -1,15 +1,20 @@
1
1
  # Logput
2
2
 
3
- [![Build Status](https://api.travis-ci.org/asmega/logput.svg)](https://travis-ci.org/asmega/logput)
3
+ [![Build Status](https://api.travis-ci.org/asmega/logput.svg)](https://travis-ci.org/asmega/logput) [![Code Climate](https://codeclimate.com/github/asmega/logput/badges/gpa.svg)](https://codeclimate.com/github/asmega/logput) [![Dependency Status](https://gemnasium.com/asmega/logput.svg)](https://gemnasium.com/asmega/logput) [![Gem Version](https://badge.fury.io/rb/logput.svg)](https://badge.fury.io/rb/logput)
4
4
 
5
5
  Rack middleware to sit in a rails app to put put the current environments log to a webpage. eg /logput
6
6
 
7
7
  To be used in test and development environments to see logs without needing direct access to the box.
8
8
 
9
- This NOT to be used in production like environments.
9
+ :warning: This is *NOT* to be used in production like environments.
10
10
 
11
11
  Supports Rails 3.x.x and 4.x.x
12
12
 
13
+ ## Credits
14
+
15
+ * Phil Lee ([@asmega](https://github.com/asmega)) [Author]
16
+ * Chris Barber ([@chrisbarber86](https://github.com/chrisbarber86))
17
+
13
18
  ## Installation
14
19
 
15
20
  Add this line to your application's Gemfile:
@@ -32,14 +37,14 @@ Inside you rails app. Add the following line to config/development.rb and/or any
32
37
 
33
38
  The following configuration options are available.
34
39
 
35
- * :path_to_log_file => '/path/to/custom/log'. Defaults to current environments log file if in rails.
36
- * :lines_to_read => 1000. Defaults to 500.
40
+ * `:path_to_log_file => '/path/to/custom/log'`. Defaults to current environments log file if in rails.
41
+ * `:lines_to_read => 1000`. Defaults to 500.
37
42
 
38
43
  Example.
39
44
 
40
45
  config.middleware.use(Logput::Middleware, :lines_to_read => 300, :path_to_log_file => './log/delayed_job')
41
46
 
42
- Start you rails server as normal in the set environemnt. Navigate to /logput e.g. [http://localhost:3000/logput](http://localhost:3000/logput)
47
+ Start your rails server as normal in the set environment. Navigate to /logput e.g. [http://localhost:3000/logput](http://localhost:3000/logput)
43
48
 
44
49
  ## Contributing
45
50
 
@@ -0,0 +1,34 @@
1
+ # Logput
2
+ module Logput
3
+ # Logging Adapters
4
+ module Adapters
5
+ # Base class from which all adapters inherit
6
+ class Base
7
+ # Initialize
8
+ def initialize(logger)
9
+ @logger = logger
10
+ end
11
+
12
+ # Registers a new adapter
13
+ #
14
+ # @param [Symbol] adapter The name of the adapter
15
+ def self.register(adapter)
16
+ raise "Already Registered :#{adapter}" if Logput::Adapters.registered_adapters[adapter]
17
+ Logput::Adapters.registered_adapters[adapter] = self
18
+ end
19
+
20
+ # Placeholder for handles? method to be overridden when subclassed
21
+ # @param [Class] _logger
22
+ # @return [Boolean]
23
+ def self.handles?(_logger)
24
+ raise NotImplementedError
25
+ end
26
+
27
+ # Placeholder for path method to be overridden when subclassed
28
+ # @return [String] path
29
+ def path
30
+ raise NotImplementedError
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,22 @@
1
+ # Logput
2
+ module Logput
3
+ # Logging Adapters
4
+ module Adapters
5
+ # Logger Adapter
6
+ class Logger < Base
7
+ register :logger
8
+
9
+ # @param [Class] logger
10
+ # @return [Boolean]
11
+ def self.handles?(logger)
12
+ return false unless logger
13
+ logger.is_a? ::Logger
14
+ end
15
+
16
+ # @return [String] path
17
+ def path
18
+ @logger.instance_variable_get(:@logdev).filename
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # Logput
2
+ module Logput
3
+ # Logging Adapters
4
+ module Adapters
5
+ # Active Support Tagged Logging Adapter
6
+ class TaggedLogging < Base
7
+ register :tagged_logging
8
+
9
+ # @param [Class] logger
10
+ # @return [Boolean]
11
+ def self.handles?(logger)
12
+ return false unless logger
13
+ logger.is_a? ::ActiveSupport::TaggedLogging
14
+ end
15
+
16
+ # @return [String] path
17
+ def path
18
+ @logger.instance_variable_get(:@logger).instance_variable_get(:@log_dest).path
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # Logput
2
+ module Logput
3
+ # Logging Adapters
4
+ module Adapters
5
+ # @return [Hash] Currently registered adapters
6
+ def self.registered_adapters
7
+ @registered_adapters ||= {}
8
+ end
9
+
10
+ # Find a registered adapter
11
+ # @return [Adapter] An instance of the adapter, or raise an exception
12
+ def self.obtain(logger)
13
+ registered_adapters.each do |_, adapter|
14
+ return adapter.new(logger) if adapter.handles?(logger)
15
+ end
16
+ raise "#{logger} is not supported."
17
+ end
18
+
19
+ require 'logput/adapters/base'
20
+ require 'logput/adapters/logger'
21
+ require 'logput/adapters/tagged_logging'
22
+ end
23
+ end
@@ -10,36 +10,48 @@ module Logput
10
10
 
11
11
  # Call
12
12
  def call(env)
13
- @path_to_log_file ||= default_path_to_log_file(env)
13
+ @env = env
14
+ @path_to_log_file ||= default_path_to_log_file
14
15
 
15
- raise 'Log file does not exist' unless File.exists? @path_to_log_file
16
+ ensure_log_file_exists!
16
17
 
17
- if env['PATH_INFO'] == '/logput'
18
- out = `tail -n #{@lines_to_read} #{@path_to_log_file}`
19
- [200, {"Content-Type" => "text/html"}, ["<pre>", out, "</pre>"]]
18
+ if is_logput?
19
+ generate_output!
20
20
  else
21
- @app.call(env)
21
+ @app.call(@env)
22
22
  end
23
23
  end
24
24
 
25
25
  private
26
26
 
27
- def default_path_to_log_file(env)
27
+ def ensure_log_file_exists!
28
+ raise 'Log file does not exist' unless File.exist? @path_to_log_file
29
+ end
30
+
31
+ def is_logput?
32
+ @env['PATH_INFO'] == '/logput'
33
+ end
34
+
35
+ def generate_output!
36
+ out = `tail -n #{@lines_to_read} #{@path_to_log_file}`
37
+ [200, {"Content-Type" => "text/html"}, ["<pre>", out, "</pre>"]]
38
+ end
39
+
40
+ def default_path_to_log_file
28
41
  raise Exception, 'Must specify path to Rails log file' unless defined? Rails
29
- path(logger(env)) || raise(Exception, "#{logger(env).class} not supported.")
42
+ path
30
43
  end
31
44
 
32
- def logger(env)
33
- env['action_dispatch.logger']
45
+ def logger
46
+ @env['action_dispatch.logger']
34
47
  end
35
48
 
36
- def path(logger)
37
- case logger
38
- when ::ActiveSupport::TaggedLogging
39
- logger.instance_variable_get(:@logger).instance_variable_get(:@log_dest).path
40
- when ::Logger
41
- logger.instance_variable_get(:@logdev).filename
42
- end
49
+ def path
50
+ logger_adapter.path
51
+ end
52
+
53
+ def logger_adapter
54
+ @logger_adapter ||= Logput::Adapters.obtain(logger)
43
55
  end
44
56
  end
45
57
  end
@@ -1,5 +1,5 @@
1
1
  # Logput
2
2
  module Logput
3
3
  # Gem version
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
data/lib/logput.rb CHANGED
@@ -4,4 +4,5 @@ require 'rack'
4
4
  # Logput
5
5
  module Logput
6
6
  autoload :Middleware, 'logput/middleware'
7
+ autoload :Adapters, 'logput/adapters'
7
8
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ class UnRegisteredAdapter < Logput::Adapters::Base
4
+ end
5
+
6
+ describe Logput::Adapters::Base do
7
+ describe '.register' do
8
+ after :each do
9
+ Logput::Adapters.registered_adapters.delete(:test)
10
+ end
11
+
12
+ it 'adds the current adapter to the registry' do
13
+ expect(Logput::Adapters.registered_adapters[:test]).to eq(nil)
14
+ UnRegisteredAdapter.register :test
15
+ expect(Logput::Adapters.registered_adapters[:test]).to eq(UnRegisteredAdapter)
16
+ end
17
+ end
18
+
19
+ describe '.handles?' do
20
+ it 'raises a NotImplementedError' do
21
+ expect {
22
+ described_class.handles?(double)
23
+ }.to raise_error(NotImplementedError)
24
+ end
25
+ end
26
+
27
+ describe '#path' do
28
+ subject { described_class.new(:foo) }
29
+
30
+ it 'raises a NotImplementedError' do
31
+ expect {
32
+ subject.path
33
+ }.to raise_error(NotImplementedError)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Logput::Adapters::Logger do
4
+ describe '.handles?' do
5
+ it 'returns true when passed a Logger instance' do
6
+ expect(described_class.handles?(::Logger.new)).to eq(true)
7
+ end
8
+
9
+ it 'returns false when passed anything else' do
10
+ expect(described_class.handles?(::ActiveSupport::TaggedLogging.new)).to eq(false)
11
+ expect(described_class.handles?(:foo)).to eq(false)
12
+ end
13
+ end
14
+
15
+ describe '#path' do
16
+ let(:path) { './spec/support/test.log' }
17
+ let(:logger) { ::Logger.new }
18
+ let(:logdev) { double(:filename => path) }
19
+
20
+ before :each do
21
+ allow(logger).to receive(:instance_variable_get).with(:@logdev).and_return(logdev)
22
+ end
23
+
24
+ subject { described_class.new(logger) }
25
+
26
+ it 'returns the correct path' do
27
+ expect(subject.path).to eq(path)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Logput::Adapters::TaggedLogging do
4
+ describe '.handles?' do
5
+ it 'returns true when passed an ActiveSupport::TaggedLogging instance' do
6
+ expect(described_class.handles?(::ActiveSupport::TaggedLogging.new)).to eq(true)
7
+ end
8
+
9
+ it 'returns false when passed anything else' do
10
+ expect(described_class.handles?(::Logger.new)).to eq(false)
11
+ expect(described_class.handles?(:foo)).to eq(false)
12
+ end
13
+ end
14
+
15
+ describe '#path' do
16
+ let(:path) { './spec/support/test.log' }
17
+ let(:logger) { ::ActiveSupport::TaggedLogging.new }
18
+ let(:logvar) { double }
19
+ let(:logdev) { double(:filename => path) }
20
+ let(:log_dest) { double(:path => path) }
21
+
22
+ before :each do
23
+ allow(logger).to receive(:instance_variable_get).with(:@logger).and_return(logvar)
24
+ allow(logvar).to receive(:instance_variable_get).with(:@log_dest).and_return(log_dest)
25
+ end
26
+
27
+ subject { described_class.new(logger) }
28
+
29
+ it 'returns the correct path' do
30
+ expect(subject.path).to eq(path)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ class TestAdapter
4
+ def initialize(logger); end
5
+ def self.handles?(logger); false; end
6
+ end
7
+
8
+ describe Logput::Adapters do
9
+
10
+ describe '.registered_adapters' do
11
+ let(:expected) {{
12
+ logger: Logput::Adapters::Logger,
13
+ tagged_logging: Logput::Adapters::TaggedLogging
14
+ }}
15
+
16
+ it 'returns a hash of registered adapters' do
17
+ expect(described_class.registered_adapters).to eq(expected)
18
+ end
19
+ end
20
+
21
+ describe '.obtain' do
22
+ before :each do
23
+ Logput::Adapters.registered_adapters[:test] = TestAdapter
24
+ end
25
+
26
+ context 'when an adapter matches' do
27
+ it 'returns an instance of a matching adapter' do
28
+ allow(TestAdapter).to receive(:handles?).with(:bar).and_return(true)
29
+ expect(described_class.obtain(:bar)).to be_a(TestAdapter)
30
+ end
31
+ end
32
+
33
+ context 'when no adapter matches' do
34
+ it 'raises a logger not supported exception' do
35
+ expect {
36
+ described_class.obtain(:foo)
37
+ }.to raise_error
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,9 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- module ActiveSupport; class TaggedLogging; end; end
4
- class Logger; end
5
- class Rails; end
6
-
7
3
  describe Logput::Middleware do
8
4
  subject{ described_class.new(app, :path_to_log_file => './spec/support/test.log') }
9
5
 
data/spec/spec_helper.rb CHANGED
@@ -22,3 +22,7 @@ RSpec.configure do |config|
22
22
 
23
23
  config.requires = ['logput']
24
24
  end
25
+
26
+ module ActiveSupport; class TaggedLogging; end; end
27
+ class Logger; end
28
+ class Rails; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logput
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-27 00:00:00.000000000 Z
11
+ date: 2016-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -166,9 +166,17 @@ files:
166
166
  - README.md
167
167
  - Rakefile
168
168
  - lib/logput.rb
169
+ - lib/logput/adapters.rb
170
+ - lib/logput/adapters/base.rb
171
+ - lib/logput/adapters/logger.rb
172
+ - lib/logput/adapters/tagged_logging.rb
169
173
  - lib/logput/middleware.rb
170
174
  - lib/logput/version.rb
171
175
  - logput.gemspec
176
+ - spec/adapters/base_spec.rb
177
+ - spec/adapters/logger_spec.rb
178
+ - spec/adapters/tagged_logging_spec.rb
179
+ - spec/adapters_spec.rb
172
180
  - spec/middleware_spec.rb
173
181
  - spec/spec_helper.rb
174
182
  - spec/support/test.log
@@ -192,11 +200,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
200
  version: '0'
193
201
  requirements: []
194
202
  rubyforge_project:
195
- rubygems_version: 2.2.3
203
+ rubygems_version: 2.2.5
196
204
  signing_key:
197
205
  specification_version: 4
198
206
  summary: Rack middleware to output rails logs to a webpage
199
207
  test_files:
208
+ - spec/adapters/base_spec.rb
209
+ - spec/adapters/logger_spec.rb
210
+ - spec/adapters/tagged_logging_spec.rb
211
+ - spec/adapters_spec.rb
200
212
  - spec/middleware_spec.rb
201
213
  - spec/spec_helper.rb
202
214
  - spec/support/test.log