yell 1.5.1 → 2.0.0.pre

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmUxZmVhNGNiZjdmNGJmZDk4MTU0MzQ0MDFiZGJmZmViNmYwNGI2YQ==
4
+ NGIxMzYwMGJmMjdmOGEzNmMwZTgwZmM3MmE2YzM0YmVlZTg2MTE0NA==
5
5
  data.tar.gz: !binary |-
6
- NjRlM2U0MjliZjAwOTU2ZDk2MmNmNGJkYWNkZWZjN2VmMjZlNTJkOA==
6
+ OTExOWM2MmU1NTk3OTU0YzgzODIwYzQ0Njg2ZTMyYTZiYTkxMDYzMQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTc1ZTVjYWQ5NjMzYzdhOGUwMWEwMjgzM2M5YWIxMzk3NDhiYjVhZDYyZjlj
10
- N2ZiZjY0NzczNjgwMzA5NjI3MzJkNjg2ODAzMjRiNmM2Yzg1ODQyOWJhNWFh
11
- ZTkxYmVlOWNlOWU4M2Q0Yzc4ZTI0MWJmYjM1YjU4Y2VmZTE5N2Y=
9
+ YTEyNDk1NTI4NWZhZjZhNjRlYzdiNjkyOWI0MDY4YzlhZWY0ZWQ2MTI0YTNl
10
+ MGY4ZmM2N2I2ZTRjODk1NGZmMWY2MzdiNTVhNTI5NWNmNWYyNDJmZTJjMDJl
11
+ NDEzZTc4OGI5YWI2MTkwNTlmZjg3ZjIxNDE2ZTVmZWFkNjcwN2Q=
12
12
  data.tar.gz: !binary |-
13
- ZTYwZmI2MTFkNWQ5ZjAxM2YxYTE0ODNkNGI3NGNiY2FhYmZlMjYzZDJkNTY4
14
- YTRmYTk0ZWEzMTA2NjA3NzUxMTRjNzg1NzMyZmNlZGE1MzcxOTYxZWFkYzI1
15
- ZjllZDk1MTQ0NWVhMzEyMDcxNWM1Zjk4M2VhYzJlOTlkNzc5M2M=
13
+ ODM2ZTAyZDIwYWFiZmQ3OTE4ZTZiZmNiMGY5ODdkOWMxZmU1NDkzNzY0M2Jl
14
+ OTNkZmYyZTM3MDdkMjlhNDQ1ZDJlNDBiNjZkMDY3MWY3Njk3MzcwNjY0M2Nm
15
+ Y2ZiNjAzZTc4OTQ0NGQ0MDMyNGVhZmIzMGFkYzJmZDMwYzM1NjE=
data/Gemfile CHANGED
@@ -9,10 +9,13 @@ group :development, :test do
9
9
  gem 'rspec-core'
10
10
  gem 'rspec-expectations'
11
11
  gem "rr"
12
+ gem 'pry'
12
13
 
13
14
  gem 'timecop', '0.6.1'
14
15
 
15
16
  gem 'simplecov', :require => false, :platform => :ruby_20
16
17
  gem 'coveralls', :require => false, :platform => :ruby_20
18
+
19
+ gem 'activesupport', '~> 4'
17
20
  end
18
21
 
@@ -0,0 +1,18 @@
1
+ require 'logger'
2
+
3
+ class Logger
4
+
5
+ def level_with_yell=( level )
6
+ self.level_without_yell= Integer(level)
7
+ end
8
+ alias_method :level_without_yell=, :level=
9
+ alias_method :level=, :level_with_yell=
10
+
11
+ def add_with_yell( severity, message = nil, progname = nil, &block )
12
+ add_without_yell(Integer(severity), message, progname, &block)
13
+ end
14
+ alias_method :add_without_yell, :add
15
+ alias_method :add, :add_with_yell
16
+
17
+ end
18
+
data/lib/yell.rb CHANGED
@@ -129,6 +129,9 @@ require File.dirname(__FILE__) + '/yell/logger'
129
129
  # modules
130
130
  require File.dirname(__FILE__) + '/yell/loggable'
131
131
 
132
+ # core extensions
133
+ require File.dirname(__FILE__) + '/core_ext/logger'
134
+
132
135
  # register known adapters
133
136
  Yell.register :null, Yell::Adapters::Base # adapter that does nothing (for convenience only)
134
137
  Yell.register :file, Yell::Adapters::File
@@ -137,4 +140,3 @@ Yell.register :stdout, Yell::Adapters::Stdout
137
140
  Yell.register :stderr, Yell::Adapters::Stderr
138
141
 
139
142
 
140
-
@@ -25,7 +25,7 @@ module Yell #:nodoc:
25
25
  # end
26
26
  #
27
27
  # write do |event|
28
- # message = format.format(event)
28
+ # message = format.call(event)
29
29
  #
30
30
  # STDOUT.puts message
31
31
  # end
@@ -46,7 +46,7 @@ module Yell #:nodoc:
46
46
  @stream = nil
47
47
 
48
48
  self.colors = options.fetch(:colors, false)
49
- self.format = options.fetch(:format, nil)
49
+ self.formatter = options.fetch(:format, options[:formatter])
50
50
  self.sync = options.fetch(:sync, true)
51
51
 
52
52
  super
@@ -54,15 +54,14 @@ module Yell #:nodoc:
54
54
 
55
55
  # @overload write!( event )
56
56
  def write!( event )
