logging 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,21 @@
1
+ == 0.7.0 / 2008-02-12
2
+
3
+ 1 major enhancement
4
+ - Rails compatibility
5
+ * renamed Logger#add method to Logger#add_appenders
6
+ * renamed Logger#remove method to Logger#remove_appenders
7
+ * renamed Logger#clear method to Logger#clear_appenders
8
+ * added a new Logger#add method that conforms to the calling
9
+ semantics of the Ruby stdlib Logger
10
+
11
+ 2 minor enhancements
12
+ - Speed improvements and test coverage
13
+ - Created a top-level Logging.init method that is used to
14
+ define the default logging levels
15
+
16
+ 1 bug fix
17
+ - Tweaked windows detection code
18
+
1
19
  == 0.6.3 / 2008-02-08
2
20
 
3
21
  2 minor enhancements
data/README.txt CHANGED
@@ -32,7 +32,7 @@ logged.
32
32
  In this example, a single logger is crated that will append to STDOUT and to a
33
33
  file. Only log messages that are informational or higher will be logged.
34
34
 
35
- reqruie 'logging'
35
+ require 'logging'
36
36
 
37
37
  logger = Logging::Logger['example_logger']
38
38
  logger.add Logging::Appender.stdout
data/lib/logging.rb CHANGED
@@ -1,4 +1,4 @@
1
- # $Id: logging.rb 90 2008-02-08 19:03:48Z tim_pease $
1
+ # $Id: logging.rb 98 2008-02-13 00:40:28Z tim_pease $
2
2
 
3
3
  # Equivalent to a header guard in C/C++
4
4
  # Used to prevent the class/module from being loaded more than once
@@ -12,9 +12,10 @@ unless defined? Logging
12
12
  module Logging
13
13
 
14
14
  # :stopdoc:
15
- VERSION = '0.6.3'
15
+ VERSION = '0.7.0'
16
16
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
17
17
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
18
+ WIN32 = %r/djgpp|(cyg|ms|bcc)win|mingw/ =~ RUBY_PLATFORM
18
19
  LEVELS = {}
19
20
  LNAMES = {}
20
21
  # :startdoc:
@@ -75,7 +76,7 @@ module Logging
75
76
  # full description of the :pattern and :date_pattern formatting strings.
76
77
  #
77
78
  def logger( *args )
78
- opts = args.pop if Hash === args.last
79
+ opts = args.pop if args.last.instance_of?(Hash)
79
80
  opts ||= Hash.new
80
81
 
81
82
  dev = args.shift
@@ -100,10 +101,10 @@ module Logging
100
101
  layout = ::Logging::Layouts::Pattern.new(l_opts)
101
102
 
102
103
  a_opts = Hash.new
103
- a_opts[:size] = size if Fixnum === size
104
- a_opts[:age] = age if String === age
105
- a_opts[:keep] = keep if Fixnum === keep
106
- a_opts[:filename] = dev if String === dev
104
+ a_opts[:size] = size if size.instance_of?(Fixnum)
105
+ a_opts[:age] = age if age.instance_of?(String)
106
+ a_opts[:keep] = keep if keep.instance_of?(Fixnum)
107
+ a_opts[:filename] = dev if dev.instance_of?(String)
107
108
  a_opts[:layout] = layout
108
109
  a_opts.merge! opts
109
110
 
@@ -116,7 +117,7 @@ module Logging
116
117
  end
117
118
 
118
119
  logger = ::Logging::Logger.new(name)
119
- logger.add appender
120
+ logger.add_appenders appender
120
121
  logger.additive = false
121
122
 
122
123
  class << logger
@@ -132,7 +133,7 @@ module Logging
132
133
  end
133
134
 
134
135
  # call-seq:
135
- # Logging.define_levels( levels )
136
+ # Logging.init( levels )
136
137
  #
137
138
  # Defines the levels available to the loggers. The _levels_ is an array
138
139
  # of strings and symbols. Each element in the array is downcased and
@@ -154,7 +155,7 @@ module Logging
154
155
  #
155
156
  # Example:
156
157
  #
157
- # Logging.define_levels :debug, :info, :warn, :error, :fatal
158
+ # Logging.init :debug, :info, :warn, :error, :fatal
158
159
  # log = Logging::Logger['my logger']
159
160
  # log.level = :warn
160
161
  # log.warn 'Danger! Danger! Will Robinson'
@@ -162,14 +163,14 @@ module Logging
162
163
  #
163
164
  # or
164
165
  #
