log_switch 0.1.4 → 0.2.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/History.rdoc +9 -0
- data/README.rdoc +40 -2
- data/lib/log_switch/version.rb +1 -1
- data/lib/log_switch.rb +21 -1
- data/log_switch.gemspec +2 -3
- data/spec/log_switch_spec.rb +44 -7
- data/spec/spec_helper.rb +0 -2
- metadata +65 -87
data/History.rdoc
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
=== 0.2.0 / 2011-12-05
|
2
|
+
|
3
|
+
* Added ability to pass a block to +.log+ to have code executed before logging.
|
4
|
+
* Added +before+ hook to allow code to be executed every time log gets called
|
5
|
+
(but before the message actually gets logged).
|
6
|
+
* Bug fixes
|
7
|
+
* gh-1[https://github.com/turboladen/log_switch/issues/1]: Only log #each_line
|
8
|
+
when that method is supported on the object being logged.
|
9
|
+
|
1
10
|
=== 0.1.4 / 2011-10-10
|
2
11
|
|
3
12
|
* Removed gemspec enforcement of >= 1.9.2
|
data/README.rdoc
CHANGED
@@ -27,6 +27,8 @@ Features:
|
|
27
27
|
|
28
28
|
== SYNOPSIS
|
29
29
|
|
30
|
+
=== Basic Use
|
31
|
+
|
30
32
|
Get your app logging with a single point of logging:
|
31
33
|
|
32
34
|
require 'log_switch'
|
@@ -61,14 +63,50 @@ If you have another Logger object you want to write to, no problem:
|
|
61
63
|
File.open('log.txt', 'r').read # => Logfile created on 2011-10-07 15:50:19 -0700 by logger.rb/25413
|
62
64
|
# D, [2011-10-07T15:51:16.385798 #34026] DEBUG -- : hi!
|
63
65
|
|
66
|
+
=== Hooks
|
67
|
+
|
68
|
+
You can also make sure code gets executed before each call to +.log+. While
|
69
|
+
this really only makes sense when you're logging from multiple places and/or
|
70
|
+
you're not sure when the first call to your logger will occur (unlike this
|
71
|
+
example), say you want to makes sure your log directory exists before trying
|
72
|
+
to write the log file to it:
|
73
|
+
|
74
|
+
log_directory = File.expand_path('my_log_dir')
|
75
|
+
|
76
|
+
MyThing.before do
|
77
|
+
FileUtils.mkdir_p(full_directory) unless Dir.exists? log_directory
|
78
|
+
end
|
79
|
+
|
80
|
+
log_file = log_directory + "/sweet_log_bro.txt"
|
81
|
+
MyThing.logger = Logger.new(log_file)
|
82
|
+
MyThing.log "Thanks brah!" # This calls the #before hook, thus
|
83
|
+
# creating the directory.
|
84
|
+
MyThing.log "I'm hungry..." # This also calls the #before hook...
|
85
|
+
|
86
|
+
You might just want want a one-time before hook--in that case, +.log+ will call
|
87
|
+
a plain old block that you pass to it, right before writing to the log file.
|
88
|
+
Here's the above example, using the one-timer:
|
89
|
+
|
90
|
+
log_directory = File.expand_path('my_log_dir')
|
91
|
+
|
92
|
+
log_file = log_directory + "/sweet_log_bro.txt"
|
93
|
+
MyThing.logger = Logger.new(log_file)
|
94
|
+
|
95
|
+
MyThing.log("Thanks brah!") do
|
96
|
+
FileUtils.mkdir_p(full_directory) unless Dir.exists? log_directory
|
97
|
+
end
|
98
|
+
|
99
|
+
# No block gets called here (assuming the +#before+ hook from above hasn't been defined)
|
100
|
+
MyThing.log "I'm hungry..."
|
101
|
+
|
64
102
|
== REQUIREMENTS
|
65
103
|
|
66
104
|
* Rubies (tested):
|
67
|
-
* MRI 1.9.3
|
105
|
+
* MRI 1.9.3
|
68
106
|
* MRI 1.9.2
|
69
107
|
* MRI 1.8.7
|
70
108
|
* ree 1.8.7-2011.03
|
71
|
-
* JRuby 1.6.
|
109
|
+
* JRuby 1.6.5
|
72
110
|
* Rubinius 1.2.4
|
73
111
|
* RubyGems:
|
74
112
|
* None!
|
data/lib/log_switch/version.rb
CHANGED
data/lib/log_switch.rb
CHANGED
@@ -30,13 +30,33 @@ module LogSwitch
|
|
30
30
|
@log_level ||= :debug
|
31
31
|
end
|
32
32
|
|
33
|
+
# +#log+ calls the block given to this method before it logs every time.
|
34
|
+
# This, thus, acts as a hook in the case where you want to make sure some
|
35
|
+
# code gets executed before you log a message. Useful for making sure a file
|
36
|
+
# exists before logging to it.
|
37
|
+
#
|
38
|
+
# @param [Proc] block The block of code to execute before logging a message
|
39
|
+
# with +#log+.
|
40
|
+
def before(&block)
|
41
|
+
@before_block = block
|
42
|
+
end
|
43
|
+
|
33
44
|
# Logs a message using the level provided. If no level provided, use
|
34
45
|
# +@log_level+.
|
35
46
|
#
|
36
47
|
# @param [String] message The message to log.
|
37
48
|
# @param [Symbol] level The log level to send to your Logger.
|
38
49
|
def log(message, level=log_level)
|
39
|
-
|
50
|
+
@before_block.call unless @before_block.nil?
|
51
|
+
yield if block_given?
|
52
|
+
|
53
|
+
if log?
|
54
|
+
if message.respond_to? :each_line
|
55
|
+
message.each_line { |line| logger.send level, line.chomp }
|
56
|
+
else
|
57
|
+
logger.send(level, message)
|
58
|
+
end
|
59
|
+
end
|
40
60
|
end
|
41
61
|
|
42
62
|
# Sets back to defaults.
|
data/log_switch.gemspec
CHANGED
@@ -11,9 +11,9 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.description = %q(Extends a class for singleton style logging that can easily be turned on and off.)
|
12
12
|
|
13
13
|
s.required_rubygems_version = ">=1.8.0"
|
14
|
-
s.files = Dir.glob("{
|
14
|
+
s.files = Dir.glob("{lib,spec}/**/*") + Dir.glob("*.rdoc") +
|
15
15
|
%w(.gemtest Gemfile log_switch.gemspec Rakefile)
|
16
|
-
s.test_files = Dir.glob("{
|
16
|
+
s.test_files = Dir.glob("{spec}/**/*")
|
17
17
|
s.require_paths = ["lib"]
|
18
18
|
|
19
19
|
s.add_development_dependency("bundler", [">= 0"])
|
@@ -21,7 +21,6 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.add_development_dependency("rspec", ["~> 2.6.0"])
|
22
22
|
if RUBY_VERSION > '1.9'
|
23
23
|
s.add_development_dependency("simplecov", [">= 0"])
|
24
|
-
s.add_development_dependency("simplecov-rcov-text", [">= 0"])
|
25
24
|
end
|
26
25
|
s.add_development_dependency("yard", [">= 0.7.2"])
|
27
26
|
end
|
data/spec/log_switch_spec.rb
CHANGED
@@ -23,9 +23,9 @@ 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.2.0' }
|
27
27
|
|
28
|
-
describe "log" do
|
28
|
+
describe ".log" do
|
29
29
|
it "should default to true" do
|
30
30
|
MyClass.log?.should be_true
|
31
31
|
end
|
@@ -40,22 +40,52 @@ describe "LogSwitch" do
|
|
40
40
|
it "raises when log_level isn't a Symbol" do
|
41
41
|
expect { MyClass.log("stuff", "meow") }.to raise_error NoMethodError
|
42
42
|
end
|
43
|
+
|
44
|
+
it "can take a block" do
|
45
|
+
object = Object.new
|
46
|
+
object.stub :test_in_block
|
47
|
+
object.should_receive(:test_in_block)
|
48
|
+
|
49
|
+
MyClass.log('hi') { object.test_in_block }
|
50
|
+
end
|
51
|
+
|
52
|
+
context "with .before" do
|
53
|
+
it "calls the @before_block if that's set" do
|
54
|
+
MyClass.before { puts "This is also before" }
|
55
|
+
MyClass.instance_variable_get(:@before_block).should_receive(:call).once
|
56
|
+
MyClass.log 'hi'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "can log non-String objects" do
|
61
|
+
it "an Array of various object types" do
|
62
|
+
array = [1, 'stuff', { :two => 2 }]
|
63
|
+
MyClass.logger.should_receive(:send).with(:debug, array)
|
64
|
+
expect { MyClass.log array }.to_not raise_exception
|
65
|
+
end
|
66
|
+
|
67
|
+
it "an Exception" do
|
68
|
+
ex = StandardError.new("Test error.")
|
69
|
+
MyClass.logger.should_receive(:send).with(:debug, ex)
|
70
|
+
expect { MyClass.log ex }.to_not raise_exception
|
71
|
+
end
|
72
|
+
end
|
43
73
|
end
|
44
74
|
|
45
|
-
describe "log=" do
|
75
|
+
describe ".log=" do
|
46
76
|
it "allows to set logging to false" do
|
47
77
|
MyClass.log = false
|
48
78
|
MyClass.log?.should be_false
|
49
79
|
end
|
50
80
|
end
|
51
81
|
|
52
|
-
describe "logger" do
|
82
|
+
describe ".logger" do
|
53
83
|
it "is a Logger by default" do
|
54
84
|
MyClass.logger.should be_a Logger
|
55
85
|
end
|
56
86
|
end
|
57
87
|
|
58
|
-
describe "logger=" do
|
88
|
+
describe ".logger=" do
|
59
89
|
it "allows to set to use another logger" do
|
60
90
|
original_logger = MyClass.logger
|
61
91
|
another_logger = Logger.new nil
|
@@ -64,13 +94,13 @@ describe "LogSwitch" do
|
|
64
94
|
end
|
65
95
|
end
|
66
96
|
|
67
|
-
describe "log_level" do
|
97
|
+
describe ".log_level" do
|
68
98
|
it "defaults to :debug" do
|
69
99
|
MyClass.log_level.should == :debug
|
70
100
|
end
|
71
101
|
end
|
72
102
|
|
73
|
-
describe "log_level=" do
|
103
|
+
describe ".log_level=" do
|
74
104
|
it "changes the level that #log(msg, level=) uses" do
|
75
105
|
MyClass.logger.should_receive(:debug)
|
76
106
|
MyClass.log("testing...")
|
@@ -79,4 +109,11 @@ describe "LogSwitch" do
|
|
79
109
|
MyClass.log("testing...")
|
80
110
|
end
|
81
111
|
end
|
112
|
+
|
113
|
+
describe ".before" do
|
114
|
+
it "assigns the given block to @before_block" do
|
115
|
+
MyClass.before { "I'm a block" }
|
116
|
+
MyClass.instance_variable_get(:@before_block).should be_a Proc
|
117
|
+
end
|
118
|
+
end
|
82
119
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
1
|
if RUBY_VERSION > '1.9'
|
2
2
|
require 'simplecov'
|
3
|
-
require 'simplecov-rcov-text'
|
4
3
|
|
5
4
|
class SimpleCov::Formatter::MergedFormatter
|
6
5
|
def format(result)
|
7
6
|
SimpleCov::Formatter::HTMLFormatter.new.format(result)
|
8
|
-
SimpleCov::Formatter::RcovTextFormatter.new.format(result)
|
9
7
|
end
|
10
8
|
end
|
11
9
|
|
metadata
CHANGED
@@ -1,92 +1,79 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: log_switch
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 4
|
10
|
-
version: 0.1.4
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Steve Loveless
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-12-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: bundler
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &70123558624780 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
32
22
|
type: :development
|
33
|
-
version_requirements: *id001
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: rake
|
36
23
|
prerelease: false
|
37
|
-
|
24
|
+
version_requirements: *70123558624780
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70123558624300 !ruby/object:Gem::Requirement
|
38
28
|
none: false
|
39
|
-
requirements:
|
40
|
-
- -
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
|
43
|
-
segments:
|
44
|
-
- 0
|
45
|
-
version: "0"
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
46
33
|
type: :development
|
47
|
-
version_requirements: *id002
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: rspec
|
50
34
|
prerelease: false
|
51
|
-
|
35
|
+
version_requirements: *70123558624300
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &70123558623820 !ruby/object:Gem::Requirement
|
52
39
|
none: false
|
53
|
-
requirements:
|
40
|
+
requirements:
|
54
41
|
- - ~>
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
hash: 23
|
57
|
-
segments:
|
58
|
-
- 2
|
59
|
-
- 6
|
60
|
-
- 0
|
42
|
+
- !ruby/object:Gem::Version
|
61
43
|
version: 2.6.0
|
62
44
|
type: :development
|
63
|
-
version_requirements: *id003
|
64
|
-
- !ruby/object:Gem::Dependency
|
65
|
-
name: yard
|
66
45
|
prerelease: false
|
67
|
-
|
46
|
+
version_requirements: *70123558623820
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: simplecov
|
49
|
+
requirement: &70123558623320 !ruby/object:Gem::Requirement
|
68
50
|
none: false
|
69
|
-
requirements:
|
70
|
-
- -
|
71
|
-
- !ruby/object:Gem::Version
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70123558623320
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: yard
|
60
|
+
requirement: &70123558622840 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
77
65
|
version: 0.7.2
|
78
66
|
type: :development
|
79
|
-
|
80
|
-
|
81
|
-
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70123558622840
|
69
|
+
description: Extends a class for singleton style logging that can easily be turned
|
70
|
+
on and off.
|
71
|
+
email:
|
82
72
|
- steve.loveless@gmail.com
|
83
73
|
executables: []
|
84
|
-
|
85
74
|
extensions: []
|
86
|
-
|
87
75
|
extra_rdoc_files: []
|
88
|
-
|
89
|
-
files:
|
76
|
+
files:
|
90
77
|
- lib/log_switch/version.rb
|
91
78
|
- lib/log_switch.rb
|
92
79
|
- spec/log_switch_spec.rb
|
@@ -99,39 +86,30 @@ files:
|
|
99
86
|
- Rakefile
|
100
87
|
homepage: http://github.com/turboladen/log_switch
|
101
88
|
licenses: []
|
102
|
-
|
103
89
|
post_install_message:
|
104
90
|
rdoc_options: []
|
105
|
-
|
106
|
-
require_paths:
|
91
|
+
require_paths:
|
107
92
|
- lib
|
108
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
94
|
none: false
|
110
|
-
requirements:
|
111
|
-
- -
|
112
|
-
- !ruby/object:Gem::Version
|
113
|
-
|
114
|
-
|
115
|
-
- 0
|
116
|
-
version: "0"
|
117
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
100
|
none: false
|
119
|
-
requirements:
|
120
|
-
- -
|
121
|
-
- !ruby/object:Gem::Version
|
122
|
-
hash: 55
|
123
|
-
segments:
|
124
|
-
- 1
|
125
|
-
- 8
|
126
|
-
- 0
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
127
104
|
version: 1.8.0
|
128
105
|
requirements: []
|
129
|
-
|
130
106
|
rubyforge_project:
|
131
|
-
rubygems_version: 1.8.
|
107
|
+
rubygems_version: 1.8.10
|
132
108
|
signing_key:
|
133
109
|
specification_version: 3
|
134
|
-
summary: Extends a class for singleton style logging that can easily be turned on
|
135
|
-
|
110
|
+
summary: Extends a class for singleton style logging that can easily be turned on
|
111
|
+
and off.
|
112
|
+
test_files:
|
136
113
|
- spec/log_switch_spec.rb
|
137
114
|
- spec/spec_helper.rb
|
115
|
+
has_rdoc:
|