57
- message = format.format(event)
57
+ message = formatter.call(event)
58
58
 
59
59
  # colorize if applicable
60
60
  if colors and color = TTYColors[event.level]
61
61
  message = color + message + TTYColors[-1]
62
62
  end
63
63
 
64
- message << "\n" unless message[-1] == ?\n
65
- stream.syswrite( message )
64
+ stream.syswrite(message)
66
65
 
67
66
  super
68
67
  end
@@ -93,7 +92,7 @@ module Yell #:nodoc:
93
92
 
94
93
  # @overload inspectables
95
94
  def inspectables
96
- super.concat [:format, :colors, :sync]
95
+ super.concat [:formatter, :colors, :sync]
97
96
  end
98
97
 
99
98
  end
@@ -13,8 +13,8 @@ module Yell #:nodoc:
13
13
  yaml = YAML.load( ERB.new(File.read(file)).result )
14
14
 
15
15
  # in case we have ActiveSupport
16
- if yaml.respond_to?(:with_indifference_access)
17
- yaml = yaml.with_indifferent_access
16
+ if defined?(ActiveSupport::HashWithIndifferentAccess)
17
+ yaml = ActiveSupport::HashWithIndifferentAccess.new(yaml)
18
18
  end
19
19
 
20
20
  yaml[Yell.env] || {}
data/lib/yell/event.rb CHANGED
@@ -14,6 +14,24 @@ module Yell #:nodoc:
14
14
  # jruby and rubinius seem to have a different caller
15
15
  CallerIndex = defined?(RUBY_ENGINE) && ["rbx", "jruby"].include?(RUBY_ENGINE) ? 1 : 2
16
16
 
17
+
18
+ class Options
19
+ attr_reader :severity
20
+ attr_reader :caller_offset
21
+
22
+ def initialize( severity, caller_offset )
23
+ @severity = severity
24
+ @caller_offset = caller_offset
25
+ end
26
+
27
+ def <=>( other )
28
+ @severity <=> other
29
+ end
30
+
31
+ alias :to_i :severity
32
+ alias :to_int :severity
33
+ end
34
+
17
35
  # Prefetch those values (no need to do that on every new instance)
18
36
  @@hostname = Socket.gethostname rescue nil
19
37
  @@progname = $0
@@ -21,7 +39,7 @@ module Yell #:nodoc:
21
39
  # Accessor to the log level
22
40
  attr_reader :level
23
41
 
24
- # Accessor to the log messages
42
+ # Accessor to the log message
25
43
  attr_reader :messages
26
44
 
27
45
  # Accessor to the time the log event occured
@@ -31,17 +49,18 @@ module Yell #:nodoc:
31
49
  attr_reader :name
32
50
 
33
51
 
34
- def initialize(logger, level, *messages, &block)
35
- @time = Time.now
36
- @level = level
37
- @name = logger.name
52
+ def initialize( logger, options, *messages, &block )
53
+ @time = Time.now
54
+ @name = logger.name
55
+
56
+ extract!(options)
38
57
 
39
58
  @messages = messages
40
- @messages << block.call if block
59
+ @messages << block.call unless block.nil?
41
60
 
42
- @caller = logger.trace.at?(level) ? caller[CallerIndex].to_s : ''
43
- @file = nil
44
- @line = nil
61
+ @caller = logger.trace.at?(level) ? caller[caller_index].to_s : ''
62
+ @file = nil
63
+ @line = nil
45
64
  @method = nil
46
65
 
47
66
  @pid = nil
@@ -85,6 +104,20 @@ module Yell #:nodoc:
85
104
 
86
105
  private
87
106
 
107
+ def extract!( options )
108
+ if options.is_a?(Yell::Event::Options)
109
+ @level = options.severity
110
+ @caller_offset = options.caller_offset
111
+ else
112
+ @level = options
113
+ @caller_offset = 0
114
+ end
115
+ end
116
+
117
+ def caller_index
118
+ CallerIndex + @caller_offset
119
+ end
120
+
88
121
  def backtrace!
89
122
  if m = CallerRegexp.match(@caller)
90
123
  @file, @line, @method = m[1..-1]
@@ -1,6 +1,14 @@
1
1
  # encoding: utf-8
2
2
  require 'time'
3
3
 
4
+ # TODO: Register custom formats
5
+ #
6
+ # @example The Yell default fomat
7
+ # Yell::Formatter.register(:default)
8
+ #
9
+ # @example The Ruby standard logger format
10
+ # Yell::Formatter.register(:stdlogger, "%l, [%d #%p] %5L -- : %m", "%Y-%m-%dT%H:%M:%S.%6N")
11
+ #
4
12
  module Yell #:nodoc:
5
13
 
6
14
  # No format on the log message
@@ -47,8 +55,8 @@ module Yell #:nodoc:
47
55
  class Formatter
48
56
 
49
57
  Table = {
50
- "m" => "message(*event.messages)", # Message
51
- "l" => "level(event.level)[0,1]", # Level (short), e.g.'I', 'W'
58
+ "m" => "message(event.messages)", # Message
59
+ "l" => "level(event.level, 1)", # Level (short), e.g.'I', 'W'
52
60
  "L" => "level(event.level)", # Level, e.g. 'INFO', 'WARN'
53
61
  "d" => "date(event.time)", # ISO8601 Timestamp
54
62
  "h" => "event.hostname", # Hostname
@@ -62,7 +70,20 @@ module Yell #:nodoc:
62
70
  "N" => "event.name" # Name of the logger
63
71
  }
