stump 2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bae9bf51d8252bdbd0e75dbfcb11b94f111b0689
4
+ data.tar.gz: 2285b5967d3c8e00954d2e8a8af005439cbde24e
5
+ SHA512:
6
+ metadata.gz: c379a2251d772b2508e5e4f7da346a1813127c207b52f19761610919338e6b622cf1ac4efce42bfc01e6d077ea01fa588a6034aa1138cab2e734facc2d5833b5
7
+ data.tar.gz: 5eb5bc706323e897773399c62a0f76e6d1477a353a74e2ae88e11e6756887d087e8d7d21e3c5798f4604c724fb0b9bc6d3bdcff733e56869d79821e158c5d146
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/*
19
+ .github_token
20
+ .idea/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stump.gemspec
4
+ gemspec
5
+
6
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nicolay Hvidsten
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # Stump
2
+
3
+ [![Code Climate](https://codeclimate.com/github/nicohvi/stump.png)](https://codeclimate.com/github/nicohvi/stump)
4
+ [![Build Status](https://travis-ci.org/nicohvi/stump.png?branch=master)](https://travis-ci.org/nicohvi/stump)
5
+ [![Dependency Status](https://gemnasium.com/nicohvi/stump.png)](https://gemnasium.com/nicohvi/stump)
6
+ [![Gem Version](https://badge.fury.io/rb/stump.png)](http://badge.fury.io/rb/stump)
7
+
8
+ Stump is a minimal wrapper around the basic ruby `logger` that provides
9
+ logging to a log file in addition to STDOUT - something which is for some reason
10
+ rather hard to do for rack-based applications not using Rails. It also provides
11
+ an `access_log` which you can write to STDOUT and (if you wish) a log file.
12
+
13
+ Why call it Stump? It's like a small (tree) log, get it? Get it?
14
+
15
+ ## Usage
16
+
17
+ ````
18
+ # Gemfile
19
+ gem 'stump'
20
+
21
+ # configuration file for your rack-based application
22
+ require 'stump'
23
+
24
+ logger = Stump::Logger.new
25
+ use Stump::AccessLog, logger
26
+
27
+ ````
28
+
29
+ ## Example
30
+ The following example demonstrates usage of `stump`in a vanilla, minimal
31
+ `sinatra` application. You can *literally* copy this code and run the example
32
+ in less than a minute.
33
+
34
+ ````
35
+ # Gemfile
36
+ source 'https://rubygems.org'
37
+
38
+ gem 'sinatra'
39
+ gem 'stump'
40
+
41
+ # hi.rb
42
+ require 'sinatra'
43
+ require 'stump'
44
+
45
+ logger = Stump::Logger.new "tmp/log.log"
46
+ logger.level = Logger::DEBUG
47
+ use Stump::AccessLog, logger
48
+
49
+ get '/hi' do
50
+ logger.debug 'hello'
51
+ 'What came first - the cat or the internet?'
52
+ end
53
+
54
+ ````
55
+
56
+ Run `bundle` and `ruby hi.rb`, point your brower to `localhost:4567/hi`
57
+ and witness the magic.
58
+
59
+
60
+ ## Issues
61
+
62
+ "Hey, it's not working!"
63
+
64
+ This can totally happen, so just open up an issue and I'll get right on it!
65
+
66
+
67
+ ## License
68
+
69
+ Copyright (c) @nicovhi, released under the MIT License.
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => :spec
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,30 @@
1
+ require 'stump/logger_targets'
2
+ require 'stump/access_log'
3
+ require 'stump/version'
4
+ require 'logger'
5
+
6
+ module Stump
7
+
8
+ module Logger
9
+
10
+ #
11
+ # Initialize a new Logger with the desired *targets*: either STDOUT,
12
+ # a log file or both.
13
+ # *path* is an array of file paths.
14
+ #
15
+ def self.new(path=nil)
16
+ return ::Logger.new(LoggerTargets.new(STDOUT)) unless path
17
+
18
+ if FileTest.exist?(path)
19
+ log_file = File.open(path, 'a')
20
+ else
21
+ FileUtils.mkdir_p(File.dirname(path))
22
+ log_file = File.new(path, 'w')
23
+ end
24
+
25
+ ::Logger.new LoggerTargets.new(STDOUT, log_file)
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,57 @@
1
+ module Stump
2
+
3
+ #
4
+ # The Middleware ensures that the logger provided (could be a standard Ruby Logger)
5
+ # gets called by Rack.
6
+ #
7
+ class AccessLog
8
+
9
+ # Adheres to the Apache Common Log format: http://en.wikipedia.org/wiki/Common_Log_Format
10
+ ACCESS_LOG_FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %0.4f \n}
11
+
12
+ def initialize(app, logger)
13
+ @app = app
14
+ @logger = logger || ::Logger.new(STDOUT, 'daily')
15
+ @logger.level ||= 'info'
16
+ end
17
+
18
+ def call(env)
19
+ env['rack.logger'] = @logger
20
+ began_at = Time.now
21
+ status, header, body = @app.call(env)
22
+ log(env, status, began_at)
23
+ [status, header, body]
24
+ end
25
+
26
+ private
27
+
28
+ #
29
+ # Logs access log type messages to the logger's targets if the @access_log instance
30
+ # variable exists
31
+ #
32
+ def log(env, status, began_at)
33
+ now = Time.now
34
+ msg = ACCESS_LOG_FORMAT % [
35
+ env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR'] || '-',
36
+ env['REMOTE_USER'] || '-',
37
+ now.strftime('%d/%b/%Y:%H:%M:%S %z'),
38
+ env['REQUEST_METHOD'],
39
+ env['PATH_INFO'],
40
+ env['QUERY_STRING'].empty? ? '' : '?'+env['QUERY_STRING'],
41
+ env['HTTP_VERSION'],
42
+ status.to_s[0..3],
43
+ now - began_at ]
44
+
45
+ # Standard library logger doesn't support write but it supports << which actually
46
+ # calls to write on the log device without formatting
47
+ # logger = @logger || env['rack.logger']
48
+ if @logger.respond_to?(:write)
49
+ @logger.write(msg)
50
+ else
51
+ @logger << msg
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,28 @@
1
+ module Stump
2
+
3
+ #
4
+ # LoggerTargets gets passed to the Logger.rb instantiate method as its *logdevice*. Thus whenever *write*
5
+ # is called for the logger, the *write* method defined below is called. This method loops through the predefined
6
+ # targets (usually a file and STDOUT) and writes to them. *flush* is necessary due to the fact that *write* is
7
+ # a buffered method.
8
+ #
9
+ class LoggerTargets
10
+
11
+ def initialize(*targets)
12
+ @targets = targets
13
+ end
14
+
15
+ def write(*args)
16
+ @targets.each do |t|
17
+ t.write(*args)
18
+ t.flush
19
+ end
20
+ end
21
+
22
+ def close
23
+ @targets.each(&:close)
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,3 @@
1
+ module Stump
2
+ VERSION = '2.0'
3
+ end
@@ -0,0 +1,24 @@
1
+ require 'stump'
2
+ require 'logger'
3
+
4
+ describe 'file creation' do
5
+
6
+ before :all do
7
+ @log_file_name = 'tmp/test.log'
8
+ end
9
+
10
+ it 'does not create a file when one is not specified in the config' do
11
+ logger = Stump::Logger.new
12
+ File.exists?(@log_file_name).should eq(false)
13
+ end
14
+
15
+ it 'creates a new log file if none exists and path is supplied' do
16
+ logger = Stump::Logger.new(@log_file_name).should be_a(Logger)
17
+ File.exists?(@log_file_name).should eq(true)
18
+ end
19
+
20
+ after :all do
21
+ File.delete('tmp/test.log')
22
+ end
23
+
24
+ end
@@ -0,0 +1,36 @@
1
+ require 'stump'
2
+ require 'logger'
3
+
4
+ describe 'logging to STDOUT' do
5
+
6
+ it 'should log to STDOUT when no path is supplied' do
7
+ logger = Stump::Logger.new
8
+ $stdout.should_receive(:write)
9
+ logger.info('log entry')
10
+ end
11
+
12
+ it 'should log to STDOUT when a path is supplied as well' do
13
+ logger = Stump::Logger.new 'tmp/test.log'
14
+ $stdout.should_receive(:write)
15
+ logger.info('log entry')
16
+ end
17
+
18
+ end
19
+
20
+ describe 'logging to files' do
21
+
22
+ before :all do
23
+ @log_file_name = 'tmp/test.log'
24
+ end
25
+
26
+ after :all do
27
+ File.delete('tmp/test.log')
28
+ end
29
+
30
+ it 'logs to supplied file' do
31
+ logger = Stump::Logger.new @log_file_name
32
+ logger.info('log entry')
33
+ File.readlines(@log_file_name).any? { |line| line['log entry'] }.should eq(true)
34
+ end
35
+
36
+ end
@@ -0,0 +1,38 @@
1
+ require 'stump'
2
+ require 'rack'
3
+
4
+ describe Stump::AccessLog do
5
+
6
+ let(:app) { ->(env) { [200, env, "app"] } }
7
+ let(:log_file_name) { 'tmp/test.log' }
8
+
9
+ after :each do
10
+ File.delete(log_file_name) if File.exist?(log_file_name)
11
+ end
12
+
13
+ it 'should log requests to STDOUT' do
14
+ logger = Stump::Logger.new
15
+ middleware = Stump::AccessLog.new(app, logger)
16
+ $stdout.should_receive(:write)
17
+ middleware.call env_for '/'
18
+ end
19
+
20
+ it 'should create supplied log file' do
21
+ logger = Stump::Logger.new log_file_name
22
+ middleware = Stump::AccessLog.new(app, logger)
23
+ middleware.call env_for '/'
24
+ File.exists?(log_file_name).should eq(true)
25
+ end
26
+
27
+ it 'should log access log messages to supplied log file' do
28
+ logger = Stump::Logger.new log_file_name
29
+ middleware = Stump::AccessLog.new(app, logger)
30
+ middleware.call env_for '/'
31
+ File.readlines(log_file_name).length.should eq(1)
32
+ end
33
+
34
+ def env_for(url)
35
+ Rack::MockRequest.env_for(url)
36
+ end
37
+
38
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stump/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'stump'
8
+ spec.version = Stump::VERSION
9
+ spec.authors = ['Nicolay Hvidsten']
10
+ spec.email = ['nicohvi@gmail.com']
11
+ spec.description = 'Minute logging gem for small rack applications'
12
+ spec.summary = 'Log all the things!'
13
+ spec.homepage = 'http://nplol.com'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'logger'
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rack'
25
+ spec.add_development_dependency 'rspec', '~> 2.6'
26
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stump
3
+ version: !ruby/object:Gem::Version
4
+ version: '2.0'
5
+ platform: ruby
6
+ authors:
7
+ - Nicolay Hvidsten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.6'
83
+ description: Minute logging gem for small rack applications
84
+ email:
85
+ - nicohvi@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/stump.rb
98
+ - lib/stump/access_log.rb
99
+ - lib/stump/logger_targets.rb
100
+ - lib/stump/version.rb
101
+ - spec/file_creation_spec.rb
102
+ - spec/logger_spec.rb
103
+ - spec/middleware_spec.rb
104
+ - stump.gemspec
105
+ homepage: http://nplol.com
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Log all the things!
129
+ test_files:
130
+ - spec/file_creation_spec.rb
131
+ - spec/logger_spec.rb
132
+ - spec/middleware_spec.rb