165
- # Logging.define_levels %w(DEBUG INFO NOTICE WARNING ERR CRIT ALERT EMERG)
166
+ # Logging.init %w(DEBUG INFO NOTICE WARNING ERR CRIT ALERT EMERG)
166
167
  # log = Logging::Logger['syslog']
167
168
  # log.level = :notice
168
169
  # log.warning 'This is your first warning'
169
170
  # log.info 'Just FYI' # => not logged
170
171
  #
171
- def define_levels( *args )
172
- return nil if args.empty?
172
+ def init( *args )
173
+ args = %w(debug info warn error fatal) if args.empty?
173
174
 
174
175
  args.flatten!
175
176
  levels = ::Logging::LEVELS.clear
@@ -1,4 +1,4 @@
1
- # $Id: email.rb 85 2008-02-06 17:59:00Z tim_pease $
1
+ # $Id: email.rb 96 2008-02-10 18:18:10Z tim_pease $
2
2
 
3
3
  require 'net/smtp'
4
4
  require 'time' # get rfc822 time format
@@ -87,7 +87,7 @@ class Email < ::Logging::Appender
87
87
  #
88
88
  def write( event )
89
89
  immediate = false
90
- str = if ::Logging::LogEvent === event
90
+ str = if event.instance_of?(::Logging::LogEvent)
91
91
  immediate = @immediate[event.level]
92
92
  @layout.format(event)
93
93
  else
@@ -1,4 +1,4 @@
1
- # $Id: growl.rb 85 2008-02-06 17:59:00Z tim_pease $
1
+ # $Id: growl.rb 96 2008-02-10 18:18:10Z tim_pease $
2
2
 
3
3
  module Logging::Appenders
4
4
 
@@ -77,7 +77,7 @@ module Logging::Appenders
77
77
  def write( event )
78
78
  title = ''
79
79
  priority = 0
80
- message = if ::Logging::LogEvent === event
80
+ message = if event.instance_of?(::Logging::LogEvent)
81
81
  priority = @map[event.level]
82
82
  @layout.format(event)
83
83
  else
@@ -1,4 +1,4 @@
1
- # $Id: io.rb 85 2008-02-06 17:59:00Z tim_pease $
1
+ # $Id: io.rb 96 2008-02-10 18:18:10Z tim_pease $
2
2
 
3
3
  module Logging::Appenders
4
4
 
@@ -65,7 +65,8 @@ module Logging::Appenders
65
65
  #
66
66
  def write( event )
67
67
  begin
68
- str = ::Logging::LogEvent === event ? @layout.format(event) : event.to_s
68
+ str = event.instance_of?(::Logging::LogEvent) ?
69
+ @layout.format(event) : event.to_s
69
70
  return if str.empty?
70
71
  @io.print str
71
72
  rescue IOError
@@ -1,4 +1,4 @@
1
- # $Id: rolling_file.rb 85 2008-02-06 17:59:00Z tim_pease $
1
+ # $Id: rolling_file.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  begin
4
4
  require 'lockfile'
@@ -7,6 +7,14 @@ rescue LoadError
7
7
  require 'lockfile'
8
8
  end
9
9
 
10
+ # FIXME: bug when truncating a rolling file on create
11
+ # If there is not log file in existence, it is created and then
12
+ # immediately rolld resulting in two log files
13
+ #
14
+ # This appears to be because of rails -- creating the logfile and
15
+ # then we replace the default logger with a rolling file appender. It
16
+ # sees the already existing log file and happily rolls it right over.
17
+
10
18
  module Logging::Appenders
11
19
 
12
20
  # An appender that writes to a file and ensures that the file size or age
@@ -79,7 +87,7 @@ module Logging::Appenders
79
87
  @keep = opts.getopt(:keep, :as => Integer)
80
88
  @size = opts.getopt(:size, :as => Integer)
81
89
 
