low 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- low (0.0.2)
4
+ low (0.0.10)
5
5
  bson_ext
6
6
  mongo
7
7
  rack
@@ -0,0 +1,32 @@
1
+ require 'logger'
2
+
3
+ module Low
4
+ module Middleware
5
+ class LogLevel
6
+
7
+ DEFAULT_DEFAULT_LEVEL = Logger::INFO
8
+
9
+ def initialize(app, opts = {})
10
+ @app = app
11
+ @default_level = opts[:default_level] || DEFAULT_DEFAULT_LEVEL
12
+ end
13
+
14
+ def call(env)
15
+ env['low.log_level'] = log_level
16
+ @app.call(env)
17
+ end
18
+
19
+ def log_level
20
+ # If `LOG_LEVEL` is a valid level
21
+ if ['FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG'].include? ENV['LOG_LEVEL']
22
+ # use it;
23
+ eval "Logger::#{ENV['LOG_LEVEL']}"
24
+ else
25
+ # otherwise, use the default
26
+ @default_level
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,33 @@
1
+ module Low
2
+ module Middleware
3
+ class RackErrors
4
+
5
+ DEFAULT_FS_ENVS = ['development', 'test']
6
+
7
+ def initialize(app, opts = {})
8
+ @app = app
9
+ @fs_envs = opts[:fs_envs] || DEFAULT_FS_ENVS
10
+ end
11
+
12
+ def call(env)
13
+ env['rack.errors'] = io
14
+ @app.call(env)
15
+ end
16
+
17
+ def io
18
+ # If `RACK_ENV` should log to the FS,
19
+ if @fs_envs.include? ENV['RACK_ENV']
20
+ # make sure the log directory exists,
21
+ Dir.mkdir('log') unless Dir.exists?('log')
22
+
23
+ # and log to an eponymous file;
24
+ File.open("log/#{ENV['RACK_ENV']}.log", 'a')
25
+ else
26
+ # otherwise, log to STDOUT (Heroku likes it this way).
27
+ STDOUT
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -2,67 +2,26 @@ require 'logger'
2
2
 
3
3
  module Low
4
4
  module Middleware
5
- # `RequestLogger` set the env key "logger" to an instance of
6
- # `Low::ScopedLogger`, with various attributes set according to the state
7
- # of the request:
8
- # * _IO_: if RACK_ENV is development or test, use an eponymous file in
9
- # the log directory, otherwise use STDOUT (as Heroku likes).
10
- # * _log_level_: If set, use the LOG_LEVEL environment variable value;
11
- # otherwise, use INFO.
12
- # * _group_key_: Use request_id env value (see the RequestId middleware).
5
+ # `RequestLogger` sets 'rack.logger' to an instance of
6
+ # `Low::ScopedLogger`, with `#level` and `#scope` taken from env.
13
7
  class RequestLogger
14
- def self.level
15
- # If `LOG_LEVEL` is a valid level other than INFO,
16
- if ['FATAL', 'ERROR', 'WARN', 'DEBUG'].include? ENV['LOG_LEVEL']
17
- # use it;
18
- eval "::Logger::#{ENV['LOG_LEVEL']}"
19
- else
20
- # otherwise, use `::Logger::INFO`
21
- ::Logger::INFO
22
- end
23
- end
24
-
25
- def self.io
26
- # If `RACK_ENV` is development or test,
27
- if ['development', 'test'].include? ENV['RACK_ENV']
28
- # make sure the log directory exists,
29
- Dir.mkdir('log') unless Dir.exists?('log')
30
-
31
- # and log to the appropriate file;
32
- File.open("log/#{ENV['RACK_ENV']}.log", 'a')
33
- else
34
- # otherwise, log to STDOUT (Heroku likes it this way).
35
- STDOUT
36
- end
37
- end
38
-
39
- DEFAULT_KEY = 'logger'
40
8
 
41
9
  def initialize(app, opts = {})
42
10
  @app = app
43
- @key = opts[:key] || DEFAULT_KEY
44
11
  end
45
12
 
46
13
  def call(env)
47
- # Set the 'rack.errors' environment key to the above `RequestLogger.io`
48
- # (other rack components, such as Sinatra, use this),
49
- env['rack.errors'] = RequestLogger.io
14
+ env['rack.logger'] = logger(env)
15
+ @app.call(env)
16
+ end
50
17
 
51
- # instantiate a new `Useless::Logger`,
18
+ def logger(env)
52
19
  logger = Low::ScopedLogger.new(env['rack.errors'])
53
-
54
- # set the logger level to the above `Logger.level`,
55
- logger.level = RequestLogger.level
56
-
57
- # set the request_id if one is available
20
+ logger.level = env['low.log_level'] || env['log_level']
58
21
  logger.scope = env['request_id']
59
-
60
- # add it to the env,
61
- env[@key] = logger
62
-
63
- # and call the app
64
- @app.call(env)
22
+ logger
65
23
  end
24
+
66
25
  end
67
26
  end
68
27
  end
data/lib/low/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Low
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.11'
3
3
  end
