mediashelf-loggable 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +26 -0
- data/README.rdoc +81 -0
- data/Rakefile +40 -0
- data/VERSION +1 -0
- data/lib/loggable.rb +17 -0
- data/lib/loggable/log_methods.rb +33 -0
- data/lib/loggable/logger_stub.rb +9 -0
- data/lib/loggable/version.rb +13 -0
- data/loggable.gemspec +30 -0
- data/test/examples/log_methods_test.rb +54 -0
- data/test/examples/logger_stub_test.rb +15 -0
- data/test/test_helper.rb +7 -0
- metadata +96 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
gemcutter (0.6.1)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.4.0)
|
7
|
+
gemcutter (>= 0.1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rubyforge (>= 2.0.0)
|
10
|
+
jnunemaker-matchy (0.4.0)
|
11
|
+
json_pure (1.4.6)
|
12
|
+
mocha (0.9.9)
|
13
|
+
rake
|
14
|
+
rake (0.8.7)
|
15
|
+
rubyforge (2.0.4)
|
16
|
+
json_pure (>= 1.1.7)
|
17
|
+
thoughtbot-shoulda (2.11.1)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
jeweler
|
24
|
+
jnunemaker-matchy
|
25
|
+
mocha
|
26
|
+
thoughtbot-shoulda
|
data/README.rdoc
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
= Loggable
|
2
|
+
|
3
|
+
Loggable is a gem that's designed to add a logging interface to classes that don't
|
4
|
+
automatically support it.
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
Stable:
|
9
|
+
|
10
|
+
sudo gem install loggable
|
11
|
+
|
12
|
+
Bleeding Edge (hardly):
|
13
|
+
|
14
|
+
sudo gem install vigetlabs-loggable --source=http://gems.github.com
|
15
|
+
|
16
|
+
== Usage
|
17
|
+
|
18
|
+
Using the Gem is easy, just assign a new logger to your class:
|
19
|
+
|
20
|
+
require 'rubygems'
|
21
|
+
require 'logger'
|
22
|
+
require 'loggable'
|
23
|
+
|
24
|
+
class MyClass; end
|
25
|
+
|
26
|
+
MyClass.logger = Logger.new('debug.log')
|
27
|
+
|
28
|
+
Now, any class or instance methods have access to the logger:
|
29
|
+
|
30
|
+
class MyClass
|
31
|
+
def self.do_something
|
32
|
+
logger.debug 'doing something in the class'
|
33
|
+
end
|
34
|
+
|
35
|
+
def do_something
|
36
|
+
logger.debug 'doing something in an instance'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
The above code will write into the log file when called:
|
41
|
+
|
42
|
+
MyClass.do_something
|
43
|
+
MyClass.new.do_something
|
44
|
+
|
45
|
+
debug.log:
|
46
|
+
|
47
|
+
D, [2008-04-24T20:37:32.273930 #25821] DEBUG -- : doing something in the class
|
48
|
+
D, [2008-04-24T20:37:32.274062 #25821] DEBUG -- : doing something in an instance
|
49
|
+
|
50
|
+
== Removing Logger Functionality
|
51
|
+
|
52
|
+
Calling the <tt>logger=</tt> class method to assign a logger to the class will
|
53
|
+
trigger the methods necessary to start logging messages. If you want to turn off
|
54
|
+
logging temporarily (without removing the logging statements from your class),
|
55
|
+
comment out the line where you assign the new logger. By default, the logger is
|
56
|
+
stubbed out, so any calls to methods on it will just return nil.
|
57
|
+
|
58
|
+
== Credits
|
59
|
+
|
60
|
+
Copyright (c) 2008 Patrick Reagan of Viget Labs (mailto:patrick.reagan@viget.com)
|
61
|
+
|
62
|
+
Permission is hereby granted, free of charge, to any person
|
63
|
+
obtaining a copy of this software and associated documentation
|
64
|
+
files (the "Software"), to deal in the Software without
|
65
|
+
restriction, including without limitation the rights to use,
|
66
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
67
|
+
copies of the Software, and to permit persons to whom the
|
68
|
+
Software is furnished to do so, subject to the following
|
69
|
+
conditions:
|
70
|
+
|
71
|
+
The above copyright notice and this permission notice shall be
|
72
|
+
included in all copies or substantial portions of the Software.
|
73
|
+
|
74
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
75
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
76
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
77
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
78
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
79
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
80
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
81
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "mediashelf-loggable"
|
9
|
+
gem.summary = %Q{A gem that provides logging capabilities to any class}
|
10
|
+
gem.description = %Q{A gem that provides logging capabilities to any class. Relies on Rails logger if it's available. Extended from loggable gem by viget}
|
11
|
+
gem.email = "matt.zumwalt@yourmediashelf.com"
|
12
|
+
gem.homepage = "http://github.com/mediashelf/mediashelf-loggable"
|
13
|
+
gem.authors = ['Patrick Reagan',"Matt Zumwalt"]
|
14
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/*_test.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/*_test.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.4.0
|
data/lib/loggable.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
require 'loggable/log_methods'
|
5
|
+
require 'loggable/logger_stub'
|
6
|
+
|
7
|
+
class Object
|
8
|
+
include Loggable::InstanceMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
class Class
|
12
|
+
include Loggable::ClassMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
class Module
|
16
|
+
include Loggable::ClassMethods
|
17
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Loggable
|
2
|
+
|
3
|
+
module ClassMethods
|
4
|
+
|
5
|
+
@@logger = nil
|
6
|
+
|
7
|
+
# Use this method on any of your classes to trigger the logging facility:
|
8
|
+
#
|
9
|
+
# MyClass.logger = Logger.new('/path/to/logfile')
|
10
|
+
#
|
11
|
+
# Now you can call the 'logger' method inside a class or instance method to log at
|
12
|
+
# the specified level. See the README for details.
|
13
|
+
#
|
14
|
+
def logger=(logger)
|
15
|
+
@@logger = logger
|
16
|
+
end
|
17
|
+
|
18
|
+
# If RAILS_DEFAULT_LOGGER is defined, that will be returned.
|
19
|
+
# If no logger has been defined, a new STDOUT Logger will be created.
|
20
|
+
def logger
|
21
|
+
# @@logger || LoggerStub.new
|
22
|
+
@@logger ||= defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : ::Logger.new(STDOUT)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
module InstanceMethods
|
28
|
+
def logger
|
29
|
+
self.class.logger
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/loggable.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{loggable}
|
5
|
+
s.version = "0.3.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Patrick Reagan"]
|
9
|
+
s.date = %q{2009-04-28}
|
10
|
+
s.email = %q{patrick.reagan@viget.com}
|
11
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
12
|
+
s.files = ["README.rdoc", "Rakefile", "lib/loggable", "lib/loggable/log_methods.rb", "lib/loggable/logger_stub.rb", "lib/loggable/version.rb", "lib/loggable.rb", "test/examples", "test/examples/log_methods_test.rb", "test/examples/logger_stub_test.rb", "test/test_helper.rb"]
|
13
|
+
s.has_rdoc = true
|
14
|
+
s.homepage = %q{http://viget.com/extend}
|
15
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{viget}
|
18
|
+
s.rubygems_version = %q{1.3.2}
|
19
|
+
s.summary = %q{A gem that provides logging capabilities to any class}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class MyClass
|
4
|
+
def logged_method
|
5
|
+
logger.debug 'message'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module MyModule; end
|
10
|
+
|
11
|
+
class LogMethodsTest < Test::Unit::TestCase
|
12
|
+
context "An instance of MyClass, with loggable mix-in" do
|
13
|
+
|
14
|
+
setup { @logger = mock() }
|
15
|
+
teardown { MyClass.logger = nil}
|
16
|
+
|
17
|
+
should "have a logger stub by default" do
|
18
|
+
MyClass.logger.should be_instance_of(Logger)
|
19
|
+
end
|
20
|
+
|
21
|
+
should "not fail when an instance calls an uninitialized logger" do
|
22
|
+
m = MyClass.new
|
23
|
+
lambda { m.logged_method }.should_not raise_error
|
24
|
+
end
|
25
|
+
|
26
|
+
should "allow the asssignment of a logger" do
|
27
|
+
MyClass.logger = @logger
|
28
|
+
MyClass.logger.should == @logger
|
29
|
+
end
|
30
|
+
|
31
|
+
should "allow access to the logger from an instance" do
|
32
|
+
MyClass.logger = @logger
|
33
|
+
MyClass.new.logger.should == @logger
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
context "MyModule, with loggable mix-in" do
|
39
|
+
|
40
|
+
should "have a logger stub by default" do
|
41
|
+
MyModule.logger = nil
|
42
|
+
MyModule.logger.should be_kind_of(Logger)
|
43
|
+
end
|
44
|
+
|
45
|
+
should "be able to log messages" do
|
46
|
+
logger = mock()
|
47
|
+
logger.expects(:debug).with('blip')
|
48
|
+
|
49
|
+
MyModule.logger = logger
|
50
|
+
MyModule.logger.debug('blip')
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class LoggerStubTest < Test::Unit::TestCase
|
4
|
+
context "An instance of the LoggerStub class" do
|
5
|
+
|
6
|
+
setup { @logger = LoggerStub.new }
|
7
|
+
|
8
|
+
should "return nil for all standard logging methods" do
|
9
|
+
[:debug, :error, :fatal, :info, :warn].each do |method|
|
10
|
+
@logger.send(method, 'message').should be(nil)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mediashelf-loggable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Patrick Reagan
|
14
|
+
- Matt Zumwalt
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-10-26 00:00:00 -05:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: thoughtbot-shoulda
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
description: A gem that provides logging capabilities to any class. Relies on Rails logger if it's available. Extended from loggable gem by viget
|
37
|
+
email: matt.zumwalt@yourmediashelf.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- Gemfile.lock
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/loggable.rb
|
52
|
+
- lib/loggable/log_methods.rb
|
53
|
+
- lib/loggable/logger_stub.rb
|
54
|
+
- lib/loggable/version.rb
|
55
|
+
- loggable.gemspec
|
56
|
+
- test/examples/log_methods_test.rb
|
57
|
+
- test/examples/logger_stub_test.rb
|
58
|
+
- test/test_helper.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/mediashelf/mediashelf-loggable
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --charset=UTF-8
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.3.7
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: A gem that provides logging capabilities to any class
|
93
|
+
test_files:
|
94
|
+
- test/examples/log_methods_test.rb
|
95
|
+
- test/examples/logger_stub_test.rb
|
96
|
+
- test/test_helper.rb
|