logger-joint 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e45acde6b29657925fcfb1129ac063cffe029bb0edf81eced14b54499d4a957f
4
+ data.tar.gz: 603cd6fc299340edb2375cfb3d4a2e2cc5241b6195b1a78f150910cee9b5d6ce
5
+ SHA512:
6
+ metadata.gz: 1dcf661052c15f5e3b518660df74363e282b8f28b08145e8f18c2be644cfaf2803fd629fdaef173adca437fa9d235a6c385580de12399328f0df1311a9e60d9d
7
+ data.tar.gz: be85060c678684a7d4a563373c9a8ecc4f0e9ab0c4080b804bc4d3830128d23187d2b99ab7068d4567abbb5c560f3f0c70530a18ffa9ba1c4b98c516c446e615
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+ /Gemfile.lock
11
+ /README.html
12
+ *~
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in logger-joint.gemspec
8
+ gemspec
9
+
10
+ # Local Variables:
11
+ # mode: Ruby
12
+ # indent-tabs-mode: nil
13
+ # End:
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TOKI Yoshinori
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,115 @@
1
+ Logger::Joint
2
+ =============
3
+
4
+ logger-joint is a utility to joint multiple loggers into one logger.
5
+ Logs can be output to multiple output destinations at the same time
6
+ with one jointed logger.
7
+
8
+ Installation
9
+ ------------
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'logger-joint'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install logger-joint
24
+
25
+ Usage
26
+ -----
27
+
28
+ Using logger-joint is simple.
29
+ Create an instance of `Logger::Joint` that joints two loggers.
30
+ When a log is output to an instance of `Logger::Joint`, that log is
31
+ output to each logger.
32
+
33
+ ```ruby
34
+ irb(main):001:0> require 'stringio'
35
+ => false
36
+ irb(main):002:0> out1 = StringIO.new
37
+ => #<StringIO:...>
38
+ irb(main):003:0> out2 = StringIO.new
39
+ => #<StringIO:...>
40
+ irb(main):004:0> require 'logger'
41
+ => true
42
+ irb(main):005:0> logger1 = Logger.new(out1)
43
+ => #<Logger:...>
44
+ irb(main):006:0> logger2 = Logger.new(out2)
45
+ => #<Logger:...>
46
+ irb(main):007:0> require 'logger/joint'
47
+ => true
48
+ irb(main):008:0> joint_logger = Logger::Joint.new(logger1, logger2)
49
+ => #<Logger::Joint:... @primary=#<Logger:...>, @secondary=#<Logger:...>>
50
+ irb(main):009:0> joint_logger.info('HALO')
51
+ => true
52
+ irb(main):010:0> out1.string
53
+ => "I, [2019-03-10T15:46:37.815496 #15407] INFO -- : HALO\n"
54
+ irb(main):011:0> out2.string
55
+ => "I, [2019-03-10T15:46:37.815669 #15407] INFO -- : HALO\n"
56
+ ```
57
+
58
+ An instance of `Logger::Joint` can be jointed with another logger.
59
+
60
+ ```ruby
61
+ irb(main):012:0> out3 = StringIO.new
62
+ => #<StringIO:...>
63
+ irb(main):013:0> logger3 = Logger.new(out3)
64
+ => #<Logger:...>
65
+ irb(main):014:0> next_joint_logger = joint_logger + logger3
66
+ => #<Logger::Joint:... @primary=#<Logger::Joint:... @primary=#<Logger:...>, @secondary=#<Logger:...>>, @secondary=#<Logger:...>>
67
+ irb(main):015:0> next_joint_logger.info('foo')
68
+ => true
69
+ irb(main):016:0> out1.string
70
+ => "I, [2019-03-10T15:46:37.815496 #15407] INFO -- : HALO\nI, [2019-03-10T15:56:35.320060 #15416] INFO -- : foo\n"
71
+ irb(main):017:0> out2.string
72
+ => "I, [2019-03-10T15:46:37.815669 #15407] INFO -- : HALO\nI, [2019-03-10T15:56:35.320521 #15416] INFO -- : foo\n"
73
+ irb(main):018:0> out3.string
74
+ => "I, [2019-03-10T15:56:35.320643 #15416] INFO -- : foo\n"
75
+ ```
76
+
77
+ By using refinement of `Logger::JointPlus` it is possible to extend
78
+ the logger object and joint the logger objects to create a
79
+ `Logger::Joint` instance.
80
+
81
+ ```ruby
82
+ irb(main):025:0> Logger.new(StringIO.new) + Logger.new(StringIO.new)
83
+ Traceback (most recent call last):
84
+ 4: from /usr/local/bin/irb:23:in `<main>'
85
+ 3: from /usr/local/bin/irb:23:in `load'
86
+ 2: from /usr/local/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
87
+ 1: from (irb):25
88
+ NoMethodError (undefined method `+' for #<Logger:0x00007ffff42da338>)
89
+ irb(main):026:0> module RefinementExample
90
+ irb(main):027:1> using Logger::JointPlus
91
+ irb(main):028:1> Logger.new(StringIO.new) + Logger.new(StringIO.new)
92
+ irb(main):029:1> end
93
+ => #<Logger::Joint:... @primary=#<Logger:...>, @secondary=#<Logger:...>>
94
+ ```
95
+
96
+ By applying monkey patch with `require 'logger/joint_plus'` it is
97
+ possible to extend the logger object globally and joint the logger
98
+ objects to create a `Logger::Joint` instance.
99
+
100
+ ```ruby
101
+ irb(main):030:0> require 'logger/joint_plus'
102
+ => true
103
+ irb(main):031:0> Logger.new(StringIO.new) + Logger.new(StringIO.new)
104
+ => #<Logger::Joint:... @primary=#<Logger:...>, @secondary=#<Logger:...>>
105
+ ```
106
+
107
+ Contributing
108
+ ------------
109
+
110
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/y10k/logger-joint>.
111
+
112
+ License
113
+ -------
114
+
115
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'rake/clean'
5
+ require 'rake/testtask'
6
+ require 'rdoc/task'
7
+
8
+ task :default => :spec
9
+
10
+ Rake::TestTask.new do |task|
11
+ if ((ENV.key? 'RUBY_DEBUG') && (! ENV['RUBY_DEBUG'].empty?)) then
12
+ task.ruby_opts << '-d'
13
+ end
14
+ end
15
+
16
+ Rake::RDocTask.new do |rd|
17
+ rd.rdoc_files.include('lib/**/*.rb')
18
+ end
19
+
20
+ desc 'Build README.html from markdown source.'
21
+ task :readme => %w[ README.html ]
22
+
23
+ file 'README.html' => [ 'README.md' ] do
24
+ sh "pandoc --from=markdown --to=html5 --standalone --self-contained --css=$HOME/.pandoc/github.css --output=README.html README.md"
25
+ end
26
+ CLOBBER.include 'README.html'
27
+
28
+ # Local Variables:
29
+ # mode: Ruby
30
+ # indent-tabs-mode: nil
31
+ # End:
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "logger/joint"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,115 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'logger'
4
+ require 'logger/joint/version'
5
+
6
+ class Logger::Joint
7
+ # methods common to Logger and Syslog::Logger are need for.
8
+ def self.respond_as_logger?(object)
9
+ [ :level,
10
+ :add,
11
+ :debug,
12
+ :info,
13
+ :warn,
14
+ :error,
15
+ :fatal,
16
+ :unknown,
17
+ :debug?,
18
+ :info?,
19
+ :warn?,
20
+ :error?,
21
+ :fatal?
22
+ ].all?{|name| object.respond_to? name }
23
+ end
24
+
25
+ def initialize(primary_logger, secondary_logger)
26
+ self.class.respond_as_logger? primary_logger or raise TypeError, 'primary is not a logger.'
27
+ self.class.respond_as_logger? secondary_logger or raise TypeError, 'secondary is not a logger.'
28
+ @primary = primary_logger
29
+ @secondary = secondary_logger
30
+ end
31
+
32
+ def level
33
+ [ @primary, @secondary ].map(&:level).min
34
+ end
35
+
36
+ def apply_method(name, *args, &block)
37
+ r = @primary.__send__(name, *args, &block)
38
+ @secondary.__send__(name, *args, &block)
39
+ r
40
+ end
41
+ private :apply_method
42
+
43
+ def add(*args, &block)
44
+ apply_method(:add, *args, &block)
45
+ end
46
+
47
+ def debug(*args, &block)
48
+ apply_method(:debug, *args, &block)
49
+ end
50
+
51
+ def info(*args, &block)
52
+ apply_method(:info, *args, &block)
53
+ end
54
+
55
+ def warn(*args, &block)
56
+ apply_method(:warn, *args, &block)
57
+ end
58
+
59
+ def error(*args, &block)
60
+ apply_method(:error, *args, &block)
61
+ end
62
+
63
+ def fatal(*args, &block)
64
+ apply_method(:fatal, *args, &block)
65
+ end
66
+
67
+ def unknown(*args, &block)
68
+ apply_method(:unknown, *args, &block)
69
+ end
70
+
71
+ def apply_level_predicate(name)
72
+ @primary.__send__(name) || @secondary.__send__(name)
73
+ end
74
+ private :apply_level_predicate
75
+
76
+ def debug?
77
+ apply_level_predicate(:debug?)
78
+ end
79
+
80
+ def info?
81
+ apply_level_predicate(:info?)
82
+ end
83
+
84
+ def warn?
85
+ apply_level_predicate(:warn?)
86
+ end
87
+
88
+ def error?
89
+ apply_level_predicate(:error?)
90
+ end
91
+
92
+ def fatal?
93
+ apply_level_predicate(:fatal?)
94
+ end
95
+
96
+ module PlusMethod
97
+ def joint(other_logger)
98
+ Logger::Joint.new(self, other_logger)
99
+ end
100
+
101
+ alias + joint
102
+ end
103
+ include PlusMethod
104
+ end
105
+
106
+ module Logger::JointPlus
107
+ refine Logger do
108
+ include Logger::Joint::PlusMethod
109
+ end
110
+ end
111
+
112
+ # Local Variables:
113
+ # mode: Ruby
114
+ # indent-tabs-mode: nil
115
+ # End:
@@ -0,0 +1,12 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'logger'
4
+
5
+ class Logger::Joint
6
+ VERSION = '0.1.0'
7
+ end
8
+
9
+ # Local Variables:
10
+ # mode: Ruby
11
+ # indent-tabs-mode: nil
12
+ # End:
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'logger'
4
+ require 'logger/joint'
5
+
6
+ Logger.class_eval{
7
+ include Logger::Joint::PlusMethod
8
+ }
9
+
10
+ # Local Variables:
11
+ # mode: Ruby
12
+ # indent-tabs-mode: nil
13
+ # End:
@@ -0,0 +1,40 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "logger/joint/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "logger-joint"
9
+ spec.version = Logger::Joint::VERSION
10
+ spec.authors = ["TOKI Yoshinori"]
11
+ spec.email = ["toki@freedom.ne.jp"]
12
+
13
+ spec.summary = %q{logger-joint is a utility to joint multiple loggers into one logger.}
14
+ spec.description = <<-'EOF'
15
+ logger-joint is a utility to joint multiple loggers into one logger.
16
+ Logs can be output to multiple output destinations at the same
17
+ time with one jointed logger.
18
+ EOF
19
+ spec.homepage = "https://github.com/y10k/logger-joint"
20
+ spec.license = "MIT"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.17"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "test-unit"
34
+ spec.add_development_dependency "rdoc"
35
+ end
36
+
37
+ # Local Variables:
38
+ # mode: Ruby
39
+ # indent-tabs-mode: nil
40
+ # End:
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logger-joint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TOKI Yoshinori
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: |2
70
+ logger-joint is a utility to joint multiple loggers into one logger.
71
+ Logs can be output to multiple output destinations at the same
72
+ time with one jointed logger.
73
+ email:
74
+ - toki@freedom.ne.jp
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/logger/joint.rb
87
+ - lib/logger/joint/version.rb
88
+ - lib/logger/joint_plus.rb
89
+ - logger-joint.gemspec
90
+ homepage: https://github.com/y10k/logger-joint
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.0.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: logger-joint is a utility to joint multiple loggers into one logger.
113
+ test_files: []