entity_logger 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 +20 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +48 -0
- data/Rakefile +2 -0
- data/entity_logger.gemspec +23 -0
- data/lib/entity_logger/mixin.rb +56 -0
- data/lib/entity_logger/tagged_logging.rb +81 -0
- data/lib/entity_logger/version.rb +3 -0
- data/lib/entity_logger.rb +5 -0
- data/spec/entity_logger_spec.rb +39 -0
- data/spec/spec_helper.rb +26 -0
- metadata +130 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 m.filippovich
|
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,48 @@
|
|
1
|
+
# EntityLogger
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'entity_logger'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install entity_logger
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
```ruby
|
21
|
+
require 'entity_logger'
|
22
|
+
|
23
|
+
class Entity
|
24
|
+
include EntityLogger::Mixin
|
25
|
+
|
26
|
+
log Logger.new(STDOUT), 'Prefix', :some_attr, or_lambda: lambda { |e| e.some_attr + e.some_attr1 }
|
27
|
+
|
28
|
+
def some_attr
|
29
|
+
'Some attr'
|
30
|
+
end
|
31
|
+
|
32
|
+
def some_attr1
|
33
|
+
1
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
entity = Entity.new
|
38
|
+
entity.info 'Some log message'
|
39
|
+
# Prefix [Some attr] [Some attr1] Some log message
|
40
|
+
```
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/entity_logger/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["m.filippovich"]
|
6
|
+
gem.email = ["fatumka@gmail.com"]
|
7
|
+
gem.description = %q{Entity logger}
|
8
|
+
gem.summary = %q{Extend entity with tag logging}
|
9
|
+
gem.homepage = "http://twitter.com/mfilippovich"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "entity_logger"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = EntityLogger::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'activesupport', '>= 3.0'
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
gem.add_development_dependency 'pry'
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/core_ext/class'
|
3
|
+
require 'active_support/core_ext/object/blank'
|
4
|
+
require 'active_support/core_ext/class/attribute'
|
5
|
+
require 'active_support/deprecation'
|
6
|
+
require 'entity_logger/tagged_logging'
|
7
|
+
|
8
|
+
module EntityLogger
|
9
|
+
module Mixin
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
included do
|
13
|
+
def self.log(*attrs)
|
14
|
+
self.logger_writer = attrs.shift
|
15
|
+
self.prefix = attrs.shift
|
16
|
+
self.tags_for_logging = attrs
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
class_attribute :tags_for_logging, :logger_writer, :prefix
|
21
|
+
end
|
22
|
+
|
23
|
+
def log_with_tags(&block)
|
24
|
+
tags = extract_tags(self.tags_for_logging)
|
25
|
+
logger.tagged(tags) { yield } if tags
|
26
|
+
end
|
27
|
+
|
28
|
+
def logger
|
29
|
+
EntityLogger::TaggedLogging.new(self.logger_writer, self.prefix)
|
30
|
+
end
|
31
|
+
|
32
|
+
%w(info error debug).each do |level|
|
33
|
+
define_method(level) do |msg|
|
34
|
+
tags = extract_tags(self.tags_for_logging)
|
35
|
+
|
36
|
+
if tags
|
37
|
+
logger.tagged(tags) { logger.send(level, msg) }
|
38
|
+
else
|
39
|
+
logger.send(level, msg)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def extract_tags(attrs)
|
46
|
+
attrs.map do |attr|
|
47
|
+
case attr
|
48
|
+
when Hash
|
49
|
+
attr.values.first.call(self)
|
50
|
+
else
|
51
|
+
send(attr)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
|
+
require 'active_support/deprecation'
|
3
|
+
require 'active_support/buffered_logger'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
module EntityLogger
|
7
|
+
# Wraps any standard Logger class to provide tagging capabilities. Examples:
|
8
|
+
#
|
9
|
+
# Logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
|
10
|
+
# Logger.tagged("BCX") { Logger.info "Stuff" } # Logs "[BCX] Stuff"
|
11
|
+
# Logger.tagged("BCX", "Jason") { Logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff"
|
12
|
+
# Logger.tagged("BCX") { Logger.tagged("Jason") { Logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
|
13
|
+
#
|
14
|
+
# This is used by the default Rails.logger as configured by Railties to make it easy to stamp log lines
|
15
|
+
# with subdomains, request ids, and anything else to aid debugging of multi-user production applications.
|
16
|
+
class TaggedLogging
|
17
|
+
def initialize(logger, prefix)
|
18
|
+
@logger, @prefix = logger, prefix
|
19
|
+
end
|
20
|
+
|
21
|
+
def tagged(*tags)
|
22
|
+
new_tags = push_tags(*tags)
|
23
|
+
yield self
|
24
|
+
ensure
|
25
|
+
pop_tags(new_tags.size)
|
26
|
+
end
|
27
|
+
|
28
|
+
def push_tags(*tags)
|
29
|
+
tags.flatten.reject(&:blank?).tap do |new_tags|
|
30
|
+
current_tags.concat new_tags
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def pop_tags(size = 1)
|
35
|
+
current_tags.pop size
|
36
|
+
end
|
37
|
+
|
38
|
+
def clear_tags!
|
39
|
+
current_tags.clear
|
40
|
+
end
|
41
|
+
|
42
|
+
def silence(temporary_level = Logger::ERROR, &block)
|
43
|
+
@logger.silence(temporary_level, &block)
|
44
|
+
end
|
45
|
+
deprecate :silence
|
46
|
+
|
47
|
+
def add(severity, message = nil, progname = nil, &block)
|
48
|
+
message = (block_given? ? block.call : progname) if message.nil?
|
49
|
+
@logger.add(severity, "#{@prefix} #{tags_text}#{message}", progname)
|
50
|
+
end
|
51
|
+
|
52
|
+
%w( fatal error warn info debug unknown ).each do |severity|
|
53
|
+
eval <<-EOM, nil, __FILE__, __LINE__ + 1
|
54
|
+
def #{severity}(progname = nil, &block)
|
55
|
+
add(Logger::#{severity.upcase}, nil, progname, &block)
|
56
|
+
end
|
57
|
+
EOM
|
58
|
+
end
|
59
|
+
|
60
|
+
def flush
|
61
|
+
clear_tags!
|
62
|
+
@logger.flush if @logger.respond_to?(:flush)
|
63
|
+
end
|
64
|
+
|
65
|
+
def method_missing(method, *args)
|
66
|
+
@logger.send(method, *args)
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
def tags_text
|
71
|
+
tags = current_tags
|
72
|
+
if tags.any?
|
73
|
+
tags.collect { |tag| "[#{tag}] " }.join
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def current_tags
|
78
|
+
Thread.current[:activesupport_tagged_logging_tags] ||= []
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EntityLogger do
|
4
|
+
class EntityClass2
|
5
|
+
include EntityLogger::Mixin
|
6
|
+
|
7
|
+
log Logger.new(STDOUT), 'Prefix', :attr22
|
8
|
+
end
|
9
|
+
|
10
|
+
class EntityClass
|
11
|
+
include EntityLogger::Mixin
|
12
|
+
|
13
|
+
log Logger.new(STDOUT), 'Prefix',
|
14
|
+
:attr1, :attr1, :attr3 => lambda { |e| e.attr1 + e.attr2 }
|
15
|
+
|
16
|
+
def attr1
|
17
|
+
'test1'
|
18
|
+
end
|
19
|
+
|
20
|
+
def attr2
|
21
|
+
'test2'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#log_with_tags" do
|
26
|
+
it 'should wrap inner log call with tags' do
|
27
|
+
obj = EntityClass.new
|
28
|
+
obj.log_with_tags { obj.logger.info('test') }.should be_true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
%w(info debug error).each do |level|
|
33
|
+
it "should receive logger method: #{level}" do
|
34
|
+
obj = EntityClass.new
|
35
|
+
EntityLogger::TaggedLogging.any_instance.should_receive(level.to_sym).with('Test')
|
36
|
+
obj.send(level, 'Test')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'bundler/setup'
|
10
|
+
|
11
|
+
Bundler.require
|
12
|
+
|
13
|
+
require 'pry'
|
14
|
+
require 'entity_logger'
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
18
|
+
config.run_all_when_everything_filtered = true
|
19
|
+
config.filter_run :focus
|
20
|
+
|
21
|
+
# Run specs in random order to surface order dependencies. If you find an
|
22
|
+
# order dependency and want to debug it, you can fix the order by providing
|
23
|
+
# the seed, which is printed after each run.
|
24
|
+
# --seed 1234
|
25
|
+
config.order = 'random'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: entity_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- m.filippovich
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Entity logger
|
79
|
+
email:
|
80
|
+
- fatumka@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- entity_logger.gemspec
|
92
|
+
- lib/entity_logger.rb
|
93
|
+
- lib/entity_logger/mixin.rb
|
94
|
+
- lib/entity_logger/tagged_logging.rb
|
95
|
+
- lib/entity_logger/version.rb
|
96
|
+
- spec/entity_logger_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage: http://twitter.com/mfilippovich
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
hash: -983180373507418601
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
hash: -983180373507418601
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 1.8.24
|
125
|
+
signing_key:
|
126
|
+
specification_version: 3
|
127
|
+
summary: Extend entity with tag logging
|
128
|
+
test_files:
|
129
|
+
- spec/entity_logger_spec.rb
|
130
|
+
- spec/spec_helper.rb
|