data/lib/low.rb CHANGED
@@ -5,6 +5,8 @@ module Low
5
5
  autoload :Mongo, 'low/mongo'
6
6
 
7
7
  module Middleware
8
+ autoload :LogLevel, 'low/middleware/log_level'
9
+ autoload :RackErrors, 'low/middleware/rack_errors'
8
10
  autoload :RequestId, 'low/middleware/request_id'
9
11
  autoload :RequestLogger, 'low/middleware/request_logger'
10
12
  autoload :SubdomainMap, 'low/middleware/subdomain_map'
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+ require 'logger'
3
+
4
+ describe Low::Middleware::LogLevel do
5
+ def test_app
6
+ lambda do |env|
7
+ [200, {'Content-Type' => 'text/plain'}, env['low.log_level']]
8
+ end
9
+ end
10
+
11
+ before(:all) { @original_log_level = ENV['LOG_LEVEL'] }
12
+ after(:each) { ENV['LOG_LEVEL'] = @original_log_level }
13
+
14
+ it 'should set low.log_level to `Logger::FATAL` if LOG_LEVEL is \'FATAL\'' do
15
+ ENV['LOG_LEVEL'] = 'FATAL'
16
+ rack = Low::Middleware::LogLevel.new test_app
17
+ response = rack.call({})
18
+ response[2].should == Logger::FATAL
19
+ end
20
+
21
+ it 'should set low.log_level to `Logger::ERROR` if LOG_LEVEL is \'ERROR\'' do
22
+ ENV['LOG_LEVEL'] = 'ERROR'
23
+ rack = Low::Middleware::LogLevel.new test_app
24
+ response = rack.call({})
25
+ response[2].should == Logger::ERROR
26
+ end
27
+
28
+ it 'should set low.log_level to `Logger::WARN` if LOG_LEVEL is \'WARN\'' do
29
+ ENV['LOG_LEVEL'] = 'WARN'
30
+ rack = Low::Middleware::LogLevel.new test_app
31
+ response = rack.call({})
32
+ response[2].should == Logger::WARN
33
+ end
34
+
35
+ it 'should set low.log_level to `Logger::INFO` if LOG_LEVEL is \'INFO\'' do
36
+ ENV['LOG_LEVEL'] = 'INFO'
37
+ rack = Low::Middleware::LogLevel.new test_app
38
+ response = rack.call({})
39
+ response[2].should == Logger::INFO
40
+ end
41
+
42
+ it 'should set low.log_level to `Logger::DEBUG` if LOG_LEVEL is \'DEBUG\'' do
43
+ ENV['LOG_LEVEL'] = 'DEBUG'
44
+ rack = Low::Middleware::LogLevel.new test_app
45
+ response = rack.call({})
46
+ response[2].should == Logger::DEBUG
47
+ end
48
+
49
+ it 'should set low.log_level to `Logger::INFO` if neither LOG_LEVEL nor `:default_level` is set' do
50
+ ENV['LOG_LEVEL'] = nil
51
+ rack = Low::Middleware::LogLevel.new test_app
52
+ response = rack.call({})
53
+ response[2].should == Logger::INFO
54
+ end
55
+
56
+ it 'should set low.log_level to the specified `:default_level` if LOG_LEVEL is not set' do
57
+ ENV['LOG_LEVEL'] = nil
58
+ rack = Low::Middleware::LogLevel.new test_app, default_level: Logger::DEBUG
59
+ response = rack.call({})
60
+ response[2].should == Logger::DEBUG
61
+ end
62
+ end
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe Low::Middleware::RackErrors do
5
+ def test_app
6
+ lambda do |env|
7
+ [200, {'Content-Type' => 'text/plain'}, env['rack.errors']]
8
+ end
9
+ end
10
+
11
+ after(:each) do
12
+ ENV['RACK_ENV'] = 'test'
13
+ end
14
+
15
+ it 'should log to the FS in the development RACK_ENV by default' do
16
+ ENV['RACK_ENV'] = 'development'
17
+ rack = Low::Middleware::RackErrors.new test_app
18
+ response = rack.call({})
19
+ response[2].path.should == 'log/development.log'
20
+ end
21
+
22
+ it 'should log to the FS in the test RACK_ENV by default' do
23
+ ENV['RACK_ENV'] = 'test'
24
+ rack = Low::Middleware::RackErrors.new test_app
25
+ response = rack.call({})
26
+ response[2].path.should == 'log/test.log'
27
+ end
28
+
29
+ it 'should log to the FS in the specified RACK_ENV' do
30
+ ENV['RACK_ENV'] = 'production'
31
+ rack = Low::Middleware::RackErrors.new test_app, fs_envs: ['production']
32
+ response = rack.call({})
33
+ response[2].path.should == 'log/production.log'
34
+ end
35
+
36
+ it 'should log to STDOUT if not in a specified RACK_ENV' do
37
+ ENV['RACK_ENV'] = 'production'
38
+ rack = Low::Middleware::RackErrors.new test_app
39
+ response = rack.call({})
40
+ response[2].should == STDOUT
41
+ end
42
+ end
@@ -2,60 +2,19 @@ require File.dirname(__FILE__) + '/../../spec_helper'
2
2
  require 'logger'
