nestable_logger 0.1.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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +73 -0
- data/VERSION +1 -0
- data/lib/nestable_logger.rb +90 -0
- data/nestable_logger.gemspec +56 -0
- data/spec/nestable_logger_spec.rb +118 -0
- data/spec/rcov.opts +1 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +95 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 akimatter
|
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,17 @@
|
|
1
|
+
= nestable_logger
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(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)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 akimatter. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "nestable_logger"
|
8
|
+
gem.summary = %Q{nestable_logger supports nesting logging descriptions}
|
9
|
+
gem.description = %Q{nestable_logger supports nesting logging descriptions}
|
10
|
+
gem.email = "akm2000@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/akm/nestable_logger"
|
12
|
+
gem.authors = ["akimatter"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
spec.rcov_opts = lambda do
|
32
|
+
IO.readlines("spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
task :spec => :check_dependencies
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'reek/adapters/rake_task'
|
40
|
+
Reek::RakeTask.new do |t|
|
41
|
+
t.fail_on_error = true
|
42
|
+
t.verbose = false
|
43
|
+
t.source_files = 'lib/**/*.rb'
|
44
|
+
end
|
45
|
+
rescue LoadError
|
46
|
+
task :reek do
|
47
|
+
abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
begin
|
52
|
+
require 'roodi'
|
53
|
+
require 'roodi_task'
|
54
|
+
RoodiTask.new do |t|
|
55
|
+
t.verbose = false
|
56
|
+
end
|
57
|
+
rescue LoadError
|
58
|
+
task :roodi do
|
59
|
+
abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
task :default => :spec
|
64
|
+
|
65
|
+
require 'rake/rdoctask'
|
66
|
+
Rake::RDocTask.new do |rdoc|
|
67
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
68
|
+
|
69
|
+
rdoc.rdoc_dir = 'rdoc'
|
70
|
+
rdoc.title = "nestable_logger #{version}"
|
71
|
+
rdoc.rdoc_files.include('README*')
|
72
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
73
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
class NestableLogger
|
3
|
+
LEVELS = %w[debug info warn error fatal unknown].freeze
|
4
|
+
|
5
|
+
# Loggerの定数と同じ
|
6
|
+
DEBUG = 0
|
7
|
+
INFO = 1
|
8
|
+
WARN = 2
|
9
|
+
ERROR = 3
|
10
|
+
FATAL = 4
|
11
|
+
UNKNOWN = 5
|
12
|
+
|
13
|
+
attr_reader :options
|
14
|
+
def initialize(impl, options = {})
|
15
|
+
@impl = impl
|
16
|
+
@options = options || {}
|
17
|
+
@msg_stack = []
|
18
|
+
@last_index = 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_method(severity, object, method_name = nil, msg = nil, &block)
|
22
|
+
method_name ||= caller[1].scan(/\`(.+)\'/).flatten.first
|
23
|
+
name = object.is_a?(Module) ? "#{object.name}.#{method_name}" : "#{object.class.name}##{method_name}"
|
24
|
+
name << ' ' << msg if msg
|
25
|
+
stack(name) do |m|
|
26
|
+
add(severity, m, &block)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_index(severity, msg = nil, &block)
|
31
|
+
index = (@last_index || 0).succ
|
32
|
+
@last_index = index
|
33
|
+
msg = msg ? "#{index.to_s} #{msg}" : index.to_s
|
34
|
+
stack(msg) do |m|
|
35
|
+
add(severity, m, &block)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def add(severity, msg)
|
40
|
+
if block_given?
|
41
|
+
@impl.send(LEVELS[severity.to_i], "#{msg} S")
|
42
|
+
begin
|
43
|
+
last_index_backup = @last_index
|
44
|
+
@last_index = 0
|
45
|
+
begin
|
46
|
+
result = yield
|
47
|
+
ensure
|
48
|
+
@last_index = last_index_backup
|
49
|
+
end
|
50
|
+
@impl.send(LEVELS[severity.to_i], "#{msg} E")
|
51
|
+
return result
|
52
|
+
rescue Exception => err
|
53
|
+
# 実装中にspecしたときにメッセージがややこしくなるのでSpec関係のエラーは無視
|
54
|
+
unless err.class.name =~ /^Spec::Mocks::/
|
55
|
+
@impl.send(LEVELS[severity.to_i],
|
56
|
+
"#{msg} ERROR\n [#{err.class.name}] #{err.to_s}\n " << err.backtrace.join("\n "))
|
57
|
+
end
|
58
|
+
raise
|
59
|
+
end
|
60
|
+
else
|
61
|
+
@impl.send(LEVELS[severity.to_i], msg)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def stack(msg)
|
66
|
+
@msg_stack << (msg || '')
|
67
|
+
begin
|
68
|
+
yield(@msg_stack.join('.'))
|
69
|
+
ensure
|
70
|
+
@msg_stack.pop
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
(LEVELS - %w[unknown]).each do |level|
|
75
|
+
module_eval(<<-"EOS", __FILE__, __LINE__ + 1)
|
76
|
+
def #{level}_method(*args, &block)
|
77
|
+
add_method(#{level.upcase}, *args, &block)
|
78
|
+
end
|
79
|
+
|
80
|
+
def #{level}_index(*args, &block)
|
81
|
+
add_index(#{level.upcase}, *args, &block)
|
82
|
+
end
|
83
|
+
|
84
|
+
def #{level}(*args, &block)
|
85
|
+
add(#{level.upcase}, *args, &block)
|
86
|
+
end
|
87
|
+
EOS
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
@@ -0,0 +1,56 @@
|
|
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{nestable_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 = ["akimatter"]
|
12
|
+
s.date = %q{2010-08-05}
|
13
|
+
s.description = %q{nestable_logger supports nesting logging descriptions}
|
14
|
+
s.email = %q{akm2000@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/nestable_logger.rb",
|
27
|
+
"nestable_logger.gemspec",
|
28
|
+
"spec/nestable_logger_spec.rb",
|
29
|
+
"spec/rcov.opts",
|
30
|
+
"spec/spec.opts",
|
31
|
+
"spec/spec_helper.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/akm/nestable_logger}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.7}
|
37
|
+
s.summary = %q{nestable_logger supports nesting logging descriptions}
|
38
|
+
s.test_files = [
|
39
|
+
"spec/nestable_logger_spec.rb",
|
40
|
+
"spec/spec_helper.rb"
|
41
|
+
]
|
42
|
+
|
43
|
+
if s.respond_to? :specification_version then
|
44
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
51
|
+
end
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.expand_path('./spec_helper', File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'logger'
|
5
|
+
require 'stringio'
|
6
|
+
|
7
|
+
describe NestableLogger do
|
8
|
+
|
9
|
+
class TestLogger
|
10
|
+
attr_reader :logs
|
11
|
+
def initialize
|
12
|
+
@logs = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def debug(msg)
|
16
|
+
@logs << msg
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Target1
|
21
|
+
|
22
|
+
attr_reader :logger
|
23
|
+
def initialize(logger)
|
24
|
+
@logger = logger
|
25
|
+
end
|
26
|
+
|
27
|
+
def foo
|
28
|
+
logger.debug_method(self, "foo") do
|
29
|
+
logger.debug_index do
|
30
|
+
logger.debug_index("abc")
|
31
|
+
# puts "foo1"
|
32
|
+
logger.debug_index
|
33
|
+
"FOO" # 最後の評価が戻り値になる
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def bar
|
39
|
+
logger.debug_method(self) do # メソッド名が自動補完される
|
40
|
+
logger.debug_index do
|
41
|
+
logger.debug("abc")
|
42
|
+
# puts "foo1"
|
43
|
+
logger.debug("www")
|
44
|
+
"BAR" # 最後の評価が戻り値になる
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def hook_error_and_raise_it
|
50
|
+
logger.debug_method(self, "foo") do
|
51
|
+
logger.debug_index do
|
52
|
+
raise StandardError, "Some exception occured"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
describe :options do
|
60
|
+
before do
|
61
|
+
@impl = Logger.new(STDOUT)
|
62
|
+
@logger = NestableLogger.new(@impl)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "何も設定しなくてもオプションは取得できる" do
|
66
|
+
@logger.options.should == {
|
67
|
+
}
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe :logging do
|
72
|
+
before do
|
73
|
+
@impl = TestLogger.new
|
74
|
+
@logger = NestableLogger.new(@impl)
|
75
|
+
end
|
76
|
+
|
77
|
+
context "nested call" do
|
78
|
+
it "foo" do
|
79
|
+
@target1 = Target1.new(@logger)
|
80
|
+
@target1.foo.should == "FOO" # ちゃんと戻り値が返ってくる
|
81
|
+
@impl.logs.should == [
|
82
|
+
"Target1#foo S" ,
|
83
|
+
"Target1#foo.1 S" ,
|
84
|
+
"Target1#foo.1.1 abc",
|
85
|
+
"Target1#foo.1.2" ,
|
86
|
+
"Target1#foo.1 E" ,
|
87
|
+
"Target1#foo E" ,
|
88
|
+
]
|
89
|
+
end
|
90
|
+
|
91
|
+
it "bar" do
|
92
|
+
@target1 = Target1.new(@logger)
|
93
|
+
@target1.bar.should == "BAR" # ちゃんと戻り値が返ってくる
|
94
|
+
@impl.logs.should == [
|
95
|
+
"Target1#bar S" ,
|
96
|
+
"Target1#bar.1 S" ,
|
97
|
+
"abc" ,
|
98
|
+
"www" ,
|
99
|
+
"Target1#bar.1 E" ,
|
100
|
+
"Target1#bar E" ,
|
101
|
+
]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
it "error log" do
|
106
|
+
@target1 = Target1.new(@logger)
|
107
|
+
lambda{
|
108
|
+
@target1.hook_error_and_raise_it
|
109
|
+
}.should raise_error(StandardError)
|
110
|
+
@impl.logs[0].should == "Target1#foo S"
|
111
|
+
@impl.logs[1].should == "Target1#foo.1 S"
|
112
|
+
error_lines = @impl.logs[2].split(/$/).map{|line| line.strip}
|
113
|
+
error_lines[0].should == "Target1#foo.1 ERROR"
|
114
|
+
error_lines[1].should == "[StandardError] Some exception occured"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
data/spec/rcov.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--exclude "spec/*,gems/*"
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nestable_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- akimatter
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-05 00:00:00 +09:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 9
|
34
|
+
version: 1.2.9
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: nestable_logger supports nesting logging descriptions
|
38
|
+
email: akm2000@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- LICENSE
|
45
|
+
- README.rdoc
|
46
|
+
files:
|
47
|
+
- .document
|
48
|
+
- .gitignore
|
49
|
+
- LICENSE
|
50
|
+
- README.rdoc
|
51
|
+
- Rakefile
|
52
|
+
- VERSION
|
53
|
+
- lib/nestable_logger.rb
|
54
|
+
- nestable_logger.gemspec
|
55
|
+
- spec/nestable_logger_spec.rb
|
56
|
+
- spec/rcov.opts
|
57
|
+
- spec/spec.opts
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/akm/nestable_logger
|
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: nestable_logger supports nesting logging descriptions
|
93
|
+
test_files:
|
94
|
+
- spec/nestable_logger_spec.rb
|
95
|
+
- spec/spec_helper.rb
|