82
- @lockfile = if opts.getopt(:safe, false) and !(%r/win32/ =~ RUBY_PLATFORM)
90
+ @lockfile = if opts.getopt(:safe, false) and !::Logging::WIN32
83
91
  Lockfile.new(
84
92
  @fn + '.lck',
85
93
  :retries => 1,
@@ -161,7 +169,8 @@ module Logging::Appenders
161
169
  # maximum age.
162
170
  #
163
171
  def write( event )
164
- str = ::Logging::LogEvent === event ? @layout.format(event) : event.to_s
172
+ str = event.instance_of?(::Logging::LogEvent) ?
173
+ @layout.format(event) : event.to_s
165
174
  return if str.empty?
166
175
 
167
176
  check_logfile
@@ -1,4 +1,4 @@
1
- # $Id: syslog.rb 85 2008-02-06 17:59:00Z tim_pease $
1
+ # $Id: syslog.rb 96 2008-02-10 18:18:10Z tim_pease $
2
2
 
3
3
  begin
4
4
  require 'syslog'
@@ -165,7 +165,7 @@ module Logging::Appenders
165
165
  #
166
166
  def write( event )
167
167
  pri = LOG_DEBUG
168
- message = if ::Logging::LogEvent === event
168
+ message = if event.instance_of?(::Logging::LogEvent)
169
169
  pri = @map[event.level]
170
170
  @layout.format(event)
171
171
  else
@@ -1,4 +1,4 @@
1
- # $Id: yaml_configurator.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: yaml_configurator.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require 'yaml'
4
4
 
@@ -89,7 +89,7 @@ module Config
89
89
 
90
90
  # define levels
91
91
  levels = config['define_levels']
92
- ::Logging.define_levels(levels) unless levels.nil?
92
+ ::Logging.init(levels) unless levels.nil?
93
93
 
94
94
  # format as
95
95
  format = config['format_as']
@@ -1,4 +1,4 @@
1
- # $Id: logger.rb 77 2007-12-30 02:21:33Z tim_pease $
1
+ # $Id: logger.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require 'thread'
4
4
 
@@ -16,8 +16,8 @@ module Logging
16
16
  # Example:
17
17
  #
18
18
  # log = Logging::Logger['my logger']
19
- # log.add( Logging::Appenders::Stdout.new ) # append to STDOUT
20
- # log.level = :info # log 'info' and above
19
+ # log.add_appenders( Logging::Appender.stdout ) # append to STDOUT
20
+ # log.level = :info # log 'info' and above
21
21
  #
22
22
  # log.info 'starting foo operation'
23
23
  # ...
@@ -179,6 +179,39 @@ module Logging
179
179
  @parent << msg if @additive
180
180
  end
181
181
 
182
+ # call-seq:
183
+ # add( severity, message = nil ) {block}
184
+ #
185
+ # Log a message if the given severity is high enough. This is the generic
186
+ # logging method. Users will be more inclined to use #debug, #info, #warn,
187
+ # #error, and #fatal.
188
+ #
189
+ # <b>Message format</b>: +message+ can be any object, but it has to be
190
+ # converted to a String in order to log it. The Logging::format_as
191
+ # method is used to determine how objects chould be converted to
192
+ # strings. Generally, +inspect+ is used.
193
+ #
194
+ # A special case is an +Exception+ object, which will be printed in
195
+ # detail, including message, class, and backtrace.
196
+ #
197
+ # If a _message_ is not given, then the return value from the block is
198
+ # used as the message to log. This is useful when creating the actual
199
+ # message is an expensive operation. This allows the logger to check the
200
+ # severity against the configured level before actually constructing the
201
+ # message.
202
+ #
203
+ # This method returns +true+ if the message was logged, and +false+ is
204
+ # returned if the message was not logged.
205
+ #
206
+ def add( lvl, data = nil )
207
+ lvl = Integer(lvl)
208
+ return false if lvl < level
209
+
210
+ data = yield if block_given?
211
+ log_event(::Logging::LogEvent.new(@name, lvl, data, @trace))
212
+ true
213
+ end
214
+
182
215
  # call-seq:
183
216
  # additive = true
184
217
  #
@@ -263,52 +296,52 @@ module Logging
263
296
  #
264
297
  def appenders=( args )
265
298
  @appenders.clear
266
- add(*args) unless args.nil?
299
+ add_appenders(*args) unless args.nil?
267
300
  end
268
301
 
269
302
  # call-seq:
270
- # add( appenders )
303
+ # add_appenders( appenders )
271
304
  #
272
305
  # Add the given _appenders_ to the list of appenders, where _appenders_
273
306
  # can be either a single appender or an array of appenders.
274
307
  #
275
- def add( *args )
276
- args.each do |arg|
277
- unless arg.kind_of? ::Logging::Appender
278
- raise TypeError,
279
- "#{arg.inspect} is not a kind of 'Logging::Appender'"
280
- end
281
- @appenders << arg unless @appenders.include? arg
308
+ def add_appenders( *args )
309
+ args.flatten.each do |arg|
310
+ o = arg.kind_of?(::Logging::Appender) ? arg : ::Logging::Appender[arg]
311
+ raise ArgumentError, "unknown appender '#{arg}'" if o.nil?
312
+ @appenders << o unless @appenders.include?(o)
282
313
  end
314
+ self
283
315
  end
284
316
 
285
317
  # call-seq:
286
- # remove( appenders )
318
+ # remove_appenders( appenders )
287
319
  #
288
320
  # Remove the given _appenders_ from the list of appenders. The appenders
289
321
  # to remove can be identified either by name using a +String+ or by
290
322
  # passing the appender instance. _appenders_ can be a single appender or
291
323
  # an array of appenders.
292
324
  #
293
- def remove( *args )
294
- args.each do |arg|
325
+ def remove_appenders( *args )
326
+ args.flatten.each do |arg|
295
327
  @appenders.delete_if do |a|
296
328
  case arg
297
329
  when String; arg == a.name
298
330
  when ::Logging::Appender; arg.object_id == a.object_id
299
331
  else
300
- raise TypeError, "#{arg.inspect} is not a 'Logging::Appender'"
332
+ raise ArgumentError, "#{arg.inspect} is not a 'Logging::Appender'"
301
333
  end
302
334
  end
303
335
  end
336
+ self
304
337
  end
305
338
 
306
339
  # call-seq:
307
- # clear
340
+ # clear_appenders
308
341
  #
309
342
  # Remove all appenders from this logger.
310
343
  #
311
- def clear( ) @appenders.clear end
344
+ def clear_appenders( ) @appenders.clear end
312
345
 
313
346
 
314
347
  protected
@@ -1,4 +1,4 @@
1
- # $Id: root_logger.rb 77 2007-12-30 02:21:33Z tim_pease $
1
+ # $Id: root_logger.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  module Logging
4
4
 
@@ -20,9 +20,7 @@ module Logging
20
20
  # once when the +Repository+ singleton instance is created.
21
21
  #
22
22
  def initialize( )
23
- unless ::Logging.const_defined? 'MAX_LEVEL_LENGTH'
24
- ::Logging.define_levels %w(debug info warn error fatal)
25
- end
23
+ ::Logging.init unless ::Logging.const_defined? 'MAX_LEVEL_LENGTH'
26
24
 
27
25
  @name = 'root'
28
26
  @appenders = []
data/lib/logging/utils.rb CHANGED
@@ -1,4 +1,4 @@
1
- # $Id: utils.rb 65 2007-12-23 04:48:55Z tim_pease $
1
+ # $Id: utils.rb 96 2008-02-10 18:18:10Z tim_pease $
2
2
 
3
3
  class Hash
4
4
 
@@ -20,7 +20,7 @@ class Hash
20
20
  # If the value is +nil+, then no converstion will be performed.
21
21
  #
22
22
  def getopt( *args )
23
- opts = Hash === args.last ? args.pop : {}
23
+ opts = args.last.instance_of?(Hash) ? args.pop : {}
24
24
  key, default = args
25
25
 
26
26
  val = if has_key?(key); self[key]
@@ -1,4 +1,4 @@
1
- # $Id: test_email.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_email.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[.. setup])
4
4
  require 'flexmock'
@@ -12,7 +12,7 @@ module TestAppenders
12
12
 
13
13
  def setup
14
14
  super
15
- ::Logging.define_levels %w(debug info warn error fatal)
15
+ ::Logging.init
16
16
  @levels = ::Logging::LEVELS
17
17
 
18
18
  flexmock(Net::SMTP).new_instances do |m|
@@ -1,4 +1,4 @@
1
- # $Id: test_file.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_file.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[.. setup])
4
4
 
@@ -12,7 +12,7 @@ module TestAppenders
12
12
 
13
13
  def setup
14
14
  super
15
- ::Logging.define_levels %w(debug info warn error fatal)
15
+ ::Logging.init
16
16
 
17
17
  FileUtils.mkdir [File.join(TMP, 'dir'), File.join(TMP, 'uw_dir')]
18
18
  FileUtils.chmod 0555, File.join(TMP, 'uw_dir')
@@ -1,4 +1,4 @@
1
- # $Id: test_growl.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_growl.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[.. setup])
4
4
  require 'flexmock'
@@ -12,7 +12,7 @@ module TestAppenders
12
12
 
13
13
  def setup
14
14
  super
15
- ::Logging.define_levels %w(debug info warn error fatal)
15
+ ::Logging.init
16
16
  @levels = ::Logging::LEVELS
17
17
 
18
18
  @appender = ::Logging::Appenders::Growl.new('growl',
@@ -1,4 +1,4 @@
1
- # $Id: test_io.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_io.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[.. setup])
4
4
 
@@ -10,7 +10,7 @@ module TestAppenders
10
10
 
11
11
  def setup
12
12
  super
13
- ::Logging.define_levels %w(debug info warn error fatal)
13
+ ::Logging.init
14
14
  @levels = ::Logging::LEVELS
15
15
 
16
16
  @sio = StringIO.new
@@ -1,4 +1,4 @@
1
- # $Id: test_rolling_file.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_rolling_file.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[.. setup])
4
4
 
@@ -12,7 +12,7 @@ module TestAppenders
12
12
 
13
13
  def setup
14
14
  super
15
- ::Logging.define_levels %w(debug info warn error fatal)
15
+ ::Logging.init
16
16
 
17
17
  @fn = File.join(TMP, 'test.log')
18
18
  @fn_fmt = File.join(TMP, 'test.%d.log')
@@ -1,4 +1,4 @@
1
- # $Id: test_syslog.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_syslog.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[.. setup])
4
4
 
@@ -13,7 +13,7 @@ module TestAppenders
13
13
 
14
14
  def setup
15
15
  super
16
- ::Logging.define_levels %w(debug info warn error fatal)
16
+ ::Logging.init
17
17
  @levels = ::Logging::LEVELS
18
18
  end
19
19
 
@@ -1,4 +1,4 @@
1
- # $Id: test_basic.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_basic.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[.. setup])
4
4
 
@@ -10,7 +10,7 @@ module TestLayouts
10
10
 
11
11
  def setup
12
12
  super
13
- ::Logging.define_levels %w(debug info warn error fatal)
13
+ ::Logging.init
14
14
  @layout = ::Logging::Layouts::Basic.new
15
15
  @levels = ::Logging::LEVELS
16
16
  end
@@ -1,4 +1,4 @@
1
- # $Id: test_pattern.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_pattern.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[.. setup])
4
4
 
@@ -10,7 +10,7 @@ module TestLayouts
10
10
 
11
11
  def setup
12
12
  super
13
- ::Logging.define_levels %w(debug info warn error fatal)
13
+ ::Logging.init
14
14
  @layout = ::Logging::Layouts::Pattern.new
15
15
  @levels = ::Logging::LEVELS
16
16
  @date_fmt = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'
@@ -1,4 +1,4 @@
1
- # $Id: test_appender.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_appender.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[setup])
4
4
 
@@ -10,7 +10,7 @@ module TestLogging
10
10
  def setup
11
11
  super
12
12
 
13
- ::Logging.define_levels %w(debug info warn error fatal)
13
+ ::Logging.init
14
14
  @levels = ::Logging::LEVELS
15
15
  @event = ::Logging::LogEvent.new('logger', @levels['debug'],
16
16
  'message', false)
@@ -21,7 +21,8 @@ module TestLogging
21
21
  ary = []
22
22
  @appender.instance_variable_set :@ary, ary
23
23
  def @appender.write( event )
24
- str = ::Logging::LogEvent === event ? @layout.format(event) : event.to_s
24
+ str = event.instance_of?(::Logging::LogEvent) ?
25
+ @layout.format(event) : event.to_s
25
26
  @ary << str
26
27
  end
27
28
 
@@ -72,7 +73,8 @@ module TestLogging
72
73
  ary = []
73
74
  @appender.instance_variable_set :@ary, ary
74
75
  def @appender.write( event )
75
- str = ::Logging::LogEvent === event ? @layout.format(event) : event.to_s
76
+ str = event.instance_of?(::Logging::LogEvent) ?
77
+ @layout.format(event) : event.to_s
76
78
  @ary << str
77
79
  end
78
80
 
@@ -1,4 +1,4 @@
1
- # $Id: test_log_event.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_log_event.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[setup])
4
4
 
@@ -12,7 +12,7 @@ module TestLogging
12
12
 
13
13
  @appender = EventAppender.new('test')
14
14
  @logger = ::Logging::Logger['TestLogger']
15
- @logger.add @appender
15
+ @logger.add_appenders @appender
16
16
 
17
17
  @logger.info 'message 1'
18
18
  @event = @appender.event
data/test/test_logger.rb CHANGED
@@ -1,4 +1,4 @@
1
- # $Id: test_logger.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_logger.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[setup])
4
4
 
@@ -17,37 +17,101 @@ module TestLogging
17
17
  end
18
18
 
19
19
  def test_add
20
+ root = ::Logging::Logger[:root]
21
+ root.level = 'info'
22
+
23
+ a1 = SioAppender.new 'a1'
24
+ a2 = SioAppender.new 'a2'
25
+ log = ::Logging::Logger.new 'A Logger'
26
+
27
+ root.add_appenders a1
28
+ assert_nil a1.readline
29
+ assert_nil a2.readline
30
+
31
+ log.add(0, 'this should NOT be logged')
32
+ assert_nil a1.readline
33
+ assert_nil a2.readline
34
+
35
+ log.add(1, 'this should be logged')
36
+ assert_equal " INFO A Logger : this should be logged\n", a1.readline
37
+ assert_nil a1.readline
38
+ assert_nil a2.readline
39
+
40
+ log.add(2,[1,2,3,4])
41
+ assert_equal " WARN A Logger : <Array> #{[1,2,3,4]}\n", a1.readline
42
+ assert_nil a1.readline
43
+ assert_nil a2.readline
44
+
45
+ log.add_appenders a2
46
+ log.add(3, 'an error has occurred')
47
+ assert_equal "ERROR A Logger : an error has occurred\n", a1.readline
48
+ assert_equal "ERROR A Logger : an error has occurred\n", a2.readline
49
+ assert_nil a1.readline
50
+ assert_nil a2.readline
51
+
52
+ log.additive = false
53
+ log.add(3, 'another error has occurred')
54
+ assert_equal "ERROR A Logger : another error has occurred\n", a2.readline
55
+ assert_nil a1.readline
56
+ assert_nil a2.readline
57
+
58
+ log.add_appenders a1
59
+ log.add(4, 'fatal exception')
60
+ assert_equal "FATAL A Logger : fatal exception\n", a1.readline
61
+ assert_equal "FATAL A Logger : fatal exception\n", a2.readline
62
+ assert_nil a1.readline
63
+ assert_nil a2.readline
64
+
65
+
66
+ log.level = :warn
67
+ log.add(2) do
68
+ str = 'a string of data'
69
+ str
70
+ end
71
+ assert_equal " WARN A Logger : a string of data\n", a1.readline
72
+ assert_equal " WARN A Logger : a string of data\n", a2.readline
73
+ assert_nil a1.readline
74
+ assert_nil a2.readline
75
+
76
+ log.add(1) do
77
+ rb_raise(RuntimeError, "this block should not be executed")
78
+ end
79
+ assert_nil a1.readline
80
+ assert_nil a2.readline
81
+ end
82
+
83
+ def test_add_appenders
20
84
  log = ::Logging::Logger.new 'A'
21
85
 
22
86
  appenders = lambda {log.instance_variable_get :@appenders}
23
87
  assert_equal [], appenders[]
24
88
 
25
- assert_raise(TypeError) {log.add Object.new}
26
- assert_raise(TypeError) {log.add 'not an appender'}
89
+ assert_raise(ArgumentError) {log.add_appenders Object.new}
90
+ assert_raise(ArgumentError) {log.add_appenders 'not an appender'}
27
91
 
28
92
  a = ::Logging::Appender.new 'test_appender_1'
29
93
  b = ::Logging::Appender.new 'test_appender_2'
30
94
  c = ::Logging::Appender.new 'test_appender_3'
31
95
 
32
- log.add a
96
+ log.add_appenders a
33
97
  assert_equal [a], appenders[]
34
98
 
35
- log.add a
99
+ log.add_appenders a
36
100
  assert_equal [a], appenders[]
37
101
 
38
- log.add b
102
+ log.add_appenders b
39
103
  assert_equal [a,b], appenders[]
40
104
 
41
- log.add c
105
+ log.add_appenders c
42
106
  assert_equal [a,b,c], appenders[]
43
107
 
44
- log.add a, c
108
+ log.add_appenders a, c
45
109
  assert_equal [a,b,c], appenders[]
46
110
 
47
- log.clear
111
+ log.clear_appenders
48
112
  assert_equal [], appenders[]
49
113
 
50
- log.add a, c
114
+ log.add_appenders a, c
51
115
  assert_equal [a,c], appenders[]
52
116
  end
53
117
 
@@ -90,8 +154,8 @@ module TestLogging
90
154
  appenders = lambda {log.instance_variable_get :@appenders}
91
155
  assert_equal [], appenders[]
92
156
 
93
- assert_raise(TypeError) {log.appenders = Object.new}
94
- assert_raise(TypeError) {log.appenders = 'not an appender'}
157
+ assert_raise(ArgumentError) {log.appenders = Object.new}
158
+ assert_raise(ArgumentError) {log.appenders = 'not an appender'}
95
159
 
96
160
  a = ::Logging::Appender.new 'test_appender_1'
97
161
  b = ::Logging::Appender.new 'test_appender_2'
@@ -108,6 +172,11 @@ module TestLogging
108
172
 
109
173
  log.appenders = nil
110
174
  assert_equal [], appenders[]
175
+
176
+ log.appenders = %w[test_appender_1 test_appender_3]
177
+ assert_equal [a,c], appenders[]
178
+
179
+ assert_raise(ArgumentError) {log.appenders = 'unknown'}
111
180
  end
112
181
 
113
182
  def test_class_aref
@@ -127,7 +196,7 @@ module TestLogging
127
196
  assert_same root, ::Logging::Logger.root
128
197
  end
129
198
 
130
- def test_clear
199
+ def test_clear_appenders
131
200
  log = ::Logging::Logger.new 'Elliott'
132
201
 
133
202
  appenders = lambda {log.instance_variable_get :@appenders}
@@ -137,10 +206,10 @@ module TestLogging
137
206
  b = ::Logging::Appender.new 'test_appender_2'
138
207
  c = ::Logging::Appender.new 'test_appender_3'
139
208
 
140
- log.add a, b, c
209
+ log.add_appenders a, b, c
141
210
  assert_equal [a,b,c], appenders[]
142
211
 
143
- log.clear
212
+ log.clear_appenders
144
213
  assert_equal [], appenders[]
145
214
  end
146
215
 
@@ -149,7 +218,7 @@ module TestLogging
149
218
  a2 = SioAppender.new 'a2'
150
219
  log = ::Logging::Logger.new 'A'
151
220
 
152
- ::Logging::Logger[:root].add a1
221
+ ::Logging::Logger[:root].add_appenders a1
153
222
  assert_nil a1.readline
154
223
  assert_nil a2.readline
155
224
 
@@ -165,7 +234,7 @@ module TestLogging
165
234
  assert_nil a1.readline
166
235
  assert_nil a2.readline
167
236
 
168
- log.add a2
237
+ log.add_appenders a2
169
238
  log << "this is line four of the log file\n"
170
239
  assert_equal "this is line four of the log file\n", a1.readline
171
240
  assert_equal "this is line four of the log file\n", a2.readline
@@ -178,7 +247,7 @@ module TestLogging
178
247
  assert_nil a1.readline
179
248
  assert_nil a2.readline
180
249
 
181
- log.add a1
250
+ log.add_appenders a1
182
251
  log << "this is line six of the log file\n"
183
252
  assert_equal "this is line six of the log file\n", a1.readline
184
253
  assert_equal "this is line six of the log file\n", a2.readline
@@ -272,7 +341,7 @@ module TestLogging
272
341
  a2 = SioAppender.new 'a2'
273
342
  log = ::Logging::Logger.new 'A Logger'
274
343
 
275
- root.add a1
344
+ root.add_appenders a1
276
345
  assert_nil a1.readline
277
346
  assert_nil a2.readline
278
347
 
@@ -290,7 +359,7 @@ module TestLogging
290
359
  assert_nil a1.readline
291
360
  assert_nil a2.readline
292
361
 
293
- log.add a2
362
+ log.add_appenders a2
294
363
  log.error 'an error has occurred'
295
364
  assert_equal "ERROR A Logger : an error has occurred\n", a1.readline
296
365
  assert_equal "ERROR A Logger : an error has occurred\n", a2.readline
@@ -303,7 +372,7 @@ module TestLogging
303
372
  assert_nil a1.readline
304
373
  assert_nil a2.readline
305
374
 
306
- log.add a1
375
+ log.add_appenders a1
307
376
  log.fatal 'fatal exception'
308
377
  assert_equal "FATAL A Logger : fatal exception\n", a1.readline
309
378
  assert_equal "FATAL A Logger : fatal exception\n", a2.readline
@@ -405,7 +474,7 @@ module TestLogging
405
474
  assert_same logger['A::B::C::E'], logger['A::B::C::E::G'].parent
406
475
  end
407
476
 
408
- def test_remove
477
+ def test_remove_appenders
409
478
  log = ::Logging::Logger['X']
410
479
 
411
480
  appenders = lambda {log.instance_variable_get :@appenders}
@@ -415,28 +484,28 @@ module TestLogging
415
484
  b = ::Logging::Appender.new 'test_appender_2'
416
485
  c = ::Logging::Appender.new 'test_appender_3'
417
486
 
418
- log.add a, b, c
487
+ log.add_appenders a, b, c
419
488
  assert_equal [a,b,c], appenders[]
420
489
 
421
- assert_raise(TypeError) {log.remove Object.new}
422
- assert_raise(TypeError) {log.remove 10}
490
+ assert_raise(ArgumentError) {log.remove_appenders Object.new}
491
+ assert_raise(ArgumentError) {log.remove_appenders 10}
423
492
 
424
- log.remove b
493
+ log.remove_appenders b
425
494
  assert_equal [a,c], appenders[]
426
495
 
427
- log.remove 'test_appender_1'
496
+ log.remove_appenders 'test_appender_1'
428
497
  assert_equal [c], appenders[]
429
498
 
430
- log.remove c
499
+ log.remove_appenders c
431
500
  assert_equal [], appenders[]
432
501
 
433
- log.remove a, b, c
502
+ log.remove_appenders a, b, c
434
503
  assert_equal [], appenders[]
435
504
 
436
- log.add a, b, c
505
+ log.add_appenders a, b, c
437
506
  assert_equal [a,b,c], appenders[]
438
507
 
439
- log.remove a, c
508
+ log.remove_appenders a, c
440
509
  assert_equal [b], appenders[]
441
510
  end
442
511
 
data/test/test_logging.rb CHANGED
@@ -1,4 +1,4 @@
1
- # $Id: test_logging.rb 88 2008-02-08 18:47:36Z tim_pease $
1
+ # $Id: test_logging.rb 97 2008-02-13 00:32:18Z tim_pease $
2
2
 
3
3
  require File.join(File.dirname(__FILE__), %w[setup])
4
4
 
@@ -118,7 +118,7 @@ module TestLogging
118
118
  assert_equal 3, Dir.glob(@glob).length
119
119
  end
120
120
 
121
- def test_define_levels_default
121
+ def test_init_default
122
122
  empty = {}
123
123
  assert_equal empty, @levels
124
124
  assert_equal empty, @lnames
@@ -143,15 +143,15 @@ module TestLogging
143
143
  assert_equal 'FATAL', @lnames[4]
144
144
  end
145
145
 
146
- def test_define_levels_special
146
+ def test_init_special
147
147
  empty = {}
148
148
  assert_equal empty, @levels
149
149
  assert_equal empty, @lnames
150
150
  assert_same false, ::Logging.const_defined?(:MAX_LEVEL_LENGTH)
151
151
 
152
- assert_raise(ArgumentError) {::Logging.define_levels(1, 2, 3, 4)}
152
+ assert_raise(ArgumentError) {::Logging.init(1, 2, 3, 4)}
153
153
 
154
- ::Logging.define_levels :one, 'two', :THREE, 'FoUr', :sIx
154
+ ::Logging.init :one, 'two', :THREE, 'FoUr', :sIx
155
155
 
156
156
  assert_equal 5, @levels.length
157
157
  assert_equal 5, @lnames.length
@@ -170,13 +170,13 @@ module TestLogging
170
170
  assert_equal 'SIX', @lnames[4]
171
171
  end
172
172
 
173
- def test_define_levels_all_off
173
+ def test_init_all_off
174
174
  empty = {}
175
175
  assert_equal empty, @levels
176
176
  assert_equal empty, @lnames
177
177
  assert_same false, ::Logging.const_defined?(:MAX_LEVEL_LENGTH)
178
178
 
179
- ::Logging.define_levels %w(a b all c off d)
179
+ ::Logging.init %w(a b all c off d)
180
180
 
181
181
  assert_equal 4, @levels.length
182
182
  assert_equal 4, @lnames.length
@@ -235,6 +235,15 @@ module TestLogging
235
235
  remove_const[:OBJ_FORMAT]
236
236
  end
237
237
 
238
+ def test_path
239
+ path = ::Logging.path(*%w[one two three])
240
+ assert_match %r/one\/two\/three$/, path
241
+ end
242
+
243
+ def test_version
244
+ assert_match %r/\d+\.\d+\.\d+/, ::Logging.version
245
+ end
246
+
238
247
  end # class TestLogging
239
248
  end # module TestLogging
240
249
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Pease
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-02-08 00:00:00 -07:00
12
+ date: 2008-02-12 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency