spawning-logger 0.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: a1c55f2d38055862dc3b2b585be3e1fc1f6e56a5
4
+ data.tar.gz: 12c9036eb7d86661e95db59efaf8fd69b92f4fd2
5
+ SHA512:
6
+ metadata.gz: caf4a49ed5b233f8d8d8c38407c603008d8d874872402d96540ac0ce85c78d271ba403978839db43130b34be42e44e8ad4929e192a98899da12a6b224e20f4f3
7
+ data.tar.gz: 39aa7f7765c05342b43b6afa53f4618dabb0051cc7428a8cbc5eee2baf7b2f89b3b8abb4a6ab3ee53b1bda93de26588da067241b7277a254972e5b08e20c05c3
data/.gitignore ADDED
@@ -0,0 +1,40 @@
1
+ # SimpleCov
2
+ test/coverage
3
+
4
+ # Bundler
5
+ Gemfile.lock
6
+
7
+ *.gem
8
+ *.rbc
9
+ /.config
10
+ /coverage/
11
+ /InstalledFiles
12
+ /pkg/
13
+ /spec/reports/
14
+ /test/tmp/
15
+ /test/version_tmp/
16
+ /tmp/
17
+
18
+ ## Specific to RubyMotion:
19
+ .dat*
20
+ .repl_history
21
+ build/
22
+
23
+ ## Documentation cache and generated files:
24
+ /.yardoc/
25
+ /_yardoc/
26
+ /doc/
27
+ /rdoc/
28
+
29
+ ## Environment normalisation:
30
+ /.bundle/
31
+ /lib/bundler/man/
32
+
33
+ # for a library or gem, you might want to ignore these files since the code is
34
+ # intended to run in multiple environments; otherwise, check them in:
35
+ # Gemfile.lock
36
+ # .ruby-version
37
+ # .ruby-gemset
38
+
39
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
40
+ .rvmrc
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/.simplecov ADDED
@@ -0,0 +1,13 @@
1
+ SimpleCov.start do
2
+ coverage_dir "test/coverage"
3
+ command_name "tests"
4
+
5
+ use_merging true
6
+ merge_timeout 3600
7
+
8
+ add_filter "test/"
9
+
10
+ add_group "All", "/lib/*.rb"
11
+
12
+ formatter SimpleCov::Formatter::HTMLFormatter
13
+ end
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,13 @@
1
+ guard :minitest do
2
+
3
+ # run all tests when lib main module file changes
4
+ watch(%r{^lib/configuration.rb$}) { "test" }
5
+
6
+ # run accompanying test for single source file if it changes
7
+ watch(%r{^lib/(.*)\.rb$}) {|m| "test/#{m[1]}_test.rb" }
8
+
9
+ # run test whenever it changes
10
+ watch(%r{^test/(.*)\/?(.*)?_test\.rb$})
11
+
12
+ end
13
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Markus Seeger
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ spawning_logger
2
+ ===============
3
+
4
+ A logger that is able to spawn sub-loggers
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+
7
+ task :default => [:test]
8
+
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = false
14
+ end
@@ -0,0 +1,128 @@
1
+ # A Logger that is able to spawn sub-loggers.
2
+ #
3
+ # Sub-loggers are created with a different filename.
4
+ # Also supports a preset logfile subdirectory in case you create many subloggers.
5
+ #
6
+ # Only the constructor is modified and the spawning factory method added,
7
+ # everything else is delegated to the ruby stdlib ::Logger class.
8
+ #
9
+ # ## Examples:
10
+ #
11
+ # ### 1) usage same as ::Logger
12
+ #
13
+ # ```ruby
14
+ # logger = SpawningLogger.new('server.log')
15
+ #
16
+ # # => creates ./server.log
17
+ # ```
18
+ #
19
+ # ### 2) spawn child logger
20
+ #
21
+ # ```ruby
22
+ # logger = SpawningLogger.new('server.log')
23
+ # child_logger = logger.spawn('worker1')
24
+ #
25
+ # # => creates ./server_worker1.log
26
+ # # => returns a ::Logger instance logging into this file
27
+ # ```
28
+ #
29
+ # ### 3) spawn child logger with prefix
30
+ #
31
+ # ```ruby
32
+ # SpawningLogger.configure do |config|
33
+ # config.child_prefix = 'worker'
34
+ # end
35
+ #
36
+ # logger = SpawningLogger.new('server.log')
37
+ # child_logger = logger.spawn('1')
38
+ #
39
+ # # => creates ./server_worker_1.log
40
+ # # => returns a ::Logger instance logging into this file
41
+ # ```
42
+ #
43
+ # ### 4) create all logfiles inside subdir, injected into original path
44
+ #
45
+ # ```ruby
46
+ # SpawningLogger.configure do |config|
47
+ # config.subdir = 'production'
48
+ # end
49
+ #
50
+ # logger = SpawningLogger.new('log/server.log')
51
+ # child_logger = logger.spawn('1')
52
+ #
53
+ # # => creates ./log/production/server.log
54
+ # # => creates ./log/production/server_worker_1.log
55
+ # ```
56
+
57
+ require 'logger'
58
+
59
+ class SpawningLogger < ::Logger
60
+
61
+ class ArgumentError < ::ArgumentError; end
62
+
63
+ # cattr_accessor :child_prefix
64
+ # cattr_accessor :subdir
65
+
66
+ @@child_prefix = nil
67
+ @@subdir = nil
68
+
69
+ def self.configure
70
+ yield self
71
+ end
72
+
73
+ def self.child_prefix=(value)
74
+ @@child_prefix = value
75
+ end
76
+
77
+ def self.subdir=(value)
78
+ @@subdir = value
79
+ end
80
+
81
+ # creates the logfile inside a subdir (optional).
82
+ def initialize(file_path, subdir = nil)
83
+ file_path = File.expand_path(file_path)
84
+
85
+ @log_dir = File.dirname(file_path)
86
+ @log_dir = File.join(@log_dir, @@subdir) unless @@subdir.nil?
87
+ FileUtils.mkdir_p(@log_dir) if !Dir.exist?(@log_dir)
88
+
89
+ @file_name = File.basename(file_path)
90
+ @child_loggers = {} # these are the special sub-loggers
91
+
92
+ super(File.join(@log_dir, @file_name)) # this creates the main logger
93
+ end
94
+
95
+ # creates a sub logger with filename <orig_file>_<child_prefix>_<child_name>.log
96
+ # example: see class docstring or README.md
97
+ def spawn(child_name)
98
+ raise ArgumentError.new("empty child_name") if child_name.to_s.empty?
99
+
100
+ @child_loggers[child_name] ||= create_child_logger(child_name)
101
+ @child_loggers[child_name]
102
+ end
103
+
104
+ protected
105
+
106
+ # creates a logger for child_name. uses child_name and
107
+ # child_prefix (if configured) for construction of the new logger's filename.
108
+ #
109
+ # example:
110
+ # origfile.log => origfile_childprefix_childname.log
111
+ #
112
+ def create_child_logger(child_name)
113
+ # remove extension
114
+ parent_filename = File.basename(@file_name, File.extname(@file_name))
115
+
116
+ # add child_prefix + child_id
117
+ file_basename = [
118
+ parent_filename, @@child_prefix, child_name
119
+ ].compact.join('_')
120
+
121
+ # add extension
122
+ file_name = file_basename + File.extname(@file_name)
123
+
124
+ file_path = File.join(@log_dir, file_name)
125
+ ::Logger.new(file_path)
126
+ end
127
+
128
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'logger'
2
+
3
+ class SpawningLogger < ::Logger
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "spawning-logger"
8
+ gem.version = SpawningLogger::VERSION
9
+ gem.authors = ["Markus Seeger"]
10
+ gem.email = ["mail@codegourmet.de"]
11
+ gem.description = "A logger that can spawn other loggers"
12
+ gem.summary = ""
13
+ gem.homepage = "https://github.com/codegourmet/spawning_logger"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency "rake", "~> 10.1"
22
+ gem.add_development_dependency "guard-minitest"
23
+ gem.add_development_dependency "minitest"
24
+ gem.add_development_dependency "minitest-reporters"
25
+ gem.add_development_dependency "simplecov"
26
+ end
@@ -0,0 +1,81 @@
1
+ require_relative "./test_helper"
2
+ require_relative "../lib/spawning_logger"
3
+
4
+ class SpawningLoggerTest < MiniTest::Test
5
+
6
+ def setup
7
+ @log_dir = Dir.mktmpdir('spawning_logger_test')
8
+ @logfile_name = 'test_file.log'
9
+ @logfile_path = File.join(@log_dir, @logfile_name)
10
+ @child_id = 'childid'
11
+
12
+ reset_logger_config
13
+ end
14
+
15
+ def teardown
16
+ FileUtils.remove_entry(@log_dir)
17
+ reset_logger_config
18
+ end
19
+
20
+ def test_creates_subdir_if_doesnt_exist
21
+ subdir = 'development'
22
+
23
+ SpawningLogger.configure do |config|
24
+ config.subdir = subdir
25
+ end
26
+ SpawningLogger.new(@logfile_path)
27
+
28
+ expected_dir = File.join(@log_dir, subdir)
29
+ expected_file = File.join(expected_dir, @logfile_name)
30
+
31
+ assert File.exist?(expected_file)
32
+ assert Dir.exist?(expected_dir)
33
+ end
34
+
35
+ def test_spawn_interface_yields_logger_for_child_id
36
+ assert_creates_log_file('test_file_childid.log')
37
+ end
38
+
39
+ def test_child_logger_filename_includes_child_prefix
40
+ SpawningLogger.configure do |config|
41
+ config.child_prefix = 'childprefix'
42
+ end
43
+
44
+ assert_creates_log_file('test_file_childprefix_childid.log')
45
+ end
46
+
47
+ def test_raises_if_child_id_is_nil_or_empty
48
+ logger = SpawningLogger.new(@logfile_path)
49
+
50
+ assert_raises SpawningLogger::ArgumentError do
51
+ logger.spawn(nil)
52
+ end
53
+ end
54
+
55
+ def test_creates_child_logger_only_once
56
+ skip "NYI"
57
+ # TODO
58
+ # expect ::Logger.new(child_path) to be called only once
59
+ # spawn child logger 2x
60
+ end
61
+
62
+ protected
63
+
64
+ def assert_creates_log_file(file_name)
65
+ logger = SpawningLogger.new(@logfile_path)
66
+ child_logger = logger.spawn(@child_id)
67
+
68
+ assert_kind_of(::Logger, child_logger)
69
+
70
+ expected_path = File.join(@log_dir, file_name)
71
+ assert File.exist?(expected_path)
72
+ end
73
+
74
+ def reset_logger_config
75
+ SpawningLogger.configure do |config|
76
+ config.child_prefix = nil
77
+ config.subdir = nil
78
+ end
79
+ end
80
+
81
+ end
@@ -0,0 +1,12 @@
1
+ require 'simplecov' # has to be at top, will execute ../.simplecov
2
+
3
+ require 'spawning_logger'
4
+
5
+ require 'minitest/autorun'
6
+ require 'minitest/reporters'
7
+
8
+ if ENV["RM_INFO"] || ENV["TEAMCITY_VERSION"]
9
+ MiniTest::Reporters.use! MiniTest::Reporters::RubyMineReporter.new
10
+ else
11
+ MiniTest::Reporters.use! MiniTest::Reporters::SpecReporter.new
12
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spawning-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Markus Seeger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '10.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '10.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard-minitest
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: minitest
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: minitest-reporters
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
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A logger that can spawn other loggers
84
+ email:
85
+ - mail@codegourmet.de
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .ruby-version
92
+ - .simplecov
93
+ - Gemfile
94
+ - Guardfile
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - lib/spawning_logger.rb
99
+ - lib/version.rb
100
+ - spawning_logger.gemspec
101
+ - test/spawning_logger_test.rb
102
+ - test/test_helper.rb
103
+ homepage: https://github.com/codegourmet/spawning_logger
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.0.14
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: ''
127
+ test_files:
128
+ - test/spawning_logger_test.rb
129
+ - test/test_helper.rb