3
3
 
4
4
  describe Low::Middleware::RequestLogger do
5
- describe '.level' do
6
- it 'should return Logger::FATAL if the LOG_LEVEL evironment var is \'FATAL\'' do
7
- ENV['LOG_LEVEL'] = 'FATAL'
8
- Low::Middleware::RequestLogger.level.should == Logger::FATAL
9
- end
10
-
11
- it 'should return Logger::ERROR if the LOG_LEVEL evironment var is \'ERROR\'' do
12
- ENV['LOG_LEVEL'] = 'ERROR'
13
- Low::Middleware::RequestLogger.level.should == Logger::ERROR
14
- end
15
-
16
- it 'should return Logger::WARN if the LOG_LEVEL evironment var is \'WARN\'' do
17
- ENV['LOG_LEVEL'] = 'WARN'
18
- Low::Middleware::RequestLogger.level.should == Logger::WARN
19
- end
20
-
21
- it 'should return Logger::INFO if the LOG_LEVEL evironment var is \'INFO\'' do
22
- ENV['LOG_LEVEL'] = 'INFO'
23
- Low::Middleware::RequestLogger.level.should == Logger::INFO
24
- end
25
-
26
- it 'should return Logger::DEBUG if the LOG_LEVEL evironment var is \'DEBUG\'' do
27
- ENV['LOG_LEVEL'] = 'DEBUG'
28
- Low::Middleware::RequestLogger.level.should == Logger::DEBUG
29
- end
30
-
31
- it 'should return Logger::INFO if the LOG_LEVEL evironment var is not set' do
32
- ENV['LOG_LEVEL'] = nil
33
- Low::Middleware::RequestLogger.level.should == Logger::INFO
5
+ def test_app
6
+ lambda do |env|
7
+ [200, {'Content-Type' => 'text/plain'}, env['rack.logger']]
34
8
  end
35
9
  end
36
10
 
37
- describe '.io' do
38
- it 'should return \'log/development.log\' if the RACK_ENV evironment var is \'development\'' do
39
- begin
40
- ENV['RACK_ENV'] = 'development'
41
- Low::Middleware::RequestLogger.io.path.should == 'log/development.log'
42
- ensure
43
- ENV['RACK_ENV'] = 'test'
44
- end
45
- end
46
-
47
- it 'should return \'log/test.log\' if the RACK_ENV evironment var is \'test\'' do
48
- ENV['RACK_ENV'] = 'test'
49
- Low::Middleware::RequestLogger.io.path.should == 'log/test.log'
50
- end
51
-
52
- it 'should return STDOUT if the RACK_ENV evironment var is \'production\'' do
53
- begin
54
- ENV['RACK_ENV'] = 'production'
55
- Low::Middleware::RequestLogger.io.should == STDOUT
56
- ensure
57
- ENV['RACK_ENV'] = 'test'
58
- end
59
- end
11
+ it 'should set rack.logger to an instance of `Low::ScopedLogger` with scope set to \'request_id\'
12
+ and level set to \'low.log_level\'' do
13
+ rack = Low::Middleware::RequestLogger.new test_app
14
+ response = rack.call({'request_id' => 'abc123', 'low.log_level' => Logger::FATAL})
15
+ logger = response[2]
16
+ logger.should be_a(Low::ScopedLogger)
17
+ logger.scope.should == 'abc123'
18
+ logger.level.should == Logger::FATAL
60
19
  end
61
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: low
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-12 00:00:00.000000000 Z
12
+ date: 2012-10-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -105,6 +105,8 @@ files:
105
105
  - README.md
106
106
  - Rakefile
107
107
  - lib/low.rb
108
+ - lib/low/middleware/log_level.rb
109
+ - lib/low/middleware/rack_errors.rb
108
110
  - lib/low/middleware/request_id.rb
109
111
  - lib/low/middleware/request_logger.rb
110
112
  - lib/low/middleware/subdomain_map.rb
@@ -114,6 +116,8 @@ files:
114
116
  - lib/low/scoped_logger.rb
115
117
  - lib/low/version.rb
116
118
  - low.gemspec
119
+ - spec/low/middleware/log_level_spec.rb
120
+ - spec/low/middleware/rack_errors_spec.rb
117
121
  - spec/low/middleware/request_id_spec.rb
118
122
  - spec/low/middleware/request_logger_spec.rb
119
123
  - spec/low/middleware/subdomain_map_spec.rb
@@ -146,6 +150,8 @@ signing_key:
146
150
  specification_version: 3
147
151
  summary: A low-level utility library.
148
152
  test_files:
153
+ - spec/low/middleware/log_level_spec.rb
154
+ - spec/low/middleware/rack_errors_spec.rb
149
155
  - spec/low/middleware/request_id_spec.rb
150
156
  - spec/low/middleware/request_logger_spec.rb
151
157
  - spec/low/middleware/subdomain_map_spec.rb