logdup 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9a6a4a8c286b0b1410c53b27f03fbde54609d95
4
+ data.tar.gz: cb499d4f204a3378bad5668869e5ca8128e7f3dd
5
+ SHA512:
6
+ metadata.gz: b0c9e88a873991db18bd379293828b1dc75191b092de67ba152b3262a12c0ddf846d3fb30a9cb3c0826131bc6ba34b634a2dd1314162cc05ee07202fa1c864ad
7
+ data.tar.gz: d8b38667831efc11a1f2ebb99393edec0575fe7d9eaaf9aa8bbfe6e0afae88cc3a285d754bb596ea0935b3d5c1cebff4d02dabf97c1371c6b03ce32129caf6f5
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/bundle
19
+ *.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in logdup.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 pinzolo
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,58 @@
1
+ # Logdup
2
+
3
+ [![Build Status](https://secure.travis-ci.org/pinzolo/logdup.png)](http://travis-ci.org/pinzolo/logdup)
4
+ [![Coverage Status](https://coveralls.io/repos/pinzolo/logdup/badge.png)](https://coveralls.io/r/pinzolo/logdup)
5
+
6
+ `Logdup` duplicates logs partially.
7
+ This extends `Logger` that is a built-in library of Ruby.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'logdup'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install logdup
22
+
23
+ ## Usage
24
+
25
+ #### code
26
+
27
+ ```ruby
28
+ logger = Logger.new("base.log")
29
+ logger.info("aaa")
30
+ logger.dup_to("other.log") do
31
+ logger.info("bbb")
32
+ end
33
+ logger.info("ccc")
34
+ ```
35
+
36
+ #### result image
37
+
38
+ $ cat base.log
39
+ aaa
40
+ bbb
41
+ ccc
42
+
43
+ $ cat other.log
44
+ bbb
45
+
46
+ ## Supported ruby versions
47
+
48
+ * 1.9.3
49
+ * 2.0.0
50
+ * 2.1.0
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => [:spec]
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = 'spec/**/*_spec.rb'
8
+ spec.rspec_opts = ['-cfs']
9
+ end
10
+ rescue LoadError => e
11
+ end
@@ -0,0 +1,48 @@
1
+ # coding: utf-8
2
+ require 'logger'
3
+
4
+ module Logdup
5
+ class LogDuplication
6
+ attr_reader :device, :logs, :async_output, :buffer_size
7
+
8
+ def initialize(device, options = {})
9
+ @device = device
10
+ @async_output = options[:async_output] || false
11
+ @buffer_size = options[:buffer_size]
12
+ @logs = []
13
+ @thread_id = Thread.current.object_id
14
+ end
15
+
16
+ def <<(message)
17
+ if @thread_id == Thread.current.object_id
18
+ put_first_log_if_size_over
19
+ logs << message
20
+ end
21
+ end
22
+
23
+ def output
24
+ @async_output ? output_async : output_sync
25
+ end
26
+
27
+ private
28
+
29
+ def logdev
30
+ @logdev ||= Logger::LogDevice.new(device)
31
+ end
32
+
33
+ def output_sync
34
+ logs.each { |log| logdev.write(log) }
35
+ logdev.close
36
+ end
37
+
38
+ def output_async
39
+ Thread.new { output_sync }
40
+ end
41
+
42
+ def put_first_log_if_size_over
43
+ if buffer_size && logs.size >= buffer_size
44
+ logdev.write(logs.shift)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ require 'logger'
3
+ require 'logdup/log_duplication'
4
+
5
+ class Logger
6
+ alias_method :__format_message__, :format_message
7
+
8
+ def dup_to(device, options = {}, &block)
9
+ duplications << Logdup::LogDuplication.new(device, options)
10
+ yield(block)
11
+ duplications.pop.output
12
+ end
13
+
14
+ private
15
+
16
+ def format_message(severity, datetime, progname, msg)
17
+ message = __format_message__(severity, datetime, progname, msg)
18
+ duplications.each do |duplication|
19
+ duplication << message
20
+ end
21
+ message
22
+ end
23
+
24
+ def duplications
25
+ @duplications ||= []
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ # coding: utf-8
2
+ module Logdup
3
+ VERSION = "1.0.1"
4
+ end
data/lib/logdup.rb ADDED
@@ -0,0 +1,3 @@
1
+ # coding: utf-8
2
+ require 'logdup/version'
3
+ require 'logdup/logger_patch'
data/logdup.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'logdup/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "logdup"
8
+ spec.version = Logdup::VERSION
9
+ spec.authors = ["pinzolo"]
10
+ spec.email = ["pinzolo@gmail.com"]
11
+ spec.description = %q{Logdup duplicates logs partially.}
12
+ spec.summary = spec.description
13
+ spec.homepage = "https://github.com/pinzolo/logdup"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "coveralls"
25
+ end
data/spec/log/.gitkeep ADDED
File without changes
@@ -0,0 +1,164 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ def log_dir
5
+ File.expand_path('../log', __FILE__)
6
+ end
7
+
8
+ def log_file(file_name)
9
+ "#{log_dir}/#{file_name}.log"
10
+ end
11
+
12
+ def syms
13
+ %w(debug info warn error fatal)
14
+ end
15
+
16
+ def include_message?(lines, message)
17
+ lines.any? { |line| line.include?(message) }
18
+ end
19
+
20
+ describe Logger do
21
+ before(:all) do
22
+ log_files = Dir.entries(log_dir).select { |e| e.end_with?(".log") }
23
+ log_files.each { |log_file| File.delete("#{log_dir}/#{log_file}") }
24
+ end
25
+ describe "#dup_to" do
26
+ context "on single using" do
27
+ before(:all) do
28
+ logger = Logger.new(log_file("base-on-single"))
29
+ syms.each do |sym|
30
+ logger.send(sym, "#{sym}-01")
31
+ end
32
+ logger.dup_to(log_file("single")) do
33
+ syms.each do |sym|
34
+ logger.send(sym, "#{sym}-02")
35
+ end
36
+ end
37
+ syms.each do |sym|
38
+ logger.send(sym, "#{sym}-03")
39
+ end
40
+ end
41
+ it "logged all in base-on-single.log" do
42
+ lines = File.readlines(log_file("base-on-single"))
43
+ syms.each do |sym|
44
+ 1.upto(3) do |i|
45
+ expect(include_message?(lines, "#{sym}-0#{i}")).to be_true
46
+ end
47
+ end
48
+ end
49
+ it "logged partially in single.log" do
50
+ lines = File.readlines(log_file("single"))
51
+ syms.each do |sym|
52
+ expect(include_message?(lines, "#{sym}-01")).to be_false
53
+ expect(include_message?(lines, "#{sym}-02")).to be_true
54
+ expect(include_message?(lines, "#{sym}-03")).to be_false
55
+ end
56
+ end
57
+ end
58
+ context "on nested using" do
59
+ before(:all) do
60
+ logger = Logger.new(log_file("base-on-nested"))
61
+ syms.each do |sym|
62
+ logger.send(sym, "#{sym}-01")
63
+ end
64
+ logger.dup_to(log_file("nested-outer")) do
65
+ syms.each do |sym|
66
+ logger.send(sym, "#{sym}-02")
67
+ end
68
+ logger.dup_to(log_file("nested-inner")) do
69
+ syms.each do |sym|
70
+ logger.send(sym, "#{sym}-03")
71
+ end
72
+ end
73
+ syms.each do |sym|
74
+ logger.send(sym, "#{sym}-04")
75
+ end
76
+ end
77
+ syms.each do |sym|
78
+ logger.send(sym, "#{sym}-05")
79
+ end
80
+ end
81
+ it "logged all in base-on-nested.log" do
82
+ lines = File.readlines(log_file("base-on-nested"))
83
+ syms.each do |sym|
84
+ 1.upto(5) do |i|
85
+ expect(include_message?(lines, "#{sym}-0#{i}")).to be_true
86
+ end
87
+ end
88
+ end
89
+ it "logged partially in nested-outer.log" do
90
+ lines = File.readlines(log_file("nested-outer"))
91
+ syms.each do |sym|
92
+ expect(include_message?(lines, "#{sym}-01")).to be_false
93
+ expect(include_message?(lines, "#{sym}-02")).to be_true
94
+ expect(include_message?(lines, "#{sym}-03")).to be_true
95
+ expect(include_message?(lines, "#{sym}-04")).to be_true
96
+ expect(include_message?(lines, "#{sym}-05")).to be_false
97
+ end
98
+ end
99
+ it "logged partially in nested-inner.log" do
100
+ lines = File.readlines(log_file("nested-inner"))
101
+ syms.each do |sym|
102
+ expect(include_message?(lines, "#{sym}-01")).to be_false
103
+ expect(include_message?(lines, "#{sym}-02")).to be_false
104
+ expect(include_message?(lines, "#{sym}-03")).to be_true
105
+ expect(include_message?(lines, "#{sym}-04")).to be_false
106
+ expect(include_message?(lines, "#{sym}-05")).to be_false
107
+ end
108
+ end
109
+ end
110
+ context "on using in other thread" do
111
+ before(:all) do
112
+ logger = Logger.new(log_file("base-on-thread"))
113
+ logger.info("info-01")
114
+ Thread.new do
115
+ logger.info("info-02")
116
+ end
117
+ logger.dup_to(log_file("thread")) do
118
+ logger.info("info-03")
119
+ Thread.new do
120
+ logger.info("info-04")
121
+ end
122
+ end
123
+ logger.info("info-05")
124
+ end
125
+ it "logged all in base-on-thread.log" do
126
+ lines = File.readlines(log_file("base-on-thread"))
127
+ 1.upto(5) do |i|
128
+ expect(include_message?(lines, "info-0#{i}")).to be_true
129
+ end
130
+ end
131
+ it "logged at same thread in thread.log" do
132
+ lines = File.readlines(log_file("thread"))
133
+ syms.each do |sym|
134
+ expect(include_message?(lines, "info-01")).to be_false
135
+ expect(include_message?(lines, "info-02")).to be_false
136
+ expect(include_message?(lines, "info-03")).to be_true
137
+ expect(include_message?(lines, "info-04")).to be_false
138
+ expect(include_message?(lines, "info-05")).to be_false
139
+ end
140
+ end
141
+ end
142
+ context "with buffer_size option" do
143
+ it "output log if size is over buffer" do
144
+ logger = Logger.new(log_file("base-on-buffer"))
145
+ logger.dup_to(log_file("buffer"), buffer_size: 2) do
146
+ logger.info("info-01")
147
+ logger.info("info-02")
148
+ expect(File.exists?(log_file("buffer"))).to be_false
149
+ logger.info("info-03")
150
+ expect(File.exists?(log_file("buffer"))).to be_true
151
+ lines = File.readlines(log_file("buffer"))
152
+ expect(include_message?(lines, "info-01")).to be_true
153
+ expect(include_message?(lines, "info-02")).to be_false
154
+ expect(include_message?(lines, "info-03")).to be_false
155
+ end
156
+ lines = File.readlines(log_file("buffer"))
157
+ expect(include_message?(lines, "info-01")).to be_true
158
+ expect(include_message?(lines, "info-02")).to be_true
159
+ expect(include_message?(lines, "info-03")).to be_true
160
+ end
161
+ end
162
+ end
163
+ end
164
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # Require this file using `require "spec_helper"` to ensure that it is only
5
+ # loaded once.
6
+ #
7
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
8
+ require "coveralls"
9
+ require "simplecov"
10
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
11
+ SimpleCov.start do
12
+ add_filter '/spec/'
13
+ add_filter '/bundle/'
14
+ end
15
+ require 'logdup'
16
+
17
+ RSpec.configure do |config|
18
+ config.treat_symbols_as_metadata_keys_with_true_values = true
19
+ config.run_all_when_everything_filtered = true
20
+ config.filter_run :focus
21
+
22
+ # Run specs in random order to surface order dependencies. If you find an
23
+ # order dependency and want to debug it, you can fix the order by providing
24
+ # the seed, which is printed after each run.
25
+ # --seed 1234
26
+ config.order = 'random'
27
+ end
28
+
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logdup
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - pinzolo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-26 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: coveralls
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: Logdup duplicates logs partially.
70
+ email:
71
+ - pinzolo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .coveralls.yml
77
+ - .gitignore
78
+ - .rspec
79
+ - .travis.yml
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/logdup.rb
85
+ - lib/logdup/log_duplication.rb
86
+ - lib/logdup/logger_patch.rb
87
+ - lib/logdup/version.rb
88
+ - logdup.gemspec
89
+ - spec/log/.gitkeep
90
+ - spec/logdup_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: https://github.com/pinzolo/logdup
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.14
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Logdup duplicates logs partially.
116
+ test_files:
117
+ - spec/log/.gitkeep
118
+ - spec/logdup_spec.rb
119
+ - spec/spec_helper.rb