sinatra-logger 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +23 -0
- data/LICENSE +20 -0
- data/README.rdoc +168 -0
- data/Rakefile +90 -0
- data/VERSION +1 -0
- data/lib/sinatra/logger.rb +185 -0
- data/sinatra-logger.gemspec +61 -0
- data/spec/sinatra/logger_spec.rb +194 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +61 -0
- metadata +115 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 kematzy
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
= Sinatra::Logger
|
2
|
+
|
3
|
+
A Sinatra extension that makes logging within Your apps easy.
|
4
|
+
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
# Add Gemcutter to your RubyGems sources
|
9
|
+
$ gem sources -a http://gemcutter.com
|
10
|
+
|
11
|
+
$ (sudo)? gem install sinatra-logger
|
12
|
+
|
13
|
+
== Dependencies
|
14
|
+
|
15
|
+
This Gem depends upon the following:
|
16
|
+
|
17
|
+
=== Runtime:
|
18
|
+
|
19
|
+
* sinatra ( >= 1.0.a )
|
20
|
+
* logger
|
21
|
+
|
22
|
+
=== Development & Tests:
|
23
|
+
|
24
|
+
* rspec (>= 1.3.0 )
|
25
|
+
* rack-test (>= 0.5.3)
|
26
|
+
* rspec_hpricot_matchers (>= 0.1.0)
|
27
|
+
* sinatra-tests (>= 0.1.6)
|
28
|
+
|
29
|
+
|
30
|
+
== Getting Started
|
31
|
+
|
32
|
+
To get logging in your app, just register the extension
|
33
|
+
in your sub-classed Sinatra app:
|
34
|
+
|
35
|
+
class YourApp < Sinatra::Base
|
36
|
+
|
37
|
+
register(Sinatra::Logger)
|
38
|
+
<snip...>
|
39
|
+
end
|
40
|
+
|
41
|
+
Then in your App's route or helper method declarations, just use the <tt>#logger</tt>...
|
42
|
+
|
43
|
+
get '/some/route' do
|
44
|
+
logger.debug("some informative message goes here")
|
45
|
+
<snip...>
|
46
|
+
end
|
47
|
+
|
48
|
+
helpers do
|
49
|
+
def some_helper_method
|
50
|
+
logger.info("some equally informative message goes here")
|
51
|
+
<snip...>
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
That's pretty painless, no?
|
57
|
+
|
58
|
+
|
59
|
+
=== Logging Levels
|
60
|
+
|
61
|
+
The <b>default Log level</b> is <tt>:warn</tt>.
|
62
|
+
|
63
|
+
All the available logging levels are those of Logger[http://ruby-doc.org/stdlib/libdoc/logger/rdoc/classes/Logger.html], which are:
|
64
|
+
|
65
|
+
* <tt>logger.fatal(msg)</tt> - - (FATAL) - an unhandleable error that results in a program crash
|
66
|
+
|
67
|
+
* <tt>logger.error(msg)</tt> - - (ERROR) - a handleable error condition
|
68
|
+
|
69
|
+
* <tt>logger.warn(msg)</tt> - - (WARN) - a warning
|
70
|
+
|
71
|
+
* <tt>logger.info(msg)</tt> - - (INFO) - generic (useful) information about system operation
|
72
|
+
|
73
|
+
* <tt>logger.debug(msg)</tt> - - (DEBUG) - low-level information for developers
|
74
|
+
|
75
|
+
|
76
|
+
OK, by now you might be asking yourself,
|
77
|
+
|
78
|
+
<em>"So where does the log messages go then ?"</em>.
|
79
|
+
|
80
|
+
|
81
|
+
=== Logging Locations
|
82
|
+
|
83
|
+
By default the logger will log it's message to the following path:
|
84
|
+
|
85
|
+
< the root of your app >/log/< environment >.log
|
86
|
+
|
87
|
+
In other words if your app's root is [ <tt>/home/www/your-great-app/</tt> ] and it's
|
88
|
+
running in <tt>:production</tt> mode, then the log location would become:
|
89
|
+
|
90
|
+
/home/www/your-great-app/log/production.log
|
91
|
+
|
92
|
+
<b>NB!</b> this extension takes for granted that you have a ../log/ directory with write access at the root of your app.
|
93
|
+
|
94
|
+
|
95
|
+
=== Custom Logging Location
|
96
|
+
|
97
|
+
If the defaults are NOT for you, then just do...
|
98
|
+
|
99
|
+
class YourApp < Sinatra::Base
|
100
|
+
|
101
|
+
register(Sinatra::Logger)
|
102
|
+
|
103
|
+
set: :logger_log_file, lambda { "/path/2/your/log/file.ext" }
|
104
|
+
|
105
|
+
<snip...>
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
# the lambda { } is required, especially if you have variables in the path
|
111
|
+
|
112
|
+
..., now your log messages will be written to that log file.
|
113
|
+
|
114
|
+
|
115
|
+
=== Setting Log Level
|
116
|
+
|
117
|
+
Finally, to use a different Log level for your app, other than the default <tt>:warn</tt> just...
|
118
|
+
|
119
|
+
class YourApp < Sinatra::Base
|
120
|
+
|
121
|
+
register(Sinatra::Logger)
|
122
|
+
|
123
|
+
set: :logger_level, :fatal # or :error, :warn, :info, :debug
|
124
|
+
<snip...>
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
That's it. I hope that's easy enough.
|
129
|
+
|
130
|
+
|
131
|
+
== RTFM
|
132
|
+
|
133
|
+
If the above is not clear enough, please check the Specs for a better understanding.
|
134
|
+
|
135
|
+
|
136
|
+
== Errors / Bugs
|
137
|
+
|
138
|
+
If something is not behaving intuitively, it is a bug, and should be reported.
|
139
|
+
Report it here: http://github.com/kematzy/sinatra-logger/issues
|
140
|
+
|
141
|
+
|
142
|
+
== TODOs
|
143
|
+
|
144
|
+
* Making the logging work with Rack::CommonLogger, but still retain it's independence.
|
145
|
+
|
146
|
+
* Any other improvements you can think of.
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
== Note on Patches/Pull Requests
|
151
|
+
|
152
|
+
* Fork the project.
|
153
|
+
* Make your feature addition or bug fix.
|
154
|
+
* Add tests for it. This is important so I don't break it in a
|
155
|
+
future version unintentionally.
|
156
|
+
* Commit, do not mess with rakefile, version, or history.
|
157
|
+
* (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
158
|
+
* Send me a pull request. Bonus points for topic branches.
|
159
|
+
|
160
|
+
== Copyright
|
161
|
+
|
162
|
+
Copyright (c) 2010 kematzy. Released under the MIT License.
|
163
|
+
|
164
|
+
See LICENSE for details.
|
165
|
+
|
166
|
+
== Inspirational Source
|
167
|
+
|
168
|
+
* Monk Glue
|
data/Rakefile
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "sinatra-logger"
|
8
|
+
gem.summary = %Q{Easy logging with Sinatra}
|
9
|
+
gem.description = %Q{A Sinatra extension that makes logging easy}
|
10
|
+
gem.email = "kematzy@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/kematzy/sinatra-logger"
|
12
|
+
gem.authors = ["kematzy"]
|
13
|
+
gem.add_dependency( "sinatra", ">= 0.10.1")
|
14
|
+
gem.add_development_dependency( "rspec", ">= 1.3.0")
|
15
|
+
gem.add_development_dependency( "sinatra-tests", ">= 0.1.6")
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
|
27
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
31
|
+
spec.libs << 'lib' << 'spec'
|
32
|
+
spec.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
|
33
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
34
|
+
spec.rcov = true
|
35
|
+
end
|
36
|
+
|
37
|
+
namespace :spec do
|
38
|
+
|
39
|
+
desc "Run all specifications quietly"
|
40
|
+
Spec::Rake::SpecTask.new(:quiet) do |t|
|
41
|
+
t.libs << "lib"
|
42
|
+
t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"]
|
43
|
+
end
|
44
|
+
|
45
|
+
desc "Run specific spec (SPEC=/path/2/file)"
|
46
|
+
Spec::Rake::SpecTask.new(:select) do |t|
|
47
|
+
t.libs << "lib"
|
48
|
+
t.spec_files = [ENV["SPEC"]]
|
49
|
+
t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
task :spec => :check_dependencies
|
55
|
+
|
56
|
+
task :default => :spec
|
57
|
+
|
58
|
+
require 'rake/rdoctask'
|
59
|
+
Rake::RDocTask.new do |rdoc|
|
60
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
61
|
+
|
62
|
+
rdoc.rdoc_dir = 'rdoc'
|
63
|
+
rdoc.title = "sinatra-logger #{version}"
|
64
|
+
rdoc.rdoc_files.include('README*')
|
65
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
desc 'Build the rdoc HTML Files'
|
70
|
+
task :docs do
|
71
|
+
version = File.exist?('VERSION') ? IO.read('VERSION').chomp : "[Unknown]"
|
72
|
+
|
73
|
+
sh "sdoc -N --title 'Sinatra::Logger v#{version}' lib/ README.rdoc"
|
74
|
+
end
|
75
|
+
|
76
|
+
namespace :docs do
|
77
|
+
|
78
|
+
desc 'Remove rdoc products'
|
79
|
+
task :remove => [:clobber_rdoc]
|
80
|
+
|
81
|
+
desc 'Force a rebuild of the RDOC files'
|
82
|
+
task :rebuild => [:rerdoc]
|
83
|
+
|
84
|
+
desc 'Build docs, and open in browser for viewing (specify BROWSER)'
|
85
|
+
task :open => [:docs] do
|
86
|
+
browser = ENV["BROWSER"] || "safari"
|
87
|
+
sh "open -a #{browser} doc/index.html"
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,185 @@
|
|
1
|
+
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
|
7
|
+
# = Sinatra::Logger
|
8
|
+
#
|
9
|
+
# A Sinatra extension that makes logging within Your apps easy.
|
10
|
+
#
|
11
|
+
#
|
12
|
+
# == Installation
|
13
|
+
#
|
14
|
+
# # Add Gemcutter to your RubyGems sources
|
15
|
+
# $ gem sources -a http://gemcutter.com
|
16
|
+
#
|
17
|
+
# $ (sudo)? gem install sinatra-logger
|
18
|
+
#
|
19
|
+
# == Dependencies
|
20
|
+
#
|
21
|
+
# This Gem depends upon the following:
|
22
|
+
#
|
23
|
+
# === Runtime:
|
24
|
+
#
|
25
|
+
# * sinatra ( >= 1.0.a )
|
26
|
+
# * logger
|
27
|
+
#
|
28
|
+
# === Development & Tests:
|
29
|
+
#
|
30
|
+
# * rspec (>= 1.3.0 )
|
31
|
+
# * rack-test (>= 0.5.3)
|
32
|
+
# * rspec_hpricot_matchers (>= 0.1.0)
|
33
|
+
# * sinatra-tests (>= 0.1.6)
|
34
|
+
#
|
35
|
+
#
|
36
|
+
# == Getting Started
|
37
|
+
#
|
38
|
+
# To get logging in your app, just register the extension
|
39
|
+
# in your sub-classed Sinatra app:
|
40
|
+
#
|
41
|
+
# class YourApp < Sinatra::Base
|
42
|
+
#
|
43
|
+
# register(Sinatra::Logger)
|
44
|
+
# <snip...>
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# Then in your App's route or helper method declarations, just use the <tt>#logger</tt>...
|
48
|
+
#
|
49
|
+
# get '/some/route' do
|
50
|
+
# logger.debug("some informative message goes here")
|
51
|
+
# <snip...>
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# helpers do
|
55
|
+
# def some_helper_method
|
56
|
+
# logger.info("some equally informative message goes here")
|
57
|
+
# <snip...>
|
58
|
+
# end
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
#
|
62
|
+
# That's pretty painless, no?
|
63
|
+
#
|
64
|
+
#
|
65
|
+
# === Logging Levels
|
66
|
+
#
|
67
|
+
# The <b>default Log level</b> is <tt>:warn</tt>.
|
68
|
+
#
|
69
|
+
# All the available logging levels are those of Logger[http://ruby-doc.org/stdlib/libdoc/logger/rdoc/classes/Logger.html], which are:
|
70
|
+
#
|
71
|
+
# * <tt>logger.fatal(msg)</tt> - - (FATAL) - an unhandleable error that results in a program crash
|
72
|
+
#
|
73
|
+
# * <tt>logger.error(msg)</tt> - - (ERROR) - a handleable error condition
|
74
|
+
#
|
75
|
+
# * <tt>logger.warn(msg)</tt> - - (WARN) - a warning
|
76
|
+
#
|
77
|
+
# * <tt>logger.info(msg)</tt> - - (INFO) - generic (useful) information about system operation
|
78
|
+
#
|
79
|
+
# * <tt>logger.debug(msg)</tt> - - (DEBUG) - low-level information for developers
|
80
|
+
#
|
81
|
+
#
|
82
|
+
# OK, by now you might be asking yourself,
|
83
|
+
#
|
84
|
+
# <em>"So where does the log messages go then ?"</em>.
|
85
|
+
#
|
86
|
+
#
|
87
|
+
# === Logging Locations
|
88
|
+
#
|
89
|
+
# By default the logger will log it's message to the following path:
|
90
|
+
#
|
91
|
+
# < the root of your app >/log/< environment >.log
|
92
|
+
#
|
93
|
+
# In other words if your app's root is [ <tt>/home/www/your-great-app/</tt> ] and it's
|
94
|
+
# running in <tt>:production</tt> mode, then the log location would become:
|
95
|
+
#
|
96
|
+
# /home/www/your-great-app/log/production.log
|
97
|
+
#
|
98
|
+
# <b>NB!</b> this extension takes for granted that you have a ../log/ directory with write access at the root of your app.
|
99
|
+
#
|
100
|
+
#
|
101
|
+
# === Custom Logging Location
|
102
|
+
#
|
103
|
+
# If the defaults are NOT for you, then just do...
|
104
|
+
#
|
105
|
+
# class YourApp < Sinatra::Base
|
106
|
+
#
|
107
|
+
# register(Sinatra::Logger)
|
108
|
+
#
|
109
|
+
# set: :logger_log_file, lambda { "/path/2/your/log/file.ext" }
|
110
|
+
#
|
111
|
+
# <snip...>
|
112
|
+
#
|
113
|
+
# end
|
114
|
+
#
|
115
|
+
#
|
116
|
+
# # the lambda { } is required, especially if you have variables in the path
|
117
|
+
#
|
118
|
+
# ..., now your log messages will be written to that log file.
|
119
|
+
#
|
120
|
+
#
|
121
|
+
# === Setting Log Level
|
122
|
+
#
|
123
|
+
# Finally, to use a different Log level for your app, other than the default <tt>:warn</tt> just...
|
124
|
+
#
|
125
|
+
# class YourApp < Sinatra::Base
|
126
|
+
#
|
127
|
+
# register(Sinatra::Logger)
|
128
|
+
#
|
129
|
+
# set: :logger_level, :fatal # or :error, :warn, :info, :debug
|
130
|
+
# <snip...>
|
131
|
+
# end
|
132
|
+
#
|
133
|
+
#
|
134
|
+
# That's it. I hope that's easy enough.
|
135
|
+
#
|
136
|
+
module Logger
|
137
|
+
|
138
|
+
VERSION = '0.1.0'
|
139
|
+
##
|
140
|
+
# Returns the version string for this extension
|
141
|
+
#
|
142
|
+
# ==== Examples
|
143
|
+
#
|
144
|
+
# Sinatra::Logger.version => 'Sinatra::Logger v0.1.0'
|
145
|
+
#
|
146
|
+
def self.version; "Sinatra::Logger v#{VERSION}"; end
|
147
|
+
|
148
|
+
|
149
|
+
module Helpers
|
150
|
+
|
151
|
+
##
|
152
|
+
# Provides easy access to the Logger object throughout your
|
153
|
+
# application
|
154
|
+
#
|
155
|
+
# ==== Examples
|
156
|
+
#
|
157
|
+
# logger.warn("messsage")
|
158
|
+
#
|
159
|
+
#
|
160
|
+
# @api public
|
161
|
+
def logger
|
162
|
+
@logger ||= begin
|
163
|
+
@logger = ::Logger.new(self.class.logger_log_file)
|
164
|
+
@logger.level = ::Logger.const_get((self.class.logger_level || :warn).to_s.upcase)
|
165
|
+
@logger.datetime_format = "%Y-%m-%d %H:%M:%S"
|
166
|
+
@logger
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
end #/module Helpers
|
171
|
+
|
172
|
+
|
173
|
+
def self.registered(app)
|
174
|
+
app.helpers Sinatra::Logger::Helpers
|
175
|
+
|
176
|
+
# set the output log level
|
177
|
+
app.set :logger_level, :warn
|
178
|
+
# set the full path to the log file
|
179
|
+
app.set :logger_log_file, lambda { File.join(root, 'log', "#{environment}.log") }
|
180
|
+
|
181
|
+
end #/ self.registered
|
182
|
+
|
183
|
+
end #/module Logger
|
184
|
+
|
185
|
+
end #/ Sinatra
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{sinatra-logger}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["kematzy"]
|
12
|
+
s.date = %q{2010-02-23}
|
13
|
+
s.description = %q{A Sinatra extension that makes logging easy}
|
14
|
+
s.email = %q{kematzy@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/sinatra/logger.rb",
|
27
|
+
"sinatra-logger.gemspec",
|
28
|
+
"spec/sinatra/logger_spec.rb",
|
29
|
+
"spec/spec.opts",
|
30
|
+
"spec/spec_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/kematzy/sinatra-logger}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.6}
|
36
|
+
s.summary = %q{Easy logging with Sinatra}
|
37
|
+
s.test_files = [
|
38
|
+
"spec/sinatra/logger_spec.rb",
|
39
|
+
"spec/spec_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_runtime_dependency(%q<sinatra>, [">= 0.10.1"])
|
48
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
49
|
+
s.add_development_dependency(%q<sinatra-tests>, [">= 0.1.6"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<sinatra>, [">= 0.10.1"])
|
52
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
53
|
+
s.add_dependency(%q<sinatra-tests>, [">= 0.1.6"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<sinatra>, [">= 0.10.1"])
|
57
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
58
|
+
s.add_dependency(%q<sinatra-tests>, [">= 0.1.6"])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
@@ -0,0 +1,194 @@
|
|
1
|
+
|
2
|
+
require File.expand_path(File.dirname(File.dirname(__FILE__)) + '/spec_helper')
|
3
|
+
|
4
|
+
describe "Sinatra" do
|
5
|
+
|
6
|
+
class MyTestApp
|
7
|
+
register Sinatra::Logger
|
8
|
+
|
9
|
+
helpers do
|
10
|
+
|
11
|
+
def dummy_helper(level, message)
|
12
|
+
logger.send(level, message)
|
13
|
+
"Level: #{level}, Message: #{message}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
get '/logger/:msg' do
|
18
|
+
logger.warn("Message: #{params[:msg]}")
|
19
|
+
erb("Message: #{params[:msg]}", :layout => false )
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
it_should_behave_like "MyTestApp"
|
25
|
+
|
26
|
+
describe "Logger" do
|
27
|
+
|
28
|
+
describe "with Default Settings" do
|
29
|
+
|
30
|
+
before(:each) do
|
31
|
+
FileUtils.mkdir_p "#{fixtures_path}/log"
|
32
|
+
@log_file = "#{fixtures_path}/log/#{MyTestApp.environment}.log"
|
33
|
+
`touch #{@log_file}`
|
34
|
+
end
|
35
|
+
|
36
|
+
after(:each) do
|
37
|
+
`echo '' > #{@log_file}`
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "Configuration" do
|
41
|
+
|
42
|
+
it "should set :logger_level to :warn" do
|
43
|
+
app.settings.logger_level.should == :warn
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should set :logger_log_file to [../log/< environment >.log]" do
|
47
|
+
app.settings.logger_log_file.should == "#{fixtures_path}/log/test.log"
|
48
|
+
end
|
49
|
+
|
50
|
+
end #/ Configuration
|
51
|
+
|
52
|
+
describe "Helpers" do
|
53
|
+
|
54
|
+
describe "#logger" do
|
55
|
+
|
56
|
+
it "should create a #{MyTestApp.environment}.log file when initialized" do
|
57
|
+
test(?f, @log_file).should == true
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return an instance of Logger" do
|
61
|
+
app.should respond_to(:logger)
|
62
|
+
app.logger.should be_a_kind_of(::Logger)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should log when called from within a route" do
|
66
|
+
get('/logger/this-works')
|
67
|
+
body.should == "Message: this-works"
|
68
|
+
IO.read(@log_file).should match(/WARN -- : Message: this-works/)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should log when called from within helper methods" do
|
72
|
+
erb_app '<%= dummy_helper(:warn, "works too") %>'
|
73
|
+
IO.read(@log_file).should match(/WARN -- : works too/)
|
74
|
+
body.should == "Level: warn, Message: works too"
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should NOT log lower levels" do
|
78
|
+
erb_app '<%= dummy_helper(:info, "default-info") %>'
|
79
|
+
IO.read(@log_file).should_not match(/INFO -- : default-info/)
|
80
|
+
body.should == "Level: info, Message: default-info"
|
81
|
+
|
82
|
+
erb_app '<%= dummy_helper(:debug, "default-debug") %>'
|
83
|
+
IO.read(@log_file).should_not match(/DEBUG -- : default-debug/)
|
84
|
+
body.should == "Level: debug, Message: default-debug"
|
85
|
+
end
|
86
|
+
|
87
|
+
end #/ #logger
|
88
|
+
|
89
|
+
end #/ Helpers
|
90
|
+
|
91
|
+
end #/ with Default Settings
|
92
|
+
|
93
|
+
describe "with Custom Settings" do
|
94
|
+
|
95
|
+
class MyCustomTestApp
|
96
|
+
register Sinatra::Logger
|
97
|
+
|
98
|
+
set :logger_level, :debug
|
99
|
+
set :logger_log_file, lambda { "#{fixtures_path}/log/custom.log" }
|
100
|
+
|
101
|
+
helpers do
|
102
|
+
|
103
|
+
def dummy_helper(level, message)
|
104
|
+
logger.send(level, message)
|
105
|
+
"Level: #{level}, Message: #{message}"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
get '/customlogger/:msg' do
|
110
|
+
logger.warn("Message: #{params[:msg]}")
|
111
|
+
erb("CustomMessage: #{params[:msg]}", :layout => false )
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
before(:each) do
|
117
|
+
class ::Test::Unit::TestCase
|
118
|
+
def app; ::MyCustomTestApp.new ; end
|
119
|
+
end
|
120
|
+
@app = app
|
121
|
+
|
122
|
+
FileUtils.mkdir_p "#{fixtures_path}/log"
|
123
|
+
@custom_log_file = "#{fixtures_path}/log/custom.log"
|
124
|
+
`touch #{@custom_log_file}`
|
125
|
+
end
|
126
|
+
|
127
|
+
after(:each) do
|
128
|
+
class ::Test::Unit::TestCase
|
129
|
+
def app; nil ; end
|
130
|
+
end
|
131
|
+
@app = nil
|
132
|
+
|
133
|
+
`echo '' > #{@custom_log_file}`
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "Configuration" do
|
137
|
+
|
138
|
+
it "should set :logger_level to :debug" do
|
139
|
+
app.settings.logger_level.should == :debug
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should set :logger_log_file to [../log/custom.log]" do
|
143
|
+
app.settings.logger_log_file.should == "#{fixtures_path}/log/custom.log"
|
144
|
+
end
|
145
|
+
|
146
|
+
end #/ Configuration
|
147
|
+
|
148
|
+
describe "Helpers" do
|
149
|
+
|
150
|
+
describe "#logger" do
|
151
|
+
|
152
|
+
it "should create a custom.log file when initialised" do
|
153
|
+
test(?f, @custom_log_file).should == true
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should return an instance of Logger" do
|
157
|
+
app.logger.should be_a_kind_of(::Logger)
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should log when called from within a route" do
|
161
|
+
get('/customlogger/this-works')
|
162
|
+
body.should == "CustomMessage: this-works"
|
163
|
+
IO.read(@custom_log_file).should match(/WARN -- : Message: this-works/)
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should log when called from within helper methods" do
|
167
|
+
erb_app '<%= dummy_helper(:info, "works as well") %>'
|
168
|
+
IO.read(@custom_log_file).should match(/INFO -- : works as well/)
|
169
|
+
body.should == "Level: info, Message: works as well"
|
170
|
+
|
171
|
+
erb_app '<%= dummy_helper(:warn, "works too") %>'
|
172
|
+
IO.read(@custom_log_file).should match(/WARN -- : works too/)
|
173
|
+
body.should == "Level: warn, Message: works too"
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should log higher levels" do
|
177
|
+
erb_app '<%= dummy_helper(:info, "custom-info") %>'
|
178
|
+
IO.read(@custom_log_file).should match(/INFO -- : custom-info/)
|
179
|
+
body.should == "Level: info, Message: custom-info"
|
180
|
+
|
181
|
+
erb_app '<%= dummy_helper(:debug, "custom-debug") %>'
|
182
|
+
IO.read(@custom_log_file).should match(/DEBUG -- : custom-debug/)
|
183
|
+
body.should == "Level: debug, Message: custom-debug"
|
184
|
+
end
|
185
|
+
|
186
|
+
end #/ #logger
|
187
|
+
|
188
|
+
end #/ Helpers
|
189
|
+
|
190
|
+
end #/ with Custom Settings
|
191
|
+
|
192
|
+
end #/ Logger
|
193
|
+
|
194
|
+
end #/ Sinatra
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
|
6
|
+
|
7
|
+
#--
|
8
|
+
# DEPENDENCIES
|
9
|
+
#++
|
10
|
+
%w(
|
11
|
+
sinatra/base
|
12
|
+
fileutils
|
13
|
+
).each {|lib| require lib }
|
14
|
+
|
15
|
+
#--
|
16
|
+
## SINATRA EXTENSIONS
|
17
|
+
#++
|
18
|
+
%w(
|
19
|
+
sinatra/tests
|
20
|
+
sinatra/logger
|
21
|
+
).each {|ext| require ext }
|
22
|
+
|
23
|
+
|
24
|
+
ENV['RACK_ENV'] = 'test'
|
25
|
+
|
26
|
+
Spec::Runner.configure do |config|
|
27
|
+
config.include RspecHpricotMatchers
|
28
|
+
config.include Sinatra::Tests::TestCase
|
29
|
+
config.include Sinatra::Tests::RSpec::SharedSpecs
|
30
|
+
end
|
31
|
+
|
32
|
+
def fixtures_path
|
33
|
+
"#{File.dirname(File.expand_path(__FILE__))}/fixtures"
|
34
|
+
end
|
35
|
+
|
36
|
+
class MyTestApp < Sinatra::Base
|
37
|
+
set :root, "#{fixtures_path}"
|
38
|
+
set :app_dir, "#{fixtures_path}/app"
|
39
|
+
set :public, "#{fixtures_path}/public"
|
40
|
+
set :views, "#{app_dir}/views"
|
41
|
+
enable :raise_errors
|
42
|
+
|
43
|
+
register(Sinatra::Tests)
|
44
|
+
|
45
|
+
end #/class MyTestApp
|
46
|
+
|
47
|
+
class MyCustomTestApp < Sinatra::Base
|
48
|
+
set :root, "#{fixtures_path}"
|
49
|
+
set :app_dir, "#{fixtures_path}/app"
|
50
|
+
set :public, "#{fixtures_path}/public"
|
51
|
+
set :views, "#{app_dir}/views"
|
52
|
+
enable :raise_errors
|
53
|
+
|
54
|
+
register(Sinatra::Tests)
|
55
|
+
|
56
|
+
end #/class MyCustomTestApp
|
57
|
+
|
58
|
+
|
59
|
+
class Test::Unit::TestCase
|
60
|
+
Sinatra::Base.set :environment, :test
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- kematzy
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-02-23 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: sinatra
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 10
|
30
|
+
- 1
|
31
|
+
version: 0.10.1
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rspec
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 1
|
43
|
+
- 3
|
44
|
+
- 0
|
45
|
+
version: 1.3.0
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: sinatra-tests
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
- 1
|
58
|
+
- 6
|
59
|
+
version: 0.1.6
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
description: A Sinatra extension that makes logging easy
|
63
|
+
email: kematzy@gmail.com
|
64
|
+
executables: []
|
65
|
+
|
66
|
+
extensions: []
|
67
|
+
|
68
|
+
extra_rdoc_files:
|
69
|
+
- LICENSE
|
70
|
+
- README.rdoc
|
71
|
+
files:
|
72
|
+
- .document
|
73
|
+
- .gitignore
|
74
|
+
- LICENSE
|
75
|
+
- README.rdoc
|
76
|
+
- Rakefile
|
77
|
+
- VERSION
|
78
|
+
- lib/sinatra/logger.rb
|
79
|
+
- sinatra-logger.gemspec
|
80
|
+
- spec/sinatra/logger_spec.rb
|
81
|
+
- spec/spec.opts
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
has_rdoc: true
|
84
|
+
homepage: http://github.com/kematzy/sinatra-logger
|
85
|
+
licenses: []
|
86
|
+
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options:
|
89
|
+
- --charset=UTF-8
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
version: "0"
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
segments:
|
104
|
+
- 0
|
105
|
+
version: "0"
|
106
|
+
requirements: []
|
107
|
+
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 1.3.6
|
110
|
+
signing_key:
|
111
|
+
specification_version: 3
|
112
|
+
summary: Easy logging with Sinatra
|
113
|
+
test_files:
|
114
|
+
- spec/sinatra/logger_spec.rb
|
115
|
+
- spec/spec_helper.rb
|