taglog 0.0.1
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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +77 -0
- data/Rakefile +1 -0
- data/lib/taglog.rb +57 -0
- data/lib/taglog/version.rb +3 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/taglog_spec.rb +63 -0
- data/taglog.gemspec +21 -0
- metadata +91 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ryo Nakamura
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Taglog
|
2
|
+
Taglog provides taggable logger extension to any class which has `#logger` method.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
```
|
6
|
+
$ gem install taglog
|
7
|
+
```
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
### Basic
|
12
|
+
```ruby
|
13
|
+
# a class which has `#logger` method
|
14
|
+
class A
|
15
|
+
def hello
|
16
|
+
logger.debug("hello")
|
17
|
+
end
|
18
|
+
|
19
|
+
def log
|
20
|
+
io.string
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def logger
|
26
|
+
@logger ||= Logger.new(io)
|
27
|
+
end
|
28
|
+
|
29
|
+
def io
|
30
|
+
@io ||= StringIO.new
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# extend class A with Taglog
|
35
|
+
A.extend Taglog.new("class A")
|
36
|
+
|
37
|
+
# use logger
|
38
|
+
a = A.new
|
39
|
+
a.hello
|
40
|
+
a.hello
|
41
|
+
a.hello
|
42
|
+
puts a.log
|
43
|
+
```
|
44
|
+
|
45
|
+
```
|
46
|
+
D, [2012-12-11T00:16:03.032614 #679] DEBUG -- : [class A] hello
|
47
|
+
D, [2012-12-11T00:16:03.252622 #679] DEBUG -- : [class A] hello
|
48
|
+
D, [2012-12-11T00:16:03.680119 #679] DEBUG -- : [class A] hello
|
49
|
+
```
|
50
|
+
|
51
|
+
### Rails
|
52
|
+
```ruby
|
53
|
+
# Gemfile
|
54
|
+
group :development do
|
55
|
+
gem "taglog"
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# config/environments/development.rb
|
61
|
+
require "active_record/log_subscriber"
|
62
|
+
require "action_view/log_subscriber"
|
63
|
+
require "action_controller/log_subscriber"
|
64
|
+
ActiveRecord::LogSubscriber.extend Taglog.new("M")
|
65
|
+
ActionView::LogSubscriber.extend Taglog.new("V")
|
66
|
+
ActionController::LogSubscriber.extend Taglog.new("C")
|
67
|
+
```
|
68
|
+
|
69
|
+
```
|
70
|
+
$ rails s
|
71
|
+
...
|
72
|
+
[C] Processing by EntriesController#show as HTML
|
73
|
+
[C] Parameters: {"id"=>"1234567"}
|
74
|
+
[V] Rendered entries/show.html.erb (7.5ms)
|
75
|
+
[M] Entry Exists (6.4ms) SELECT 1 AS one FROM ...
|
76
|
+
[C] Completed 200 OK in 88ms ...
|
77
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/taglog.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "taglog/version"
|
2
|
+
require "logger"
|
3
|
+
require "stringio"
|
4
|
+
|
5
|
+
# Wrap a class that has `#logger` method to insert tag
|
6
|
+
# without over defining actual logger's methods.
|
7
|
+
class Taglog < Module
|
8
|
+
LEVELS = Logger::Severity.constants.map(&:downcase)
|
9
|
+
|
10
|
+
def initialize(tag)
|
11
|
+
@tag = tag
|
12
|
+
end
|
13
|
+
|
14
|
+
def extended(base)
|
15
|
+
tag = @tag
|
16
|
+
base.class_eval do
|
17
|
+
define_method(:logger_with_tag) do
|
18
|
+
Proxy.new(self, tag)
|
19
|
+
end
|
20
|
+
alias_method :logger_without_tag, :logger
|
21
|
+
alias_method :logger, :logger_with_tag
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Proxy
|
26
|
+
def initialize(context, tag)
|
27
|
+
@context = context
|
28
|
+
@tag = tag
|
29
|
+
end
|
30
|
+
|
31
|
+
LEVELS.each do |level|
|
32
|
+
define_method(level) do |message|
|
33
|
+
delegate(level, tagged(message))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def method_missing(name, *args)
|
38
|
+
delegate(name, *args)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
attr_reader :context, :tag
|
44
|
+
|
45
|
+
def tagged(message)
|
46
|
+
"[#{tag}] #{message}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def delegate(method_name, *args)
|
50
|
+
logger.send(method_name, *args)
|
51
|
+
end
|
52
|
+
|
53
|
+
def logger
|
54
|
+
context.send(:logger_without_tag)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/taglog_spec.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "logger"
|
3
|
+
require "stringio"
|
4
|
+
|
5
|
+
describe Taglog do
|
6
|
+
let(:taglog) do
|
7
|
+
described_class.new(tag)
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:tag) do
|
11
|
+
"tag"
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:logger) do
|
15
|
+
Logger.new(io).tap do |this|
|
16
|
+
this.formatter = formatter
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:formatter) do
|
21
|
+
proc {|_, _, _, message| message }
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:io) do
|
25
|
+
StringIO.new
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:result) do
|
29
|
+
io.string
|
30
|
+
end
|
31
|
+
|
32
|
+
let(:klass) do
|
33
|
+
logger = logger()
|
34
|
+
Class.new do
|
35
|
+
define_method(:logger) do
|
36
|
+
logger
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#extended" do
|
42
|
+
before do
|
43
|
+
klass.extend taglog
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#info" do
|
47
|
+
it "is wrapped by tag" do
|
48
|
+
klass.new.logger.info("message")
|
49
|
+
result.should == "[tag] message"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#method_missing" do
|
54
|
+
before do
|
55
|
+
logger.stub(:missing => "missing")
|
56
|
+
end
|
57
|
+
|
58
|
+
it "is delegated to original logger" do
|
59
|
+
klass.new.logger.missing.should == "missing"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/taglog.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'taglog/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "taglog"
|
8
|
+
gem.version = Taglog::VERSION
|
9
|
+
gem.authors = ["Ryo Nakamura"]
|
10
|
+
gem.email = ["ryo-nakamura@cookpad.com"]
|
11
|
+
gem.description = "Taglog provides taggable logger extension"
|
12
|
+
gem.summary = "Taggable logger extension"
|
13
|
+
gem.homepage = "https://github.com/r7kamura/taglog"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec", "2.12.0"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: taglog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ryo Nakamura
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-12-11 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - "="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 63
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 12
|
32
|
+
- 0
|
33
|
+
version: 2.12.0
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
description: Taglog provides taggable logger extension
|
37
|
+
email:
|
38
|
+
- ryo-nakamura@cookpad.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- Gemfile
|
48
|
+
- LICENSE.txt
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/taglog.rb
|
52
|
+
- lib/taglog/version.rb
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
- spec/taglog_spec.rb
|
55
|
+
- taglog.gemspec
|
56
|
+
homepage: https://github.com/r7kamura/taglog
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.24
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Taggable logger extension
|
89
|
+
test_files:
|
90
|
+
- spec/spec_helper.rb
|
91
|
+
- spec/taglog_spec.rb
|