yell 0.13.4 → 1.0.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.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  **Yell - Your Extensible Logging Library**
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/rudionrails/yell.png?branch=master)](http://travis-ci.org/rudionrails/yell)
3
+ [![Build Status](https://secure.travis-ci.org/rudionrails/yell.png?branch=master)](http://travis-ci.org/rudionrails/yell)
4
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/rudionrails/yell)
4
5
 
5
6
  Yell works and is tested with ruby 1.8.7, 1.9.x, jruby 1.8 and 1.9 mode, rubinius 1.8 and 1.9 as well as ree.
6
7
 
@@ -22,3 +22,4 @@ EOS
22
22
  puts "=== actual example ==="
23
23
  logger = Yell.new STDOUT
24
24
  logger.info "Hello World!"
25
+
@@ -4,7 +4,7 @@ require_relative '../lib/yell'
4
4
 
5
5
  puts <<-EOS
6
6
 
7
- The extended formatting string looks like: %d [%5L] %p %h : %m.
7
+ # The extended formatting string looks like: %d [%5L] %p %h : %m.
8
8
 
9
9
  logger = Yell.new STDOUT, :format => "[%f:%n in `%M'] %m"
10
10
  logger.info "Hello World!"
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../lib/yell'
4
+
5
+ puts <<-EOS
6
+
7
+ # You can add a logger to the global repository.
8
+ #
9
+ # create a logger named 'mylog' that logs to stdout
10
+ Yell.new :stdout, :name => 'mylog'
11
+
12
+ # Later in the code, you can get your logger back
13
+ Yell['mylog'].info "Hello World!"
14
+
15
+
16
+ EOS
17
+
18
+ puts "=== actuale example ==="
19
+ Yell.new :stdout, :name => 'mylog'
20
+ Yell['mylog'].info "Hello World!"
21
+
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require_relative '../lib/yell'
4
+
5
+ puts <<-EOS
6
+
7
+ # You can add logging to any class by including the Yell::Loggable module.
8
+ #
9
+ # When including the module, your class will get a :logger method. Before you
10
+ # can use it, though, you will need to define a logger providing the :name of
11
+ # your class.
12
+
13
+ Yell.new :stdout, :name => 'Foo'
14
+
15
+ # Define the class
16
+ class Foo
17
+ include Yell::Loggable
18
+ end
19
+
20
+ foo = Foo.new
21
+ foo.logger.info "Hello World!"
22
+ #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 : Hello World!"
23
+
24
+
25
+ EOS
26
+
27
+ puts "=== actual example ==="
28
+
29
+ Yell.new :stdout, :name => 'Foo'
30
+
31
+ class Foo
32
+ include Yell::Loggable
33
+ end
34
+
35
+ foo = Foo.new
36
+ foo.logger.info "Hello World!"
37
+
data/lib/yell.rb CHANGED
@@ -57,13 +57,6 @@ module Yell #:nodoc:
57
57
  Yell::Repository[ name ] = logger
58
58
  end
59
59
 
60
- # Shortcut to Yell::Repository.loggers
61
- #
62
- # @return [Hash] The map of loggers
63
- def loggers
64
- Yell::Repository.loggers
65
- end
66
-
67
60
  # Shortcut to Yell::Fomatter.new
68
61
  #
69
62
  # @return [Yell::Formatter] A Yell::Formatter instance
@@ -107,6 +100,7 @@ module Yell #:nodoc:
107
100
 
108
101
  end
109
102
 
103
+ # classes
110
104
  require File.dirname(__FILE__) + '/yell/configuration'
111
105
  require File.dirname(__FILE__) + '/yell/repository'
112
106
  require File.dirname(__FILE__) + '/yell/event'
@@ -115,3 +109,6 @@ require File.dirname(__FILE__) + '/yell/formatter'
115
109
  require File.dirname(__FILE__) + '/yell/adapters'
116
110
  require File.dirname(__FILE__) + '/yell/logger'
117
111
 
112
+ # modules
113
+ require File.dirname(__FILE__) + '/yell/loggable'
114
+
data/lib/yell/adapters.rb CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  module Yell #:nodoc:
4
4
 
5
- # NoSuchAdapter is raised whenever you want to instantiate an
5
+ # AdapterNotFound is raised whenever you want to instantiate an
6
6
  # adapter that does not exist.
7
- class UnregisteredAdapter < StandardError; end
7
+ class AdapterNotFound < StandardError; end
8
8
 
9
9
  # This module provides the interface to attaching adapters to
10
10
  # the logger. You should not have to call the corresponding classes
@@ -35,7 +35,7 @@ module Yell #:nodoc:
35
35
  else @@adapters[name]
36
36
  end
37
37
 
38
- raise UnregisteredAdapter.new( name ) unless adapter
38
+ raise AdapterNotFound.new( name ) unless adapter
39
39
 
40
40
  adapter.new( options, &block )
41
41
  end
@@ -114,7 +114,8 @@ module Yell #:nodoc:
114
114
  end
115
115
 
116
116
  def symlink!
117
- return if ::File.symlink?(@original_filename) && ::File.readlink(@original_filename) == @filename # do nothing, because symlink is already correct
117
+ # do nothing, because symlink is already correct
118
+ return if ::File.symlink?(@original_filename) && ::File.readlink(@original_filename) == @filename
118
119
 
119
120
  ::File.unlink( @original_filename ) if ::File.exist?( @original_filename )
120
121
  ::File.symlink( @filename, @original_filename )
@@ -23,7 +23,6 @@ module Yell #:nodoc:
23
23
  @stream = ::File.open( @filename, ::File::WRONLY|::File::APPEND|::File::CREAT )
24
24
  @stream.sync = true
25
25
 
26
-
27
26
  @stream
28
27
  end
29
28
 
data/lib/yell/level.rb CHANGED
@@ -61,18 +61,7 @@ module Yell #:nodoc:
61
61
  #
62
62
  # @param [Integer,String,Symbol,Array,Range,nil] severity The severity for the level.
63
63
  def initialize( severity = nil )
64
- reset!
65
-
66
- case severity
67
- when Array then at( *severity )
68
- when Range then gte( severity.first ).lte( severity.last )
69
- when Integer, Symbol then gte( severity )
70
- when String then interpret!( severity )
71
- end
72
- end
73
-
74
- def reset!
75
- @severities = Yell::Severities.map { true }
64
+ reset!( severity )
76
65
  end
77
66
 
78
67
  # Returns whether the level is allowed at the given severity
@@ -80,46 +69,89 @@ module Yell #:nodoc:
80
69
  # @example
81
70
  # at? :warn
82
71
  # at? 0 # debug
72
+ #
73
+ # @return [Boolean] tru or false
83
74
  def at?( severity )
84
75
  index = index_from( severity )
85
76
 
86
77
  index.nil? ? false : @severities[index]
87
78
  end
88
79
 
89
- # to_i implements backwards compatibility
90
- def to_i
91
- @severities.each_with_index { |s,i| return i if s == true }
92
- end
93
- alias :to_int :to_i
94
-
95
- def at( *severities ) #:nodoc:
80
+ # Set the level at specific severities.
81
+ #
82
+ # @example Set at :debug and :error only
83
+ # at :debug, :error
84
+ #
85
+ # @return [Yell::Level] the instance
86
+ def at( *severities )
96
87
  severities.each { |severity| calculate! :==, severity }
97
88
  self
98
89
  end
99
90
 
100
- def gt( severity ) #:nodoc:
91
+ # Set the level to greater than the given severity
92
+ #
93
+ # @example Set to :error and above
94
+ # gt :warn
95
+ #
96
+ # @return [Yell::Level] the instance
97
+ def gt( severity )
101
98
  calculate! :>, severity
102
99
  self
103
100
  end
104
101
 
105
- def gte( severity ) #:nodoc:
102
+ # Set the level greater or equal to the given severity
103
+ #
104
+ # @example Set to :warn and above
105
+ # gte :warn
106
+ #
107
+ # @return [Yell::Level] the instance
108
+ def gte( severity )
106
109
  calculate! :>=, severity
107
110
  self
108
111
  end
109
112
 
110
- def lt( severity ) #:nodoc:
113
+ # Set the level lower than given severity
114
+ #
115
+ # @example Set to lower than :warn
116
+ # lt :warn
117
+ #
118
+ # @return [Yell::Level] the instance
119
+ def lt( severity )
111
120
  calculate! :<, severity
112
121
  self
113
122
  end
114
123
 
115
- def lte( severity ) #:nodoc:
124
+ # Set the level lower or equal than given severity
125
+ #
126
+ # @example Set to lower or equal than :warn
127
+ # lte :warn
128
+ #
129
+ # @return [Yell::Level] the instance
130
+ def lte( severity )
116
131
  calculate! :<=, severity
117
132
  self
118
133
  end
119
134
 
135
+ # to_i implements backwards compatibility
136
+ def to_i
137
+ @severities.each_with_index { |s,i| return i if s == true }
138
+ end
139
+ alias :to_int :to_i
140
+
120
141
 
121
142
  private
122
143
 
144
+ def reset!( severity )
145
+ @severities = Yell::Severities.map { true }
146
+
147
+ case severity
148
+ when Array then at( *severity )
149
+ when Range then gte( severity.first ).lte( severity.last )
150
+ when Integer, Symbol then gte( severity )
151
+ when String then interpret!( severity )
152
+ end
153
+ end
154
+
123
155
  def interpret!( severities )
124
156
  severities.split( ' ' ).each do |severity|
125
157
  if m = InterpretRegexp.match(severity)
@@ -128,7 +160,7 @@ module Yell #:nodoc:
128
160
  end
129
161
  end
130
162
 
131
- def calculate!( modifier, severity ) #:nodoc:
163
+ def calculate!( modifier, severity )
132
164
  index = index_from( severity )
133
165
  return if index.nil?
134
166
 
@@ -143,7 +175,7 @@ module Yell #:nodoc:
143
175
  taint unless tainted?
144
176
  end
145
177
 
146
- def index_from( severity ) #:nodoc:
178
+ def index_from( severity )
147
179
  case severity
148
180
  when Integer then severity
149
181
  when String, Symbol then Yell::Severities.index( severity.to_s.upcase )
@@ -151,28 +183,27 @@ module Yell #:nodoc:
151
183
  end
152
184
  end
153
185
 
154
- def ascending!( index ) #:nodoc:
155
- @severities.each_with_index do |s, i|
156
- next if s == false # skip
186
+ def ascending!( index )
187
+ each { |s, i| @severities[i] = i < index ? false : true }
188
+ end
157
189
 
158
- @severities[i] = i < index ? false : true
159
- end
190
+ def descending!( index )
191
+ each { |s, i| @severities[i] = index < i ? false : true }
160
192
  end
161
193
 
162
- def descending!( index ) #:nodoc:
194
+ def each
163
195
  @severities.each_with_index do |s, i|
164
196
  next if s == false # skip
165
197
 
166
- @severities[i] = index < i ? false : true
198
+ yield(s, i)
167
199
  end
168
200
  end
169
201
 
170
- def set!( index ) #:nodoc:
202
+ def set!( index, val = true )
171
203
  @severities.map! { false } unless tainted?
172
204
 
173
- @severities[index] = true
205
+ @severities[index] = val
174
206
  end
175
207
 
176
208
  end
177
-
178
209
  end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ module Yell #:nodoc:
4
+
5
+ # Include this module to add a logger to any class.
6
+ #
7
+ # When including this module, your class will have a :logger instance method
8
+ # available. Before you can use it, you will need to define a Yell logger and
9
+ # provide it with the name of your class.
10
+ #
11
+ # @example
12
+ # Yell.new :stdout, :name => 'Foo'
13
+ #
14
+ # class Foo
15
+ # include Yell::Loggable
16
+ # end
17
+ #
18
+ # Foo.new.logger.info "Hello World"
19
+ module Loggable
20
+
21
+ def logger
22
+ Yell[ self.class ]
23
+ end
24
+
25
+ end
26
+ end
27
+
data/lib/yell/logger.rb CHANGED
@@ -114,8 +114,6 @@ module Yell #:nodoc:
114
114
  end
115
115
 
116
116
  # Convenience method for resetting all adapters of the Logger.
117
- #
118
- # @param [Boolean] now Perform the reset immediately (default false)
119
117
  def close
120
118
  @adapters.each(&:close)
121
119
  end
@@ -133,7 +131,7 @@ module Yell #:nodoc:
133
131
  def #{name}?; @level.at?(#{index}); end # def info?; @level.at?(1); end
134
132
  #
135
133
  def #{name}( m = nil, o = {}, &b ) # def info( m = nil, o = {}, &b )
136
- return unless #{name}? # return unless info?
134
+ return false unless #{name}? # return false unless info?
137
135
  write Yell::Event.new(#{index}, m, o, &b) # write Yell::Event.new(1, m, o, &b)
138
136
  #
139
137
  true # true
@@ -4,6 +4,11 @@ require 'monitor'
4
4
  require "singleton"
5
5
 
6
6
  module Yell #:nodoc:
7
+
8
+ class LoggerNotFound < StandardError
9
+ def message; "Could not find a Yell::Logger instance with the name '#{super}'"; end
10
+ end
11
+
7
12
  class Repository
8
13
  extend MonitorMixin
9
14
  include Singleton
@@ -29,9 +34,18 @@ module Yell #:nodoc:
29
34
  # @example Get the logger
30
35
  # Yell::Repository[ 'development' ]
31
36
  #
37
+ # @raise [Yell::LoggerNotFound] Raised when repository does not have that key
32
38
  # @return [Yell::Logger] The logger instance
33
39
  def self.[]( name )
34
- synchronize { instance.loggers[name] }
40
+ synchronize do
41
+ logger = instance.loggers[name] || instance.loggers[name.to_s]
42
+
43
+ if logger.nil? && name.respond_to?(:superclass)
44
+ return Yell::Repository[ name.superclass ]
45
+ end
46
+
47
+ logger or raise Yell::LoggerNotFound.new(name)
48
+ end
35
49
  end
36
50
 
37
51
  # Get the list of all loggers in the repository
@@ -41,11 +55,6 @@ module Yell #:nodoc:
41
55
  synchronize { instance.loggers }
42
56
  end
43
57
 
44
- # Clears all logger instances (handy for testing)
45
- def self.clear
46
- synchronize { instance.loggers.clear }
47
- end
48
-
49
58
  end
50
59
  end
51
60
 
data/lib/yell/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Yell #:nodoc:
4
- VERSION = "0.13.4"
4
+ VERSION = "1.0.0"
5
5
 
6
6
  end
7
7
 
data/spec/spec_helper.rb CHANGED
@@ -13,7 +13,7 @@ RSpec.configure do |config|
13
13
  config.mock_framework = :rr
14
14
 
15
15
  config.before do
16
- Yell::Repository.clear
16
+ Yell::Repository.loggers.clear
17
17
  end
18
18
 
19
19
  config.after do
@@ -39,3 +39,4 @@ describe "running Yell multi-threaded" do
39
39
  end
40
40
 
41
41
  end
42
+
@@ -28,8 +28,8 @@ describe Yell::Adapters do
28
28
  end
29
29
 
30
30
  context "given an unregistered adapter" do
31
- it "should raise UnregisteredAdapter" do
32
- lambda { Yell::Adapters.new :unknown }.should raise_error Yell::UnregisteredAdapter
31
+ it "should raise AdapterNotFound" do
32
+ lambda { Yell::Adapters.new :unknown }.should raise_error Yell::AdapterNotFound
33
33
  end
34
34
  end
35
35
  end
@@ -72,41 +72,61 @@ describe Yell::Level do
72
72
  context "basic string" do
73
73
  subject { 'error' }
74
74
 
75
- it { level.at?(:debug).should be_false }
76
- it { level.at?(:info).should be_false }
77
- it { level.at?(:warn).should be_false }
78
- it { level.at?(:error).should be_true }
79
- it { level.at?(:fatal).should be_true }
75
+ it "should be valid" do
76
+ level.at?(:debug).should be_false
77
+ level.at?(:info).should be_false
78
+ level.at?(:warn).should be_false
79
+ level.at?(:error).should be_true
80
+ level.at?(:fatal).should be_true
81
+ end
80
82
  end
81
83
 
82
84
  context "complex string with outer boundaries" do
83
85
  subject { 'gte.info lte.error' }
84
86
 
85
- it { level.at?(:debug).should be_false }
86
- it { level.at?(:info).should be_true }
87
- it { level.at?(:warn).should be_true }
88
- it { level.at?(:error).should be_true }
89
- it { level.at?(:fatal).should be_false }
87
+ it "should be valid" do
88
+ level.at?(:debug).should be_false
89
+ level.at?(:info).should be_true
90
+ level.at?(:warn).should be_true
91
+ level.at?(:error).should be_true
92
+ level.at?(:fatal).should be_false
93
+ end
90
94
  end
91
95
 
92
96
  context "complex string with inner boundaries" do
93
97
  subject { 'gt.info lt.error' }
94
98
 
95
- it { level.at?(:debug).should be_false }
96
- it { level.at?(:info).should be_false }
97
- it { level.at?(:warn).should be_true }
98
- it { level.at?(:error).should be_false }
99
- it { level.at?(:fatal).should be_false }
99
+ it "should be valid" do
100
+ level.at?(:debug).should be_false
101
+ level.at?(:info).should be_false
102
+ level.at?(:warn).should be_true
103
+ level.at?(:error).should be_false
104
+ level.at?(:fatal).should be_false
105
+ end
100
106
  end
101
107
 
102
108
  context "complex string with precise boundaries" do
103
109
  subject { 'at.info at.error' }
104
110
 
105
- it { level.at?(:debug).should be_false }
106
- it { level.at?(:info).should be_true }
107
- it { level.at?(:warn).should be_false }
108
- it { level.at?(:error).should be_true }
109
- it { level.at?(:fatal).should be_false }
111
+ it "should be valid" do
112
+ level.at?(:debug).should be_false
113
+ level.at?(:info).should be_true
114
+ level.at?(:warn).should be_false
115
+ level.at?(:error).should be_true
116
+ level.at?(:fatal).should be_false
117
+ end
118
+ end
119
+
120
+ context "complex string with combined boundaries" do
121
+ subject { 'gte.error at.debug' }
122
+
123
+ it "should be valid" do
124
+ level.at?(:debug).should be_true
125
+ level.at?(:info).should be_false
126
+ level.at?(:warn).should be_false
127
+ level.at?(:error).should be_true
128
+ level.at?(:fatal).should be_true
129
+ end
110
130
  end
111
131
  end
112
132
 
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ class LoggableFactory
4
+ include Yell::Loggable
5
+ end
6
+
7
+ describe Yell::Loggable do
8
+ subject { LoggableFactory.new }
9
+
10
+ it { should respond_to :logger }
11
+
12
+ it ":logger should pass class name to the repository" do
13
+ mock( Yell::Repository )[ LoggableFactory ]
14
+
15
+ subject.logger
16
+ end
17
+
18
+ end
19
+
@@ -8,16 +8,32 @@ describe Yell::Repository do
8
8
 
9
9
 
10
10
  context ".[]" do
11
- context "when not set" do
12
- it { should be_nil }
11
+ it "should raise when not set" do
12
+ lambda { subject }.should raise_error( Yell::LoggerNotFound )
13
13
  end
14
14
 
15
- context "when assigned" do
15
+ it "should return the logger when set" do
16
+ Yell::Repository[ name ] = logger
17
+
18
+ subject.should == logger
19
+ end
20
+
21
+ context "given a Class" do
16
22
  before do
17
- Yell::Repository[ name ] = logger
23
+ @logger = Yell.new :stdout, :name => "Numeric"
24
+ end
25
+
26
+ it "should raise when not set" do
27
+ lambda { Yell::Repository[ String ] }.should raise_error( Yell::LoggerNotFound )
28
+ end
29
+
30
+ it "should return the logger" do
31
+ Yell::Repository[ Numeric ].should == @logger
18
32
  end
19
33
 
20
- it { should == logger }
34
+ it "should return the logger when superclass has it defined" do
35
+ Yell::Repository[ Integer ].should == @logger
36
+ end
21
37
  end
22
38
  end
23
39
 
@@ -63,15 +79,5 @@ describe Yell::Repository do
63
79
  it { should == loggers }
64
80
  end
65
81
 
66
- context "clear" do
67
- subject { Yell::Repository.loggers }
68
-
69
- before do
70
- Yell::Repository[ name ] = logger
71
- Yell::Repository.clear
72
- end
73
-
74
- it { should be_empty }
75
- end
76
82
  end
77
83
 
data/spec/yell_spec.rb CHANGED
@@ -7,8 +7,8 @@ describe Yell do
7
7
 
8
8
  it { should be_kind_of Yell::Logger }
9
9
 
10
- it "should raise UnregisteredAdapter when adapter cant be loaded" do
11
- lambda { Yell.new :unknownadapter }.should raise_error( Yell::UnregisteredAdapter )
10
+ it "should raise AdapterNotFound when adapter cant be loaded" do
11
+ lambda { Yell.new :unknownadapter }.should raise_error( Yell::AdapterNotFound )
12
12
  end
13
13
 
14
14
  context :level do
@@ -53,14 +53,6 @@ describe Yell do
53
53
  end
54
54
  end
55
55
 
56
- context :loggers do
57
- it "should delegate to the repository" do
58
- mock.proxy( Yell::Repository ).loggers
59
-
60
- Yell.loggers
61
- end
62
- end
63
-
64
56
  context :env do
65
57
  subject { Yell.env }
66
58
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.4
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-14 00:00:00.000000000 Z
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Yell - Your Extensible Logging Library. Define multiple adapters, various
15
15
  log level combinations or message formatting options like you've never done before
@@ -33,6 +33,8 @@ files:
33
33
  - examples/003.3-formatting-ExtendedFormat.rb
34
34
  - examples/003.4-formatting-on-your-own.rb
35
35
  - examples/004.1-colorizing-the-log-output.rb
36
+ - examples/005.1-repository.rb
37
+ - examples/006.1-the-loggable-module.rb
36
38
  - lib/yell.rb
37
39
  - lib/yell/adapters.rb
38
40
  - lib/yell/adapters/base.rb
@@ -44,6 +46,7 @@ files:
44
46
  - lib/yell/event.rb
45
47
  - lib/yell/formatter.rb
46
48
  - lib/yell/level.rb
49
+ - lib/yell/loggable.rb
47
50
  - lib/yell/logger.rb
48
51
  - lib/yell/repository.rb
49
52
  - lib/yell/version.rb
@@ -60,6 +63,7 @@ files:
60
63
  - spec/yell/event_spec.rb
61
64
  - spec/yell/formatter_spec.rb
62
65
  - spec/yell/level_spec.rb
66
+ - spec/yell/loggable_spec.rb
63
67
  - spec/yell/logger_spec.rb
64
68
  - spec/yell/repository_spec.rb
65
69
  - spec/yell_spec.rb
@@ -102,6 +106,7 @@ test_files:
102
106
  - spec/yell/event_spec.rb
103
107
  - spec/yell/formatter_spec.rb
104
108
  - spec/yell/level_spec.rb
109
+ - spec/yell/loggable_spec.rb
105
110
  - spec/yell/logger_spec.rb
106
111
  - spec/yell/repository_spec.rb
107
112
  - spec/yell_spec.rb