64
72
 
65
- Matcher = /([^%]*)(%\d*)?(#{Table.keys.join('|')})?(.*)/
73
+ # For standard formatted backwards compatibility
74
+ LegacyTable = Hash[ Table.keys.map { |k| [k, 'noop'] } ].merge(
75
+ 'm' => 'message(msg)',
76
+ 'l' => 'level(event, 1)',
77
+ 'L' => 'level(event)',
78
+ 'd' => 'date(time)',
79
+ "p" => "$$",
80
+ 'P' => 'progname'
81
+ )
82
+
83
+ PatternMatcher = /([^%]*)(%\d*)?(#{Table.keys.join('|')})?(.*)/m
84
+
85
+
86
+ attr_reader :pattern, :date_pattern
66
87
 
67
88
 
68
89
  # Initializes a new +Yell::Formatter+.
@@ -92,7 +113,7 @@ module Yell #:nodoc:
92
113
  @modifier = builder.modifier
93
114
 
94
115
  define_date_method!
95
- define_format_method!
116
+ define_call_method!
96
117
  end
97
118
 
98
119
  # Get a pretty string
@@ -117,8 +138,10 @@ module Yell #:nodoc:
117
138
  case
118
139
  when mod = @repository[message.class] || @repository[message.class.to_s]
119
140
  mod.call(message)
141
+ when message.is_a?(Array)
142
+ message.map { |m| call(m) }.join(" ")
120
143
  when message.is_a?(Hash)
121
- message.map { |k,v| "#{k}: #{v}" }.join(", ")
144
+ message.map { |k, v| "#{k}: #{v}" }.join(", ")
122
145
  when message.is_a?(Exception)
123
146
  backtrace = message.backtrace ? "\n\t#{message.backtrace.join("\n\t")}" : ""
124
147
  sprintf("%s: %s%s", message.class, message.message, backtrace)
@@ -138,6 +161,7 @@ module Yell #:nodoc:
138
161
  @modifier = Modifier.new
139
162
 
140
163
  @pattern = pattern || Yell::DefaultFormat
164
+ @pattern << "\n" unless @pattern[-1] == ?\n # add newline if not present
141
165
  @date_pattern = date_pattern || :iso8601
142
166
 
143
167
  block.call(self) if block
@@ -155,33 +179,36 @@ module Yell #:nodoc:
155
179
  else "iso8601(t)"
156
180
  end
157
181
 
158
- instance_eval %-
159
- def date( t )
160
- #{buf}
182
+ # define the method
183
+ instance_eval "def date(t = Time.now); #{buf}; end", __FILE__, __LINE__
184
+ end
185
+
186
+ # define a standard +Logger+ backwards compatible #call method for the formatter
187
+ def define_call_method!
188
+ instance_eval <<-METHOD, __FILE__, __LINE__
189
+ def call(event, time = nil, progname = nil, msg = nil)
190
+ event.is_a?(Yell::Event) ? #{to_sprintf(Table)} : #{to_sprintf(LegacyTable)}
161
191
  end
162
- -
192
+ METHOD
163
193
  end
164
194
 
165
- def define_format_method!
195
+ def to_sprintf( table )
196
+ # new and improved Yell version of a formatter call method
166
197
  buff, args, _pattern = "", [], @pattern.dup
167
198
 
168
199
  while true
169
- match = Matcher.match(_pattern)
200
+ match = PatternMatcher.match(_pattern)
170
201
 
171
202
  buff << match[1] unless match[1].empty?
172
203
  break if match[2].nil?
173
204
 
174
205
  buff << match[2] + 's'
175
- args << Table[ match[3] ]
206
+ args << table[ match[3] ]
176
207
 
177
208
  _pattern = match[4]
178
209
  end
179
210
 
180
- instance_eval <<-EOS, __FILE__, __LINE__
181
- def format( event )
182
- sprintf("#{buff}", #{args.join(',')})
183
- end
184
- EOS
211
+ "sprintf('#{buff}', #{args.join(', ')})"
185
212
  end
186
213
 
187
214
  # The iso8601 implementation of the standard Time library is more than
@@ -199,12 +226,22 @@ module Yell #:nodoc:
199
226
  t.strftime("%Y-%m-%dT%H:%M:%S#{zone}")
200
227
  end
201
228
 
202
- def level( l )
203
- Yell::Severities[ l ]
229
+ def level( sev, length = nil )
230
+ severity = case sev
231
+ when Integer then Yell::Severities[sev] || 'ANY'
232
+ else sev
233
+ end
234
+
235
+ length.nil? ? severity : severity[0, length]
236
+ end
237
+
238
+ def message( messages )
239
+ @modifier.call(messages.is_a?(Array) && messages.size == 1 ? messages.first : messages)
204
240
  end
205
241
 
206
- def message( *messages )
207
- messages.map { |m| @modifier.call(m) }.join(" ")
242
+ # do nothing
243
+ def noop
244
+ ''
208
245
  end
209
246
 
210
247
  end
@@ -4,18 +4,21 @@ module Yell #:nodoc:
4
4
  module Formatter #:nodoc:
5
5
 
6
6
  # Set the format for your message.
7
- def format=( pattern )
7
+ def formatter=( pattern )
8
8
  @formatter = case pattern
9
9
  when Yell::Formatter then pattern
10
10
  when false then Yell::Formatter.new(Yell::NoFormat)
11
11
  else Yell::Formatter.new(*pattern)
12
12
  end
13
13
  end
14
+ alias :format= :formatter=
14
15
 
15
16
  # @private
16
- def format
17
+ def formatter
17
18
  @formatter
18
19
  end
20
+ alias :format :formatter
21
+
19
22
 
20
23
  private
21
24
 
data/lib/yell/level.rb CHANGED
@@ -23,6 +23,7 @@ module Yell #:nodoc:
23
23
  # @example Set at :info only
24
24
  # Yell::Level.new.at(:info)
25
25
  class Level
26
+ include Comparable
26
27
 
27
28
  InterpretRegexp = /(at|gt|gte|lt|lte)?\.?(#{Yell::Severities.join('|')})/i
28
29
 
@@ -42,7 +43,7 @@ module Yell #:nodoc:
42
43
  #
43
44
  # @param [Integer,String,Symbol,Array,Range,nil] severity The severity for the level.
44
45
  def initialize( *severities )
45
- set( *severities )
46
+ set(*severities)
46
47
  end
47
48
 
48
49
  # Set the severity to the given format
@@ -51,11 +52,11 @@ module Yell #:nodoc:
51
52
  severity = severities.length > 1 ? severities : severities.first
52
53
 
53
54
  case severity
54
- when Array then at( *severity )
55
- when Range then gte( severity.first ).lte( severity.last )
56
- when Integer, Symbol then gte( severity )
57
- when String then interpret( severity )
55
+ when Array then at(*severity)
56
+ when Range then gte(severity.first).lte(severity.last)
57
+ when String then interpret(severity)
58
58
  when Yell::Level then @severities = severity.severities
59
+ when Integer, Symbol then gte(severity)
59
60
  end
60
61
  end
61
62
 
@@ -67,7 +68,7 @@ module Yell #:nodoc:
67
68
  #
68
69
  # @return [Boolean] tru or false
69
70
  def at?( severity )
70
- index = index_from( severity )
71
+ index = index_from(severity)
71
72
 
72
73
  index.nil? ? false : @severities[index]
73
74
  end
@@ -146,8 +147,12 @@ module Yell #:nodoc:
146
147
 
147
148
  # @private
148
149
  def ==(other)
149
- return super unless other.respond_to?(:severities)
150
- severities == other.severities
150
+ other.respond_to?(:severities) ? severities == other.severities : super
151
+ end
152
+
153
+ # @private
154
+ def <=>( other )
155
+ other.is_a?(Numeric) ? to_i <=> other : super
151
156
  end
152
157
 
153
158
 
@@ -162,7 +167,7 @@ module Yell #:nodoc:
162
167
  end
163
168
 
164
169
  def calculate!( modifier, severity )
165
- index = index_from( severity )
170
+ index = index_from(severity)
166
171
  return if index.nil?
167
172
 
168
173
  case modifier
@@ -178,9 +183,8 @@ module Yell #:nodoc:
178
183
 
179
184
  def index_from( severity )
180
185
  case severity
181
- when Integer then severity
182
- when String, Symbol then Yell::Severities.index( severity.to_s.upcase )
183
- else nil
186
+ when String, Symbol then Yell::Severities.index(severity.to_s.upcase)
187
+ else Integer(severity)
184
188
  end
185
189
  end
186
190
 
data/lib/yell/logger.rb CHANGED
@@ -2,32 +2,6 @@
2
2
 
3
3
  require 'pathname'
4
4
 
5
- # TODO: DSL improvements
6
- #
7
- # Initlalize an empty logger
8
- # logger = Yell.new(adapters: false)
9
- # logger.adapters.add :stdout
10
- #
11
- # Or shorthand for adapters.add
12
- # logger.add :stdout
13
- #
14
- # Or with a block
15
- # logger = Yell.new do |l|
16
- # l.add :stdout
17
- # l.add :stderr
18
- # end
19
- #
20
- # logger = Yell.new do |l|
21
- # l.adapters.add :stdout
22
- # l.adapters.add :stderr
23
- # end
24
- #
25
- #
26
- # Define Silencers
27
- # logger = Yell.new do |l|
28
- # l.silence /password/
29
- # end
30
- #
31
5
  module Yell #:nodoc:
32
6
 
33
7
  # The +Yell::Logger+ is your entrypoint. Anything onwards is derived from here.
@@ -73,20 +47,22 @@ module Yell #:nodoc:
73
47
  # extract options
74
48
  @options = args.last.is_a?(Hash) ? args.pop : {}
75
49
 
76
- # adapters may be passed in the options
77
- extract!(*@options[:adapters]) if @options.key?(:adapters)
78
-
79
50
  # check if filename was given as argument and put it into the @options
80
51
  if [String, Pathname].include?(args.last.class)
81
52
  @options[:filename] = args.pop unless @options[:filename]
82
53
  end
83
54
 
84
- self.level = @options.fetch(:level, 0) # debug by default
85
- self.name = @options.fetch(:name, nil) # no name by default
86
- self.trace = @options.fetch(:trace, :error) # trace from :error level onwards
55
+ # FIXME: :format is deprecated in future versions --R
56
+ self.formatter = @options.fetch(:format, @options[:formatter])
57
+ self.level = @options.fetch(:level, 0)
58
+ self.name = @options.fetch(:name, nil)
59
+ self.trace = @options.fetch(:trace, :error)
87
60
 
88
61
  # silencer
89
- self.silence(*@options[:silence])
62
+ self.silence(*@options[:silence]) if @options.key?(:silence)
63
+
64
+ # adapters may be passed in the options
65
+ extract!(*@options[:adapters]) if @options.key?(:adapters)
90
66
 
91
67
  # extract adapter
92
68
  self.adapter(args.pop) if args.any?
@@ -104,12 +80,23 @@ module Yell #:nodoc:
104
80
  #
105
81
  # @return [String] The logger's name
106
82
  def name=( val )
107
- @name = val
108
- Yell::Repository[@name] = self if @name
83
+ Yell::Repository[val] = self if val
84
+ @name = val.nil? ? "<#{self.class.name}##{object_id}>": val
109
85
 
110
86
  @name
111
87
  end
112
88
 
89
+ # Somewhat backwards compatible method (not fully though)
90
+ def add( options, *messages, &block )
91
+ return false unless level.at?(options)
92
+
93
+ messages = silencer.call(*messages)
94
+ return false if messages.empty?
95
+
96
+ event = Yell::Event.new(self, options, *messages, &block)
97
+ write(event)
98
+ end
99
+
113
100
  # Creates instance methods for every log level:
114
101
  # `debug` and `debug?`
115
102
  # `info` and `info?`
@@ -119,18 +106,12 @@ module Yell #:nodoc:
119
106
  Yell::Severities.each_with_index do |s, index|
120
107
  name = s.downcase
121
108
 
122
- class_eval <<-EOS, __FILE__, __LINE__
109
+ class_eval <<-EOS, __FILE__, __LINE__ + index
123
110
  def #{name}?; level.at?(#{index}); end # def info?; level.at?(1); end
124
111
  #
125
112
  def #{name}( *m, &b ) # def info( *m, &b )
126
- return false unless #{name}? # return false unless info?
127
- #
128
- m = silencer.silence(*m) if silencer.silence? # m = silencer.silence(*m) if silencer.silence?
129
- return false if m.empty? # return false if m.empty?
130
- #
131
- event = Yell::Event.new(self, #{index}, *m, &b) # event = Yell::Event.new(self, 1, *m, &b)
132
- write(event) # write(event)
133
- true # true
113
+ options = Yell::Event::Options.new(#{index}, 1)
114
+ add(options, *m, &b) # add(Yell::Event::Options.new(1, 1), *m, &b)
134
115
  end # end
135
116
  EOS
136
117
  end
@@ -149,6 +130,11 @@ module Yell #:nodoc:
149
130
 
150
131
  private
151
132
 
133
+ def write( event )
134
+ _adapter.write(event)
135
+ true
136
+ end
137
+
152
138
  # The :adapters key may be passed to the options hash. It may appear in
153
139
  # multiple variations:
154
140
  #
@@ -166,11 +152,6 @@ module Yell #:nodoc:
166
152
  end
167
153
  end
168
154
 
169
- # Cycles all the adapters and writes the message
170
- def write( event )
171
- _adapter.write(event)
172
- end
173
-
174
155
  # Get an array of inspected attributes for the adapter.
175
156
  def inspectables
176
157
  [:name] | super
data/lib/yell/silencer.rb CHANGED
@@ -4,6 +4,15 @@ module Yell #:nodoc:
4
4
  # The +Yell::Silencer+ is your handly helper for stiping out unwanted log messages.
5
5
  class Silencer
6
6
 
7
+ class PresetNotFound < StandardError
8
+ def message; "Could not find a preset for #{super.inspect}"; end
9
+ end
10
+
11
+ Presets = {
12
+ :assets => [/^Started GET "\/assets/, /^Served asset/, /^$/] # for Rails
13
+ }
14
+
15
+
7
16
  def initialize( *patterns )
8
17
  @patterns = patterns.dup
9
18
  end
@@ -17,27 +26,24 @@ module Yell #:nodoc:
17
26
  # @example Add regular expressions
18
27
  # add( /password/ )
19
28
  #
20
- # @return [Array] All set patterns
29
+ # @return [self] The silencer instance
21
30
  def add( *patterns )
22
- @patterns = @patterns | patterns.compact
31
+ patterns.each { |pattern| add!(pattern) }
32
+
33
+ self
23
34
  end
24
35
 
25
36
  # Clears out all the messages that would match any defined pattern
26
37
  #
27
38
  # @example
28
- # silence('username', 'password')
39
+ # call(['username', 'password'])
29
40
  # #=> ['username]
30
41
  #
31
- # @return [Array<String>] The remaining messages
32
- def silence( *messages )
33
- messages.reject { |m| matches?(m) }
34
- end
42
+ # @return [Array] The remaining messages
43
+ def call( *messages )
44
+ return messages if @patterns.empty?
35
45
 
36
- # Anything to silence at all?
37
- #
38
- # @return [Boolean] true or false
39
- def silence?
40
- @patterns.any?
46
+ messages.reject { |m| matches?(m) }
41
47
  end
42
48
 
43
49
  # Get a pretty string
@@ -53,6 +59,17 @@ module Yell #:nodoc:
53
59
 
54
60
  private
55
61
 
62
+ def add!( pattern )
63
+ @patterns = @patterns | fetch(pattern)
64
+ end
65
+
66
+ def fetch( pattern )
67
+ case pattern
68
+ when Symbol then Presets[pattern] or raise PresetNotFound.new(pattern)
69
+ else [pattern]
70
+ end
71
+ end
72
+
56
73
  # Check if the provided message matches any of the defined patterns.
57
74
  #
58
75
  # @example
@@ -61,7 +78,7 @@ module Yell #:nodoc:
61
78
  #
62
79
  # @return [Boolean] true or false
63
80
  def matches?( message )
64
- @patterns.any? { |pattern| message.match(pattern) }
81
+ @patterns.any? { |pattern| message.respond_to?(:match) && message.match(pattern) }
65
82
  end
66
83
 
67
84
  end
data/lib/yell/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Yell #:nodoc:
4
- VERSION = "1.5.1"
4
+ VERSION = "2.0.0.pre"
5
5
 
6
6
  end
7
7
 
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'active_support'
4
+
5
+ # make a setup just like in railties ~> 4.0.0
6
+ #
7
+ # We simulate the case when Rails 4 starts up its server
8
+ # and wants to append the log output.
9
+ describe "Compatibility to ActiveSupport::Logger" do
10
+
11
+ let!(:yell) { Yell.new($stdout, :format => "%m") }
12
+
13
+ let!(:logger) do
14
+ console = ActiveSupport::Logger.new($stdout)
15
+ console.formatter = yell.formatter
16
+ console.level = yell.level
17
+
18
+ yell.extend(ActiveSupport::Logger.broadcast(console))
19
+
20
+ console
21
+ end
22
+
23
+ it "should behave correctly" do
24
+ mock($stdout).syswrite("Hello World\n") # yell
25
+ mock($stdout).write("Hello World\n") # logger
26
+
27
+ yell.info "Hello World"
28
+ end
29
+
30
+ end
31
+
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require 'logger'
3
+
4
+ describe "backwards compatible formatter" do
5
+
6
+ let(:time) { Time.now }
7
+ let(:formatter) { Yell::Formatter.new(Yell::DefaultFormat) }
8
+ let(:logger) { Logger.new($stdout) }
9
+
10
+ before do
11
+ Timecop.freeze(time)
12
+
13
+ logger.formatter = formatter
14
+ end
15
+
16
+ it "should format out the message correctly" do
17
+ mock($stdout).write("#{time.iso8601} [ INFO] #{$$} : Hello World!\n")
18
+
19
+ logger.info "Hello World!"
20
+ end
21
+
22
+ end
23
+
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ require 'logger'
3
+
4
+ describe "backwards compatible level" do
5
+
6
+ let(:level) { Yell::Level.new(:error) }
7
+ let(:logger) { Logger.new($stdout) }
8
+
9
+ before do
10
+ logger.level = level
11
+ end
12
+
13
+ it "should format out the level correctly" do
14
+ expect(logger.level).to eq(level.to_i)
15
+ end
16
+
17
+ end
18
+
data/spec/spec_helper.rb CHANGED
@@ -7,11 +7,13 @@ require 'rspec/core'
7
7
  require 'rspec/expectations'
8
8
  require 'rr'
9
9
  require 'timecop'
10
+ require 'pry'
10
11
 
11
12
  begin
12
13
  require 'coveralls'
13
-
14
14
  require 'simplecov'
15
+
16
+ STDERR.puts "Running coverage..."
15
17
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
16
18
  SimpleCov::Formatter::HTMLFormatter,
17
19
  Coveralls::SimpleCov::Formatter
@@ -21,7 +23,7 @@ begin
21
23
  add_filter 'spec'
22
24
  end
23
25
  rescue LoadError
24
- STDERR.puts "Not running coverage..."
26
+ # do nothing
25
27
  end
26
28
 
27
29
  require 'yell'
@@ -29,13 +31,13 @@ require 'yell'
29
31
  RSpec.configure do |config|
30
32
  config.mock_framework = :rr
31
33
 
32
- config.before do
34
+ config.before :each do
33
35
  Yell::Repository.loggers.clear
34
36
 
35
37
  Dir[ fixture_path + "/*.log" ].each { |f| File.delete f }
36
38
  end
37
39
 
38
- config.after do
40
+ config.after :each do
39
41
  Timecop.return # release time after each test
40
42
  end
41
43
 
@@ -55,14 +55,14 @@ describe Yell::Adapters::Io do
55
55
  end
56
56
 
57
57
  it "should format the message" do
58
- mock.proxy(adapter.format).format( event )
58
+ mock.proxy(adapter.format).call( event )
59
59
 
60
60
  adapter.write(event)
61
61
  end
62
62
 
63
63
  it "should print formatted message to stream" do
64
- formatted = Yell::Formatter.new.format( event )
65
- mock(stream).syswrite( formatted << "\n" )
64
+ formatted = Yell::Formatter.new.call(event)
65
+ mock(stream).syswrite(formatted)
66
66
 
67
67
  adapter.write(event)
68
68
  end
@@ -11,7 +11,7 @@ describe Yell::Formatter do
11
11
 
12
12
  let(:time) { Time.now }
13
13
 
14
- subject { formatter.format(event) }
14
+ subject { formatter.call(event) }
15
15
 
16
16
  before do
17
17
  Timecop.freeze(time)
@@ -20,42 +20,42 @@ describe Yell::Formatter do
20
20
  describe "patterns" do
21
21
  context "%m" do
22
22
  let(:pattern) { "%m" }
23
- it { should eq(event.messages.join(' ')) }
23
+ it { should eq("#{event.messages.join(' ')}\n") }
24
24
  end
25
25
 
26
26
  context "%l" do
27
27
  let(:pattern) { "%l" }
28
- it { should eq(Yell::Severities[event.level][0,1]) }
28
+ it { should eq("#{Yell::Severities[event.level][0,1]}\n") }
29
29
  end
30
30
 
31
31
  context "%L" do
32
32
  let(:pattern) { "%L" }
33
- it { should eq(Yell::Severities[event.level]) }
33
+ it { should eq("#{Yell::Severities[event.level]}\n") }
34
34
  end
35
35
 
36
36
  context "%d" do
37
37
  let(:pattern) { "%d" }
38
- it { should eq(event.time.iso8601) }
38
+ it { should eq("#{event.time.iso8601}\n") }
39
39
  end
40
40
 
41
41
  context "%p" do
42
42
  let(:pattern) { "%p" }
43
- it { should eq(event.pid.to_s) }
43
+ it { should eq("#{event.pid}\n") }
44
44
  end
45
45
 
46
46
  context "%P" do
47
47
  let(:pattern) { "%P" }
48
- it { should eq(event.progname) }
48
+ it { should eq("#{event.progname}\n") }
49
49
  end
50
50
 
51
51
  context "%t" do
52
52
  let(:pattern) { "%t" }
53
- it { should eq(event.thread_id.to_s) }
53
+ it { should eq("#{event.thread_id}\n") }
54
54
  end
55
55
 
56
56
  context "%h" do
57
57
  let(:pattern) { "%h" }
58
- it { should eq(event.hostname) }
58
+ it { should eq("#{event.hostname}\n") }
59
59
  end
60
60
 
61
61
  context ":caller" do
@@ -71,50 +71,50 @@ describe Yell::Formatter do
71
71
 
72
72
  context "%F" do
73
73
  let(:pattern) { "%F" }
74
- it { should eq("/path/to/file.rb") }
74
+ it { should eq("/path/to/file.rb\n") }
75
75
  end
76
76
 
77
77
  context "%f" do
78
78
  let(:pattern) { "%f" }
79
- it { should eq("file.rb") }
79
+ it { should eq("file.rb\n") }
80
80
  end
81
81
 
82
82
  context "%M" do
83
83
  let(:pattern) { "%M" }
84
- it { should eq("test_method") }
84
+ it { should eq("test_method\n") }
85
85
  end
86
86
 
87
87
  context "%n" do
88
88
  let(:pattern) { "%n" }
89
- it { should eq("123") }
89
+ it { should eq("123\n") }
90
90
  end
91
91
  end
92
92
 
93
93
  context "%N" do
94
94
  let(:pattern) { "%N" }
95
- it { should eq("Yell") }
95
+ it { should eq("Yell\n") }
96
96
  end
97
97
  end
98
98
 
99
99
  describe "presets" do
100
100
  context "NoFormat" do
101
101
  let(:pattern) { Yell::NoFormat }
102
- it { should eq("Hello World!") }
102
+ it { should eq("Hello World!\n") }
103
103
  end
104
104
 
105
105
  context "DefaultFormat" do
106
106
  let(:pattern) { Yell::DefaultFormat }
107
- it { should eq("#{time.iso8601} [ INFO] #{$$} : Hello World!") }
107
+ it { should eq("#{time.iso8601} [ INFO] #{$$} : Hello World!\n") }
108
108
  end
109
109
 
110
110
  context "BasicFormat" do
111
111
  let(:pattern) { Yell::BasicFormat }
112
- it { should eq("I, #{time.iso8601} : Hello World!") }
112
+ it { should eq("I, #{time.iso8601} : Hello World!\n") }
113
113
  end
114
114
 
115
115
  context "ExtendedFormat" do
116
116
  let(:pattern) { Yell::ExtendedFormat }
117
- it { should eq("#{time.iso8601} [ INFO] #{$$} #{Socket.gethostname} : Hello World!") }
117
+ it { should eq("#{time.iso8601} [ INFO] #{$$} #{Socket.gethostname} : Hello World!\n") }
118
118
  end
119
119
  end
120
120
 
@@ -125,13 +125,13 @@ describe Yell::Formatter do
125
125
  stub(message).backtrace { ["backtrace"] }
126
126
  end
127
127
 
128
- it { should eq("StandardError: This is an Exception\n\tbacktrace") }
128
+ it { should eq("StandardError: This is an Exception\n\tbacktrace\n") }
129
129
  end
130
130
 
131
131
  describe "Hash" do
132
132
  let(:message) { {:test => 'message'} }
133
133
 
134
- it { should eq("test: message") }
134
+ it { should eq("test: message\n") }
135
135
  end
136
136
 
137
137
  describe "custom message modifiers" do
@@ -139,7 +139,7 @@ describe Yell::Formatter do
139
139
  Yell::Formatter.new(pattern) { |f| f.modify(String) { |m| "Modified! #{m}" } }
140
140
  end
141
141
 
142
- it { should eq("Modified! #{message}") }
142
+ it { should eq("Modified! #{message}\n") }
143
143
  end
144
144
 
145
145
  end
@@ -3,12 +3,12 @@ require 'spec_helper'
3
3
  class LoggerFactory
4
4
  attr_accessor :logger
5
5
 
6
- def foo
6
+ def info
7
7
  logger.info :foo
8
8
  end
9
9
 
10
- def bar
11
- logger.info :bar
10
+ def add
11
+ logger.add 1, :bar
12
12
  end
13
13
  end
14
14
 
@@ -19,8 +19,6 @@ describe Yell::Logger do
19
19
  let(:logger) { Yell::Logger.new }
20
20
  subject { logger }
21
21
 
22
- its(:name) { should be_nil }
23
-
24
22
  context "log methods" do
25
23
  it { should respond_to(:debug) }
26
24
  it { should respond_to(:debug?) }
@@ -41,10 +39,16 @@ describe Yell::Logger do
41
39
  it { should respond_to(:unknown?) }
42
40
  end
43
41
 
44
- context "default #adapter" do
45
- subject { logger._adapter }
42
+ context "default #name" do
43
+ its(:name) { should eq("<Yell::Logger##{logger.object_id}>") }
44
+
45
+ it "should not be added to the repository" do
46
+ expect { Yell::Repository[logger.name] }.to raise_error(Yell::LoggerNotFound)
47
+ end
48
+ end
46
49
 
47
- it { should be_kind_of(Yell::Adapters::File) }
50
+ context "default #adapter" do
51
+ its(:_adapter) { should be_kind_of(Yell::Adapters::File) }
48
52
  end
49
53
 
50
54
  context "default #level" do
@@ -187,11 +191,11 @@ describe Yell::Logger do
187
191
  factory = LoggerFactory.new
188
192
  factory.logger = logger
189
193
 
190
- mock(stdout.send(:stream)).syswrite("#{__FILE__}, 7: foo\n")
191
- mock(stdout.send(:stream)).syswrite("#{__FILE__}, 11: bar\n")
194
+ mock(stdout.send(:stream)).syswrite("#{__FILE__}, 7: info\n")
195
+ mock(stdout.send(:stream)).syswrite("#{__FILE__}, 11: add\n")
192
196
 
193
- factory.foo
194
- factory.bar
197
+ factory.info
198
+ factory.add
195
199
  end
196
200
  end
197
201
 
@@ -206,19 +210,19 @@ describe Yell::Logger do
206
210
  end
207
211
 
208
212
  it "should output multiple messages" do
209
- logger.info "Hello", "W", "o", "r", "l", "d"
213
+ logger.info ["Hello", "W", "o", "r", "l", "d"]
210
214
 
211
215
  expect(line).to eq("Hello W o r l d\n")
212
216
  end
213
217
 
214
218
  it "should output a hash and message" do
215
- logger.info "Hello World", :test => :message
219
+ logger.info ["Hello World", {:test => :message}]
216
220
 
217
221
  expect(line).to eq("Hello World test: message\n")
218
222
  end
219
223
 
220
224
  it "should output a hash and message" do
221
- logger.info( {:test => :message}, "Hello World" )
225
+ logger.info [{:test => :message}, "Hello World"]
222
226
 
223
227
  expect(line).to eq("test: message Hello World\n")
224
228
  end
@@ -24,26 +24,15 @@ describe Yell::Silencer do
24
24
  end
25
25
  end
26
26
 
27
- context "#silence?" do
28
- let(:silencer) { Yell::Silencer.new }
29
-
30
- it "should be false when no patterns present" do
31
- expect(silencer.silence?).to be_false
32
- end
33
-
34
- it "should be true when patterns present" do
35
- silencer.add /this/
36
-
37
- expect(silencer.silence?).to be_true
38
- end
39
- end
40
-
41
- context "#silence" do
27
+ context "#call" do
42
28
  let(:silencer) { Yell::Silencer.new(/this/) }
43
29
 
44
30
  it "should reject messages that match any pattern" do
45
- expect(silencer.silence("this", "that")).to eq(["that"])
31
+ expect(silencer.call("this")).to eq([])
32
+ expect(silencer.call("that")).to eq(["that"])
33
+ expect(silencer.call("this", "that")).to eq(["that"])
46
34
  end
47
35
  end
48
36
 
49
37
  end
38
+
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: 1.5.1
4
+ version: 2.0.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rudolf Schmidt
@@ -35,6 +35,7 @@ files:
35
35
  - examples/005.1-repository.rb
36
36
  - examples/006.1-the-loggable-module.rb
37
37
  - examples/006.2-the-loggable-module-with-inheritance.rb
38
+ - lib/core_ext/logger.rb
38
39
  - lib/yell.rb
39
40
  - lib/yell/adapters.rb
40
41
  - lib/yell/adapters/base.rb
@@ -57,6 +58,9 @@ files:
57
58
  - lib/yell/repository.rb
58
59
  - lib/yell/silencer.rb
59
60
  - lib/yell/version.rb
61
+ - spec/compatibility/activesupport_logger_spec.rb
62
+ - spec/compatibility/formatter_spec.rb
63
+ - spec/compatibility/level_spec.rb
60
64
  - spec/fixtures/yell.yml
61
65
  - spec/spec_helper.rb
62
66
  - spec/threaded/yell_spec.rb
@@ -90,9 +94,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
94
  version: '0'
91
95
  required_rubygems_version: !ruby/object:Gem::Requirement
92
96
  requirements:
93
- - - ! '>='
97
+ - - ! '>'
94
98
  - !ruby/object:Gem::Version
95
- version: '0'
99
+ version: 1.3.1
96
100
  requirements: []
97
101
  rubyforge_project: yell
98
102
  rubygems_version: 2.1.5
@@ -100,6 +104,9 @@ signing_key:
100
104
  specification_version: 4
101
105
  summary: Yell - Your Extensible Logging Library
102
106
  test_files:
107
+ - spec/compatibility/activesupport_logger_spec.rb
108
+ - spec/compatibility/formatter_spec.rb
109
+ - spec/compatibility/level_spec.rb
103
110
  - spec/fixtures/yell.yml
104
111
  - spec/spec_helper.rb
105
112
  - spec/threaded/yell_spec.rb