loggable 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +6 -0
- data/README +13 -11
- data/Rakefile +1 -1
- data/lib/loggable.rb +4 -42
- data/lib/loggable/log_methods.rb +30 -0
- data/lib/loggable/logger_stub.rb +9 -0
- data/lib/{version.rb → loggable/version.rb} +2 -2
- data/test/examples/{loggable_test.rb → log_methods_test.rb} +12 -3
- data/test/examples/logger_stub_test.rb +15 -0
- metadata +8 -4
data/HISTORY
CHANGED
data/README
CHANGED
@@ -11,23 +11,17 @@ This code is available as a Ruby Gem:
|
|
11
11
|
|
12
12
|
== Usage
|
13
13
|
|
14
|
-
Using the Gem is easy, just
|
15
|
-
to enable it:
|
14
|
+
Using the Gem is easy, just assign a new logger to your class:
|
16
15
|
|
17
16
|
require 'rubygems'
|
18
17
|
require 'logger'
|
19
18
|
require 'loggable'
|
20
|
-
|
21
|
-
class MyClass
|
22
|
-
loggable
|
23
|
-
end
|
24
19
|
|
25
|
-
|
26
|
-
the specified file:
|
27
|
-
|
28
|
-
MyClass.logger = Logger.new('debug.log')
|
20
|
+
class MyClass; end
|
29
21
|
|
30
|
-
|
22
|
+
MyClass.logger = Logger.new('debug.log')
|
23
|
+
|
24
|
+
Now, any class or instance methods have access to the logger:
|
31
25
|
|
32
26
|
class MyClass
|
33
27
|
def self.do_something
|
@@ -49,6 +43,14 @@ The above code will write into the log file when called:
|
|
49
43
|
D, [2008-04-24T20:37:32.273930 #25821] DEBUG -- : doing something in the class
|
50
44
|
D, [2008-04-24T20:37:32.274062 #25821] DEBUG -- : doing something in an instance
|
51
45
|
|
46
|
+
== Removing Logger Functionality
|
47
|
+
|
48
|
+
Calling the <tt>logger=</tt> class method to assign a logger to the class will
|
49
|
+
trigger the methods necessary to start logging messages. If you want to turn off
|
50
|
+
logging temporarily (without removing the logging statements from your class),
|
51
|
+
comment out the line where you assign the new logger. By default, the logger is
|
52
|
+
stubbed out, so any calls to methods on it will just return nil.
|
53
|
+
|
52
54
|
== Credits
|
53
55
|
|
54
56
|
Copyright (c) 2008 Patrick Reagan of Viget Labs (mailto:patrick.reagan@viget.com)
|
data/Rakefile
CHANGED
data/lib/loggable.rb
CHANGED
@@ -1,43 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module LogMethods
|
4
|
-
|
5
|
-
# Including this in your class definition will add the ability to use a logger within
|
6
|
-
# your class:
|
7
|
-
#
|
8
|
-
# class MyClass
|
9
|
-
# loggable
|
10
|
-
# end
|
11
|
-
#
|
12
|
-
# Now you can assign a logger instance to this class and have it write to the specified
|
13
|
-
# logfile. See the README for details.
|
14
|
-
#
|
15
|
-
def loggable
|
16
|
-
extend ClassMethods
|
17
|
-
include InstanceMethods
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
module ClassMethods
|
22
|
-
|
23
|
-
@@logger = nil
|
24
|
-
|
25
|
-
def logger=(logger)
|
26
|
-
@@logger = logger
|
27
|
-
end
|
28
|
-
|
29
|
-
def logger
|
30
|
-
@@logger
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
module InstanceMethods
|
36
|
-
def logger
|
37
|
-
self.class.logger
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
1
|
+
require 'loggable/log_methods'
|
2
|
+
require 'loggable/logger_stub'
|
42
3
|
|
43
|
-
Object.send(:extend, Loggable::
|
4
|
+
Object.send(:extend, Loggable::ClassMethods)
|
5
|
+
Object.send(:include, Loggable::InstanceMethods)
|
@@ -0,0 +1,30 @@
|
|
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
|
+
def logger
|
19
|
+
@@logger || LoggerStub.new
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
module InstanceMethods
|
25
|
+
def logger
|
26
|
+
self.class.logger
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -1,6 +1,10 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../test_helper'
|
2
2
|
|
3
|
-
class MyClass
|
3
|
+
class MyClass
|
4
|
+
def logged_method
|
5
|
+
logger.debug 'message'
|
6
|
+
end
|
7
|
+
end
|
4
8
|
|
5
9
|
describe "MyClass, with loggable mix-in" do
|
6
10
|
|
@@ -8,8 +12,13 @@ describe "MyClass, with loggable mix-in" do
|
|
8
12
|
@logger = mock()
|
9
13
|
end
|
10
14
|
|
11
|
-
it "should have
|
12
|
-
MyClass.logger.should.be
|
15
|
+
it "should have a logger stub by default" do
|
16
|
+
MyClass.logger.should.be.an.instance_of(LoggerStub)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should not fail when an instance calls an uninitialized logger" do
|
20
|
+
m = MyClass.new
|
21
|
+
lambda { m.logged_method }.should.not.raise
|
13
22
|
end
|
14
23
|
|
15
24
|
it "should allow the asssignment of a logger" do
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
describe "LoggerStub" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@logger = LoggerStub.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return nil for all standard logging methods" do
|
10
|
+
[:debug, :error, :fatal, :info, :warn].each do |method|
|
11
|
+
@logger.send(method).should.be.nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loggable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Reagan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-05-09 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -27,10 +27,14 @@ files:
|
|
27
27
|
- README
|
28
28
|
- HISTORY
|
29
29
|
- Rakefile
|
30
|
+
- lib/loggable
|
31
|
+
- lib/loggable/log_methods.rb
|
32
|
+
- lib/loggable/logger_stub.rb
|
33
|
+
- lib/loggable/version.rb
|
30
34
|
- lib/loggable.rb
|
31
|
-
- lib/version.rb
|
32
35
|
- test/examples
|
33
|
-
- test/examples/
|
36
|
+
- test/examples/log_methods_test.rb
|
37
|
+
- test/examples/logger_stub_test.rb
|
34
38
|
- test/test_helper.rb
|
35
39
|
has_rdoc: true
|
36
40
|
homepage: http://viget.rubyforge.org/loggable
|