log_switch 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
@@ -0,0 +1 @@
1
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+ gemspec
3
+
data/History.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ === 0.1.0 / 2011-10-07
2
+
3
+ * Features:
4
+ * +require+ and +extend+ to mix in to your class/module to get a single point of logging
5
+ * Switch on/off logging
6
+ * Use whatever Logger you want
7
+
data/README.rdoc ADDED
@@ -0,0 +1,87 @@
1
+ = log_switch
2
+
3
+ http://github.com/turboladen/log_switch
4
+
5
+ == DESCRIPTION
6
+
7
+ While developing other gems that required a single class/singleton style logger,
8
+ I got tired of repeating the code to create that logger and mix it in to my base
9
+ class. I just wanted to be able to require something, then be able to do:
10
+
11
+ MyLib.log "some message"
12
+
13
+ I also wanted to be able to programmatically turn on/off logging by doing something
14
+ like:
15
+
16
+ MyLib.log = false
17
+
18
+ This gem allows just that. Well, almost...
19
+
20
+ == FEATURES/PROBLEMS
21
+
22
+ Features:
23
+
24
+ * +require+ and +extend+ to mix in to your class/module to get a single point of logging
25
+ * Switch on/off logging
26
+ * Use whatever Logger you want
27
+
28
+ == SYNOPSIS
29
+
30
+ Get your app logging with a single point of logging:
31
+
32
+ require 'log_switch'
33
+
34
+ class MyThing
35
+ extend LogSwitch
36
+ end
37
+
38
+ MyThing.log "I like you, Ruby." # => D, [2011-10-07T14:40:26.697084 #30080] DEBUG -- : I like you, Ruby.
39
+
40
+ ...and then you can simply switch off logging by doing:
41
+
42
+ MyThing.log = false
43
+ MyThing.log "You're my favorite." # => No logging occurs!
44
+
45
+ By default, LogSwitch sets the log level to :debug. You can change the default
46
+ log level as you go:
47
+
48
+ MyThing.log_level = :warn
49
+ MyThing.log "Crap!" # => W, [2011-10-07T15:30:54.012502 #32892] WARN -- : Crap!
50
+
51
+ You can pass in the log level for your Logger type too:
52
+
53
+ MyThing.log "Stuff!", :info # => I, [2011-10-07T15:28:49.480741 #32892] INFO -- : Stuff!
54
+ MyThing.log "Meow", :fatal # => F, [2011-10-07T15:32:21.207867 #32892] FATAL -- : Meow
55
+
56
+ If you have another Logger object you want to write to, no problem:
57
+
58
+ some_other_logger = Logger.new 'log.txt'
59
+ MyThing.logger = some_other_logger
60
+ MyThing.log "hi!"
61
+ File.open('log.txt', 'r').read # => Logfile created on 2011-10-07 15:50:19 -0700 by logger.rb/25413
62
+ # D, [2011-10-07T15:51:16.385798 #34026] DEBUG -- : hi!
63
+
64
+ == REQUIREMENTS
65
+
66
+ * Ruby 1.9.2
67
+ * RubyGems:
68
+ * None!
69
+
70
+ == INSTALL
71
+
72
+ $ gem install log_switch
73
+
74
+ == DEVELOPERS
75
+
76
+ After checking out the source, run:
77
+
78
+ $ bundle install
79
+
80
+ This task will install any missing dependencies for you.
81
+
82
+ == THANKS
83
+
84
+ I need to thank the http://github.com/rubiii/savon project for most of the code
85
+ here. Somehow I ran across how they do logging and started following suit. The
86
+ code in +log_switch+ is almost identical to Savon's logging.
87
+
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'yard'
4
+
5
+ # Load all extra rake task definitions
6
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].each { |ext| load ext }
7
+
8
+ YARD::Rake::YardocTask.new do |t|
9
+ t.files = %w(lib/**/*.rb - History.rdoc)
10
+ t.options = %w(--title log_switch Documentation (#{LogSwitch::VERSION}))
11
+ t.options += %w(--main README.rdoc)
12
+ end
13
+
14
+ RSpec::Core::RakeTask.new
15
+
16
+ # Alias for rubygems-test
17
+ task test: :spec
18
+
19
+ task default: :install
20
+
@@ -0,0 +1,3 @@
1
+ module LogSwitch
2
+ VERSION = '0.1.0'
3
+ end
data/lib/log_switch.rb ADDED
@@ -0,0 +1,48 @@
1
+ require_relative 'log_switch/version'
2
+ require "logger"
3
+
4
+ # LogSwitch allows for extending a class/module with a logger and, most
5
+ # importantly, allows for turning off logging programmatically. See the
6
+ # +README.rdoc+ for more info.
7
+ module LogSwitch
8
+
9
+ # Use to turn logging on or off.
10
+ attr_writer :log
11
+
12
+ # Tells whether logging is turned on or not.
13
+ def log?
14
+ @log != false
15
+ end
16
+
17
+ # Set this to the Logger you want to use.
18
+ attr_writer :logger
19
+
20
+ # Defaults to a +Logger+ writing to STDOUT.
21
+ def logger
22
+ @logger ||= ::Logger.new STDOUT
23
+ end
24
+
25
+ # Set the log level so you don't have to pass it in on your call.
26
+ attr_writer :log_level
27
+
28
+ # @return [Symbol] The current default log level. Starts off as :debug.
29
+ def log_level
30
+ @log_level ||= :debug
31
+ end
32
+
33
+ # Logs a message using the level provided. If no level provided, use
34
+ # +@log_level+.
35
+ #
36
+ # @param [String] message The message to log.
37
+ # @param [Symbol] level The log level to send to your Logger.
38
+ def log(message, level=log_level)
39
+ message.each_line { |line| logger.send level, line.chomp if log? }
40
+ end
41
+
42
+ # Sets back to defaults.
43
+ def reset_config!
44
+ self.log = true
45
+ self.logger = ::Logger.new STDOUT
46
+ self.log_level = :debug
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require 'log_switch/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "log_switch"
6
+ s.version = LogSwitch::VERSION
7
+ s.authors = %w("Steve Loveless")
8
+ s.homepage = %q(http://github.com/turboladen/log_switch)
9
+ s.email = %w(steve.loveless@gmail.com)
10
+ s.summary = %q(Extends a class for singleton style logging that can easily be turned on and off.)
11
+ s.description = %q(Extends a class for singleton style logging that can easily be turned on and off.)
12
+
13
+ s.required_rubygems_version = ">=1.8.0"
14
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9.2")
15
+ s.files = Dir.glob("{bin,features,lib,spec,tasks}/**/*") + Dir.glob("*.rdoc") +
16
+ %w(.gemtest Gemfile log_switch.gemspec Rakefile)
17
+ s.test_files = Dir.glob("{features,spec}/**/*")
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency("bundler", [">= 0"])
21
+ s.add_development_dependency("code_statistics", ["~> 0.2.13"])
22
+ s.add_development_dependency("metric_fu", [">= 2.0.1"])
23
+ s.add_development_dependency("rake", [">= 0"])
24
+ s.add_development_dependency("rspec", ["~> 2.6.0"])
25
+ s.add_development_dependency("simplecov", [">= 0"])
26
+ s.add_development_dependency("simplecov-rcov-text", [">= 0"])
27
+ s.add_development_dependency("yard", [">= 0.7.2"])
28
+ end
29
+
@@ -0,0 +1,82 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Kernel do
4
+ def self.get_requires
5
+ Dir[File.dirname(__FILE__) + '/../lib/log_switch/**/*.rb']
6
+ end
7
+
8
+ # Try to require each of the files in LogSwitch
9
+ get_requires.each do |r|
10
+ it "should require #{r}" do
11
+
12
+ # A require returns true if it was required, false if it had already been
13
+ # required, and nil if it couldn't require.
14
+ Kernel.require(r.to_s).should_not be_nil
15
+ end
16
+ end
17
+ end
18
+
19
+ describe "LogSwitch" do
20
+ around :each do |example|
21
+ class MyClass; extend LogSwitch; end;
22
+ example.run
23
+ MyClass.reset_config!
24
+ end
25
+
26
+ it { LogSwitch::VERSION.should == '0.1.0' }
27
+
28
+ describe "log" do
29
+ it "should default to true" do
30
+ MyClass.log?.should be_true
31
+ end
32
+
33
+ it "can log at any log level" do
34
+ logger = double "Logger"
35
+ logger.should_receive(:send).with(:meow, "stuff")
36
+ MyClass.logger = logger
37
+ MyClass.log("stuff", :meow)
38
+ end
39
+
40
+ it "raises when log_level isn't a Symbol" do
41
+ expect { MyClass.log("stuff", "meow") }.to raise_error NoMethodError
42
+ end
43
+ end
44
+
45
+ describe "log=" do
46
+ it "allows to set logging to false" do
47
+ MyClass.log = false
48
+ MyClass.log?.should be_false
49
+ end
50
+ end
51
+
52
+ describe "logger" do
53
+ it "is a Logger by default" do
54
+ MyClass.logger.should be_a Logger
55
+ end
56
+ end
57
+
58
+ describe "logger=" do
59
+ it "allows to set to use another logger" do
60
+ original_logger = MyClass.logger
61
+ another_logger = Logger.new nil
62
+ MyClass.logger = another_logger
63
+ MyClass.logger.should_not == original_logger
64
+ end
65
+ end
66
+
67
+ describe "log_level" do
68
+ it "defaults to :debug" do
69
+ MyClass.log_level.should == :debug
70
+ end
71
+ end
72
+
73
+ describe "log_level=" do
74
+ it "changes the level that #log(msg, level=) uses" do
75
+ MyClass.logger.should_receive(:debug)
76
+ MyClass.log("testing...")
77
+ MyClass.log_level = :info
78
+ MyClass.logger.should_receive(:info)
79
+ MyClass.log("testing...")
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,18 @@
1
+ require 'simplecov'
2
+ require 'simplecov-rcov-text'
3
+
4
+ class SimpleCov::Formatter::MergedFormatter
5
+ def format(result)
6
+ SimpleCov::Formatter::HTMLFormatter.new.format(result)
7
+ SimpleCov::Formatter::RcovTextFormatter.new.format(result)
8
+ end
9
+ end
10
+
11
+ SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
12
+
13
+ SimpleCov.start do
14
+ add_filter "/spec"
15
+ end
16
+
17
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
18
+ require 'log_switch'
@@ -0,0 +1,59 @@
1
+ require 'metric_fu'
2
+
3
+ MetricFu::Configuration.run do |config|
4
+ config.metrics = [
5
+ :churn,
6
+ :flog,
7
+ :flay,
8
+ :hotspots,
9
+ :rcov,
10
+ :reek,
11
+ :roodi,
12
+ :stats,
13
+ ]
14
+ config.graphs = [
15
+ :flog,
16
+ :flay,
17
+ :rcov,
18
+ :reek,
19
+ :roodi,
20
+ :stats
21
+ ]
22
+ config.churn = {
23
+ start_date: "1 year ago",
24
+ minimum_churn_count: 10
25
+ }
26
+ config.flay = {
27
+ dirs_to_flay: %w(
28
+ lib
29
+ features/step_definitions
30
+ features/support
31
+ ),
32
+ minimum_score: 10,
33
+ filetypes: %w(rb)
34
+ }
35
+ config.flog = {
36
+ dirs_to_flog: %w(
37
+ lib
38
+ features/step_definitions
39
+ features/support
40
+ )
41
+ }
42
+ config.reek = {
43
+ dirs_to_reek: %w(
44
+ lib
45
+ features/step_definitions
46
+ features/support
47
+ )
48
+ }
49
+ config.roodi = {
50
+ dirs_to_roodi: %w(
51
+ lib
52
+ features/step_definitions
53
+ features/support
54
+ ),
55
+ roodi_config: "tasks/roodi_config.yaml"
56
+ }
57
+ config.rcov[:external] = 'coverage/rcov/rcov.txt'
58
+ config.graph_engine = :bluff
59
+ end
@@ -0,0 +1,14 @@
1
+ ---
2
+ AssignmentInConditionalCheck: { }
3
+ CaseMissingElseCheck: { }
4
+ ClassLineCountCheck: { line_count: 300 }
5
+ ClassNameCheck: { pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/ }
6
+ CyclomaticComplexityBlockCheck: { complexity: 4 }
7
+ CyclomaticComplexityMethodCheck: { complexity: 8 }
8
+ EmptyRescueBodyCheck: { }
9
+ ForLoopCheck: { }
10
+ MethodLineCountCheck: { line_count: 30 }
11
+ MethodNameCheck: { pattern: !ruby/regexp /^[_a-z<>=\[\]|+-\/\*`]+[_a-z0-9_<>=~@\[\]]*[=!\?]?$/ }
12
+ ModuleLineCountCheck: { line_count: 300 }
13
+ ModuleNameCheck: { pattern: !ruby/regexp /^[A-Z][a-zA-Z0-9]*$/ }
14
+ ParameterNumberCheck: { parameter_count: 5 }
data/tasks/stats.rake ADDED
@@ -0,0 +1,12 @@
1
+ require 'code_statistics'
2
+
3
+ STATS_DIRECTORIES = [
4
+ %w(Library lib),
5
+ %w(Behavior\ tests features/step_definitions),
6
+ %w(Unit\ tests spec)
7
+ ].collect { |name, dir| [ name, "#{dir}" ] }.select { |name, dir| File.directory?(dir) }
8
+
9
+ desc "Report code statistics (KLOCs, etc) from the application"
10
+ task :stats do
11
+ CodeStatistics::CodeStatistics.new(*STATS_DIRECTORIES).to_s
12
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log_switch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ! '"Steve'
9
+ - Loveless"
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-10-08 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: &70101806784680 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *70101806784680
26
+ - !ruby/object:Gem::Dependency
27
+ name: code_statistics
28
+ requirement: &70101806783740 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.13
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70101806783740
37
+ - !ruby/object:Gem::Dependency
38
+ name: metric_fu
39
+ requirement: &70101806782920 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: 2.0.1
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70101806782920
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: &70101806782420 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70101806782420
59
+ - !ruby/object:Gem::Dependency
60
+ name: rspec
61
+ requirement: &70101806781700 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 2.6.0
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70101806781700
70
+ - !ruby/object:Gem::Dependency
71
+ name: simplecov
72
+ requirement: &70101806780960 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *70101806780960
81
+ - !ruby/object:Gem::Dependency
82
+ name: simplecov-rcov-text
83
+ requirement: &70101806780160 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *70101806780160
92
+ - !ruby/object:Gem::Dependency
93
+ name: yard
94
+ requirement: &70101806778460 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: 0.7.2
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *70101806778460
103
+ description: Extends a class for singleton style logging that can easily be turned
104
+ on and off.
105
+ email:
106
+ - steve.loveless@gmail.com
107
+ executables: []
108
+ extensions: []
109
+ extra_rdoc_files: []
110
+ files:
111
+ - lib/log_switch/version.rb
112
+ - lib/log_switch.rb
113
+ - spec/log_switch_spec.rb
114
+ - spec/spec_helper.rb
115
+ - tasks/metrics.rake
116
+ - tasks/roodi_config.yaml
117
+ - tasks/stats.rake
118
+ - History.rdoc
119
+ - README.rdoc
120
+ - .gemtest
121
+ - Gemfile
122
+ - log_switch.gemspec
123
+ - Rakefile
124
+ homepage: http://github.com/turboladen/log_switch
125
+ licenses: []
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: 1.9.2
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: 1.8.0
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 1.8.11
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Extends a class for singleton style logging that can easily be turned on
148
+ and off.
149
+ test_files:
150
+ - spec/log_switch_spec.rb
151
+ - spec/spec_helper.rb