log_switch 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +10 -0
- data/README.rdoc +34 -1
- data/Rakefile +3 -1
- data/lib/log_switch.rb +21 -5
- data/lib/log_switch/mixin.rb +20 -0
- data/lib/log_switch/version.rb +1 -1
- data/spec/log_switch/mixin_spec.rb +49 -0
- data/spec/log_switch_spec.rb +3 -1
- metadata +15 -12
data/History.rdoc
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
=== 0.3.0 / 2012-02-03
|
2
|
+
|
3
|
+
* Improvements:
|
4
|
+
* gh-2[https://github.com/turboladen/log_switch/issues/2]: Added ability to
|
5
|
+
mix in {LogSwitch::Mixin} to classes to allow calling +#log+ in your class
|
6
|
+
and have it delegate to your singleton logger.
|
7
|
+
* Bug fixes:
|
8
|
+
* gh-3[https://github.com/turboladen/log_switch/issues/3]: Fixed warning on
|
9
|
+
+@before_block+.
|
10
|
+
|
1
11
|
=== 0.2.0 / 2011-12-05
|
2
12
|
|
3
13
|
* Added ability to pass a block to +.log+ to have code executed before logging.
|
data/README.rdoc
CHANGED
@@ -44,7 +44,7 @@ Get your app logging with a single point of logging:
|
|
44
44
|
MyThing.log = false
|
45
45
|
MyThing.log "You're my favorite." # => No logging occurs!
|
46
46
|
|
47
|
-
By default, LogSwitch sets the log level to
|
47
|
+
By default, LogSwitch sets the log level to +:debug+. You can change the default
|
48
48
|
log level as you go:
|
49
49
|
|
50
50
|
MyThing.log_level = :warn
|
@@ -99,6 +99,39 @@ Here's the above example, using the one-timer:
|
|
99
99
|
# No block gets called here (assuming the +#before+ hook from above hasn't been defined)
|
100
100
|
MyThing.log "I'm hungry..."
|
101
101
|
|
102
|
+
=== Mixin for lazy people like me
|
103
|
+
|
104
|
+
All that stuff above describes how you can extend some class of yours to make it
|
105
|
+
a singleton logger (well, not that you can't have it do other stuff too...). If
|
106
|
+
you're using this in a sizeable project, however, it can get tiresome typing out
|
107
|
+
<tt>MyThing.log "message"</tt> over and over again; for me, it's usually more
|
108
|
+
like <tt>MyThing::Logger.log "message"</tt>. The {LogSwitch::Mixin} mixin
|
109
|
+
lets you shorten things up.
|
110
|
+
|
111
|
+
If you've extended {LogSwitch} into a class...
|
112
|
+
|
113
|
+
class MyThing
|
114
|
+
class Logger
|
115
|
+
extend LogSwitch
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
...then you can mix in {LogSwitch::Mixin} to your other classes that you want to
|
120
|
+
use +MyThing::Logger.log+:
|
121
|
+
|
122
|
+
class MyThing
|
123
|
+
class WidgetMaker
|
124
|
+
include LogSwitch::Mixin
|
125
|
+
|
126
|
+
def make_widget
|
127
|
+
log "Making widget...", :info # This will delegate to MyThing::Logger.log
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
This lets you still use a single logger throughout your classes, but shortens
|
133
|
+
the amount of typing you have to do.
|
134
|
+
|
102
135
|
== REQUIREMENTS
|
103
136
|
|
104
137
|
* Rubies (tested):
|
data/Rakefile
CHANGED
data/lib/log_switch.rb
CHANGED
@@ -1,11 +1,27 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/log_switch/version')
|
2
1
|
require "logger"
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/log_switch/version')
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/log_switch/mixin')
|
3
4
|
|
4
5
|
# LogSwitch allows for extending a class/module with a logger and, most
|
5
6
|
# importantly, allows for turning off logging programmatically. See the
|
6
7
|
# +README.rdoc+ for more info.
|
7
8
|
module LogSwitch
|
8
9
|
|
10
|
+
# Saves the name of the class that extended itself with this module. Used
|
11
|
+
# by {LogSwitch::Mixin} to know which class to include itself to.
|
12
|
+
def self.extend_object(base)
|
13
|
+
@extender = base
|
14
|
+
super(base)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Simply returns the name of the class that extended itself with this module.
|
18
|
+
# It's set by {.extend_object}.
|
19
|
+
#
|
20
|
+
# @return [Class] The class that extended itself with LogSwitch.
|
21
|
+
def self.extender
|
22
|
+
@extender
|
23
|
+
end
|
24
|
+
|
9
25
|
# Use to turn logging on or off.
|
10
26
|
attr_writer :log
|
11
27
|
|
@@ -30,15 +46,15 @@ module LogSwitch
|
|
30
46
|
@log_level ||= :debug
|
31
47
|
end
|
32
48
|
|
33
|
-
#
|
49
|
+
# {#log} calls the block given to this method before it logs every time.
|
34
50
|
# This, thus, acts as a hook in the case where you want to make sure some
|
35
51
|
# code gets executed before you log a message. Useful for making sure a file
|
36
52
|
# exists before logging to it.
|
37
53
|
#
|
38
54
|
# @param [Proc] block The block of code to execute before logging a message
|
39
|
-
# with
|
55
|
+
# with {#log}.
|
40
56
|
def before(&block)
|
41
|
-
@before_block
|
57
|
+
@before_block ||= block
|
42
58
|
end
|
43
59
|
|
44
60
|
# Logs a message using the level provided. If no level provided, use
|
@@ -47,7 +63,7 @@ module LogSwitch
|
|
47
63
|
# @param [String] message The message to log.
|
48
64
|
# @param [Symbol] level The log level to send to your Logger.
|
49
65
|
def log(message, level=log_level)
|
50
|
-
|
66
|
+
before.call unless before.nil?
|
51
67
|
yield if block_given?
|
52
68
|
|
53
69
|
if log?
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module LogSwitch
|
2
|
+
module Mixin
|
3
|
+
|
4
|
+
# When this module is included, this method gets called and defines a +#log+
|
5
|
+
# method on the including class.
|
6
|
+
#
|
7
|
+
# @param [Class] klass The class that's including this module.
|
8
|
+
# @raise [RuntimeError] If {LogSwitch.extender} isn't set (which gets set
|
9
|
+
# when you +extend+ your class with {LogSwitch}).
|
10
|
+
def self.included(klass)
|
11
|
+
if LogSwitch.extender
|
12
|
+
klass.send :define_method, :log do |*args|
|
13
|
+
LogSwitch.extender.log *args
|
14
|
+
end
|
15
|
+
else
|
16
|
+
raise "No class has been extended by LogSwitch yet."
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/log_switch/version.rb
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
require 'log_switch/mixin'
|
3
|
+
|
4
|
+
|
5
|
+
class Tester
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
describe LogSwitch::Mixin do
|
10
|
+
describe "::included" do
|
11
|
+
context "LogSwitch.extender is set" do
|
12
|
+
let(:logger_class) do
|
13
|
+
double "LogSwitch.logger"
|
14
|
+
end
|
15
|
+
|
16
|
+
before { LogSwitch.stub(:extender).and_return logger_class }
|
17
|
+
|
18
|
+
it "takes a class and defines the #log method on it" do
|
19
|
+
Tester.should_receive(:send).with(:define_method, :log)
|
20
|
+
|
21
|
+
class Tester
|
22
|
+
include LogSwitch::Mixin
|
23
|
+
end
|
24
|
+
|
25
|
+
LogSwitch.unstub(:extender)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "results in an object with a #log message" do
|
29
|
+
class Tester
|
30
|
+
include LogSwitch::Mixin
|
31
|
+
end
|
32
|
+
|
33
|
+
Tester.new.methods.should include(:log)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "LogSwitch.extender is not set" do
|
38
|
+
before { LogSwitch.stub(:extender).and_return nil }
|
39
|
+
|
40
|
+
it "raises a RuntimeError" do
|
41
|
+
expect {
|
42
|
+
class Tester
|
43
|
+
include LogSwitch::Mixin
|
44
|
+
end
|
45
|
+
}.to raise_error RuntimeError
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/log_switch_spec.rb
CHANGED
@@ -23,7 +23,7 @@ describe "LogSwitch" do
|
|
23
23
|
MyClass.reset_config!
|
24
24
|
end
|
25
25
|
|
26
|
-
it { LogSwitch::VERSION.should == '0.
|
26
|
+
it { LogSwitch::VERSION.should == '0.3.0' }
|
27
27
|
|
28
28
|
describe ".log" do
|
29
29
|
it "should default to true" do
|
@@ -47,6 +47,8 @@ describe "LogSwitch" do
|
|
47
47
|
object.should_receive(:test_in_block)
|
48
48
|
|
49
49
|
MyClass.log('hi') { object.test_in_block }
|
50
|
+
|
51
|
+
object.unstub :test_in_block
|
50
52
|
end
|
51
53
|
|
52
54
|
context "with .before" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: log_switch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &70277189408400 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70277189408400
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70277189407920 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70277189407920
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70277189407440 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 2.6.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70277189407440
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: simplecov
|
49
|
-
requirement: &
|
49
|
+
requirement: &70277189406900 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70277189406900
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: yard
|
60
|
-
requirement: &
|
60
|
+
requirement: &70277189406420 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: 0.7.2
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70277189406420
|
69
69
|
description: Extends a class for singleton style logging that can easily be turned
|
70
70
|
on and off.
|
71
71
|
email:
|
@@ -74,8 +74,10 @@ executables: []
|
|
74
74
|
extensions: []
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
|
+
- lib/log_switch/mixin.rb
|
77
78
|
- lib/log_switch/version.rb
|
78
79
|
- lib/log_switch.rb
|
80
|
+
- spec/log_switch/mixin_spec.rb
|
79
81
|
- spec/log_switch_spec.rb
|
80
82
|
- spec/spec_helper.rb
|
81
83
|
- History.rdoc
|
@@ -110,6 +112,7 @@ specification_version: 3
|
|
110
112
|
summary: Extends a class for singleton style logging that can easily be turned on
|
111
113
|
and off.
|
112
114
|
test_files:
|
115
|
+
- spec/log_switch/mixin_spec.rb
|
113
116
|
- spec/log_switch_spec.rb
|
114
117
|
- spec/spec_helper.rb
|
115
118
|
has_rdoc:
|