TwP-logging 0.9.8 → 0.9.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,5 @@
1
1
 
2
- module Logging
3
- module Layouts
2
+ module Logging::Layouts
4
3
 
5
4
  # A flexible layout configurable with pattern string.
6
5
  #
@@ -22,7 +21,7 @@ module Layouts
22
21
  # Let the conversion pattern be "%-5l [%c]: %m\n" and assume that the
23
22
  # logging environment was set to use a Pattern layout. Then the statements
24
23
  #
25
- # root = Logging::Logger[:root]
24
+ # root = Logging.logger[:root]
26
25
  # root.debug("Message 1")
27
26
  # root.warn("Message 2")
28
27
  #
@@ -57,9 +56,9 @@ module Layouts
57
56
  # [t] Used to output the object ID of the thread that generated the
58
57
  # log event.
59
58
  # [T] Used to output the name of the thread that generated the log event.
60
- # Name can be specified using Thread.current[:name] notation. Output empty
61
- # string if name not specified. This options helps to create more human
62
- # readable output for multithread application log.
59
+ # Name can be specified using Thread.current[:name] notation. Output
60
+ # empty string if name not specified. This option helps to create
61
+ # more human readable output for multithread application logs.
63
62
  # [%] The sequence '%%' outputs a single percent sign.
64
63
  #
65
64
  # The directives F, L, and M will only work if the Logger generating the
@@ -290,7 +289,6 @@ module Layouts
290
289
  # :startdoc:
291
290
 
292
291
  end # class Pattern
293
- end # module Layouts
294
- end # module Logging
292
+ end # module Logging::Layouts
295
293
 
296
294
  # EOF
@@ -0,0 +1,47 @@
1
+
2
+ module Logging
3
+ module Layouts
4
+
5
+ # Accessor / Factory for the Basic layout.
6
+ #
7
+ def basic( *args )
8
+ return ::Logging::Layouts::Basic if args.empty?
9
+ ::Logging::Layouts::Basic.new(*args)
10
+ end
11
+
12
+ # Accessor / Factory for the Pattern layout.
13
+ #
14
+ def pattern( *args )
15
+ return ::Logging::Layouts::Pattern if args.empty?
16
+ ::Logging::Layouts::Pattern.new(*args)
17
+ end
18
+
19
+ # Accessor for the Parseable layout.
20
+ #
21
+ def parseable
22
+ ::Logging::Layouts::Parseable
23
+ end
24
+
25
+ # Factory for the Parseable layout using JSON formatting.
26
+ #
27
+ def json( *args )
28
+ ::Logging::Layouts::Parseable.json(*args)
29
+ end
30
+
31
+ # Factory for the Parseable layout using YAML formatting.
32
+ #
33
+ def yaml( *args )
34
+ ::Logging::Layouts::Parseable.yaml(*args)
35
+ end
36
+
37
+ extend self
38
+ end # module Layouts
39
+ end # module Logging
40
+
41
+
42
+ %w[basic parseable pattern].
43
+ each do |fn|
44
+ require ::Logging.libpath('logging', 'layouts', fn)
45
+ end
46
+
47
+ # EOF
@@ -37,7 +37,7 @@ module Logging
37
37
 
38
38
  m = CALLER_RGXP.match(t)
39
39
  @file = m[1]
40
- @line = m[2]
40
+ @line = Integer(m[2])
41
41
  @method = m[3] unless m[3].nil?
42
42
  end
43
43
  end
@@ -12,8 +12,8 @@ module Logging
12
12
  #
13
13
  # Example:
14
14
  #
15
- # log = Logging::Logger['my logger']
16
- # log.add_appenders( Logging::Appender.stdout ) # append to STDOUT
15
+ # log = Logging.logger['my logger']
16
+ # log.add_appenders( Logging.appenders.stdout ) # append to STDOUT
17
17
  # log.level = :info # log 'info' and above
18
18
  #
19
19
  # log.info 'starting foo operation'
@@ -312,8 +312,8 @@ module Logging
312
312
  #
313
313
  def add_appenders( *args )
314
314
  args.flatten.each do |arg|
315
- o = arg.kind_of?(::Logging::Appender) ? arg : ::Logging::Appender[arg]
316
- raise ArgumentError, "unknown appender '#{arg}'" if o.nil?
315
+ o = arg.kind_of?(::Logging::Appender) ? arg : ::Logging::Appenders[arg.to_s]
316
+ raise ArgumentError, "unknown appender #{arg.inspect}" if o.nil?
317
317
  @appenders << o unless @appenders.include?(o)
318
318
  end
319
319
  self
data/lib/logging.rb CHANGED
@@ -3,7 +3,24 @@
3
3
  # Used to prevent the class/module from being loaded more than once
4
4
  unless defined? Logging
5
5
 
6
+ require 'yaml'
7
+ require 'stringio'
6
8
  require 'thread'
9
+
10
+ begin
11
+ require 'lockfile'
12
+ rescue LoadError
13
+ retry if require 'rubygems'
14
+ raise
15
+ end
16
+
17
+ begin
18
+ require 'syslog'
19
+ HAVE_SYSLOG = true
20
+ rescue LoadError
21
+ HAVE_SYSLOG = false
22
+ end
23
+
7
24
  begin require 'fastthread'; rescue LoadError; end
8
25
 
9
26
  # TODO: Windows Log Service appender
@@ -13,7 +30,7 @@ begin require 'fastthread'; rescue LoadError; end
13
30
  module Logging
14
31
 
15
32
  # :stopdoc:
16
- VERSION = '0.9.8'
33
+ VERSION = '0.9.8.1'
17
34
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
18
35
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
19
36
  WIN32 = %r/djgpp|(cyg|ms|bcc)win|mingw/ =~ RUBY_PLATFORM
@@ -85,6 +102,8 @@ module Logging
85
102
  # full description of the :pattern and :date_pattern formatting strings.
86
103
  #
87
104
  def logger( *args )
105
+ return ::Logging::Logger if args.empty?
106
+
88
107
  opts = args.pop if args.last.instance_of?(Hash)
89
108
  opts ||= Hash.new
90
109
 
@@ -141,6 +160,18 @@ module Logging
141
160
  logger
142
161
  end
143
162
 
163
+ # Access to the layouts.
164
+ #
165
+ def layouts
166
+ ::Logging::Layouts
167
+ end
168
+
169
+ # Access to the appenders.
170
+ #
171
+ def appenders
172
+ ::Logging::Appenders
173
+ end
174
+
144
175
  # call-seq:
145
176
  # Logging.init( levels )
146
177
  #
@@ -182,8 +213,8 @@ module Logging
182
213
  args = %w(debug info warn error fatal) if args.empty?
183
214
 
184
215
  args.flatten!
185
- levels = ::Logging::LEVELS.clear
186
- names = ::Logging::LNAMES.clear
216
+ levels = LEVELS.clear
217
+ names = LNAMES.clear
187
218
 
188
219
  id = 0
189
220
  args.each do |lvl|
@@ -274,19 +305,6 @@ module Logging
274
305
  args.empty? ? PATH : ::File.join(PATH, args.flatten)
275
306
  end
276
307
 
277
- # Utility method used to rquire all files ending in .rb that lie in the
278
- # directory below this file that has the same name as the filename passed
279
- # in. Optionally, a specific _directory_ name can be passed in such that
280
- # the _filename_ does not have to be equivalent to the directory.
281
- #
282
- def require_all_libs_relative_to( fname, dir = nil )
283
- dir ||= ::File.basename(fname, '.*')
284
- search_me = ::File.expand_path(
285
- ::File.join(::File.dirname(fname), dir, '*.rb'))
286
-
287
- Dir.glob(search_me).sort.each {|rb| require rb}
288
- end
289
-
290
308
  # call-seq:
291
309
  # show_configuration( io = STDOUT, logger = 'root' )
292
310
  #
@@ -386,17 +404,29 @@ module Logging
386
404
  end
387
405
  end # module Logging
388
406
 
389
- Logging.require_all_libs_relative_to(__FILE__)
390
- Logging.require_all_libs_relative_to(__FILE__, 'logging/config')
407
+
408
+ require Logging.libpath(%w[logging utils])
409
+ require Logging.libpath(%w[logging appender])
410
+ require Logging.libpath(%w[logging layout])
411
+ require Logging.libpath(%w[logging log_event])
412
+ require Logging.libpath(%w[logging logger])
413
+ require Logging.libpath(%w[logging repository])
414
+ require Logging.libpath(%w[logging root_logger])
415
+ require Logging.libpath(%w[logging stats])
416
+ require Logging.libpath(%w[logging appenders])
417
+ require Logging.libpath(%w[logging layouts])
418
+
419
+ require Logging.libpath(%w[logging config configurator])
420
+ require Logging.libpath(%w[logging config yaml_configurator])
421
+
391
422
 
392
423
  # This exit handler will close all the appenders that exist in the system.
393
424
  # This is needed for closing IO streams and connections to the syslog server
394
425
  # or e-mail servers, etc.
395
426
  #
396
427
  at_exit {
397
- Logging::Appender.instance_variable_get(:@appenders).values.each do |ap|
398
- ap.close
399
- end
428
+ Logging.log_internal {'at_exit hook called - closing all appenders'}
429
+ Logging::Appenders.each {|appender| appender.close}
400
430
  }
401
431
 
402
432
  end # unless defined?
@@ -0,0 +1,34 @@
1
+
2
+ module Spec
3
+ module LoggingHelper
4
+
5
+ # Capture log messages from the Logging framework and make them
6
+ # available via a @log_output instance variable. The @log_output
7
+ # supports a readline method to access the log messags.
8
+ #
9
+ def capture_log_messages( opts = {} )
10
+ from = opts.getopt(:from, 'root')
11
+ to = opts.getopt(:to, '__rspec__')
12
+ exclusive = opts.getopt(:exclusive, true)
13
+
14
+ appender = Logging::Appenders[to] || Logging::Appenders::StringIo.new(to)
15
+ logger = Logging::Logger[from]
16
+ if exclusive
17
+ logger.appenders = appender
18
+ else
19
+ logger.add_appenders(appender)
20
+ end
21
+
22
+ before(:all) do
23
+ @log_output = Logging::Appenders[to]
24
+ end
25
+
26
+ before(:each) do
27
+ @log_output.reset
28
+ end
29
+ end
30
+
31
+ end # module LoggingHelper
32
+ end # module Spec
33
+
34
+ # EOF
@@ -9,20 +9,18 @@ module TestAppenders
9
9
 
10
10
  def setup
11
11
  super
12
- ::Logging.init
13
- @levels = ::Logging::LEVELS
14
-
15
- @appender = ::Logging::Appenders::StringIo.new(
12
+ @appender = Logging.appenders.string_io(
16
13
  'test_appender', :auto_flushing => 3, :immediate_at => :error
17
14
  )
15
+ @appender.clear
18
16
  @sio = @appender.sio
19
-
17
+ @levels = Logging::LEVELS
20
18
  begin readline rescue EOFError end
21
19
  end
22
20
 
23
21
  def test_append
24
- event = ::Logging::LogEvent.new('TestLogger', @levels['warn'],
25
- [1, 2, 3, 4], false)
22
+ event = Logging::LogEvent.new('TestLogger', @levels['warn'],
23
+ [1, 2, 3, 4], false)
26
24
  @appender.append event
27
25
  assert_nil(readline)
28
26
 
@@ -45,15 +43,15 @@ module TestAppenders
45
43
  def test_append_error
46
44
  # setup an internal logger to capture error messages from the IO
47
45
  # appender
48
- log = Logging::Appenders::StringIo.new('__internal_io')
49
- Logging::Logger[Logging].add_appenders(log)
50
- Logging::Logger[Logging].level = 'all'
46
+ log = Logging.appenders.string_io('__internal_io')
47
+ Logging.logger[Logging].add_appenders(log)
48
+ Logging.logger[Logging].level = 'all'
51
49
 
52
50
 
53
51
  # close the string IO object so we get an error
54
52
  @sio.close
55
- event = ::Logging::LogEvent.new('TestLogger', @levels['warn'],
56
- [1, 2, 3, 4], false)
53
+ event = Logging::LogEvent.new('TestLogger', @levels['warn'],
54
+ [1, 2, 3, 4], false)
57
55
  @appender.append event
58
56
  assert_nil(log.readline)
59
57
 
@@ -68,6 +66,16 @@ module TestAppenders
68
66
  assert_equal 5, @appender.level
69
67
  end
70
68
 
69
+ def test_auto_flushing
70
+ assert_raise(ArgumentError) {
71
+ @appender.auto_flushing = Object.new
72
+ }
73
+
74
+ assert_raise(ArgumentError) {
75
+ @appender.auto_flushing = -1
76
+ }
77
+ end
78
+
71
79
  def test_close
72
80
  assert_equal false, @sio.closed?
73
81
  assert_equal false, @appender.closed?
@@ -77,7 +85,7 @@ module TestAppenders
77
85
  assert_equal true, @appender.closed?
78
86
 
79
87
  [STDIN, STDERR, STDOUT].each do |io|
80
- @appender = ::Logging::Appenders::IO.new 'test', io
88
+ @appender = Logging.appenders.io('test', io)
81
89
  @appender.close
82
90
  assert_equal false, io.closed?
83
91
  assert_equal true, @appender.closed?
@@ -105,9 +113,9 @@ module TestAppenders
105
113
  def test_concat_error
106
114
  # setup an internal logger to capture error messages from the IO
107
115
  # appender
108
- log = Logging::Appenders::StringIo.new('__internal_io')
109
- Logging::Logger[Logging].add_appenders(log)
110
- Logging::Logger[Logging].level = 'all'
116
+ log = Logging.appenders.string_io('__internal_io')
117
+ Logging.logger[Logging].add_appenders(log)
118
+ Logging.logger[Logging].level = 'all'
111
119
 
112
120
  # close the string IO object so we get an error
113
121
  @sio.close
@@ -141,8 +149,8 @@ module TestAppenders
141
149
  end
142
150
 
143
151
  def test_immediate_at
144
- event = ::Logging::LogEvent.new('TestLogger', @levels['warn'],
145
- [1, 2, 3, 4], false)
152
+ event = Logging::LogEvent.new('TestLogger', @levels['warn'],
153
+ [1, 2, 3, 4], false)
146
154
  @appender.append event
147
155
  assert_nil(readline)
148
156
 
@@ -8,9 +8,9 @@ module TestAppenders
8
8
  include LoggingTestCase
9
9
 
10
10
  def test_initialize
11
- ::Logging::Repository.instance
11
+ Logging::Repository.instance
12
12
 
13
- appender = ::Logging::Appenders::Stdout.new
13
+ appender = Logging.appenders.stdout
14
14
  assert_equal 'stdout', appender.name
15
15
  assert_same STDOUT, appender.instance_variable_get(:@io)
16
16
 
@@ -18,14 +18,14 @@ module TestAppenders
18
18
  assert_equal true, appender.closed?
19
19
  assert_equal false, STDOUT.closed?
20
20
 
21
- appender = ::Logging::Appenders::Stdout.new('foo')
21
+ appender = Logging.appenders.stdout('foo')
22
22
  assert_equal 'foo', appender.name
23
23
 
24
- appender = ::Logging::Appenders::Stdout.new(:level => :warn)
24
+ appender = Logging.appenders.stdout(:level => :warn)
25
25
  assert_equal 'stdout', appender.name
26
26
  assert_equal 2, appender.level
27
27
 
28
- appender = ::Logging::Appenders::Stdout.new('bar', :level => :error)
28
+ appender = Logging.appenders.stdout('bar', :level => :error)
29
29
  assert_equal 'bar', appender.name
30
30
  assert_equal 3, appender.level
31
31
  end
@@ -36,9 +36,9 @@ module TestAppenders
36
36
  include LoggingTestCase
37
37
 
38
38
  def test_initialize
39
- ::Logging::Repository.instance
39
+ Logging::Repository.instance
40
40
 
41
- appender = ::Logging::Appenders::Stderr.new
41
+ appender = Logging.appenders.stderr
42
42
  assert_equal 'stderr', appender.name
43
43
  assert_same STDERR, appender.instance_variable_get(:@io)
44
44
 
@@ -46,14 +46,14 @@ module TestAppenders
46
46
  assert_equal true, appender.closed?
47
47
  assert_equal false, STDERR.closed?
48
48
 
49
- appender = ::Logging::Appenders::Stderr.new('foo')
49
+ appender = Logging.appenders.stderr('foo')
50
50
  assert_equal 'foo', appender.name
51
51
 
52
- appender = ::Logging::Appenders::Stderr.new(:level => :warn)
52
+ appender = Logging.appenders.stderr(:level => :warn)
53
53
  assert_equal 'stderr', appender.name
54
54
  assert_equal 2, appender.level
55
55
 
56
- appender = ::Logging::Appenders::Stderr.new('bar', :level => :error)
56
+ appender = Logging.appenders.stderr('bar', :level => :error)
57
57
  assert_equal 'bar', appender.name
58
58
  assert_equal 3, appender.level
59
59
  end
@@ -11,8 +11,6 @@ module TestAppenders
11
11
 
12
12
  def setup
13
13
  super
14
- ::Logging.init
15
- @levels = ::Logging::LEVELS
16
14
 
17
15
  flexmock(Net::SMTP).new_instances do |m|
18
16
  m.should_receive(:start).at_least.once.with(
@@ -20,25 +18,26 @@ module TestAppenders
20
18
  m.should_receive(:sendmail).at_least.once.with(String, 'me', ['you'])
21
19
  end
22
20
 
23
- @appender = ::Logging::Appenders::Email.new('email',
21
+ @appender = Logging.appenders.email('email',
24
22
  'from' => 'me', 'to' => 'you',
25
23
  :buffer_size => '3', :immediate_at => 'error, fatal',
26
24
  :domain => 'test.logging', :acct => 'test', :passwd => 'test'
27
25
  )
26
+ @levels = Logging::LEVELS
28
27
  end
29
28
 
30
29
  def test_initialize
31
30
  assert_raise(ArgumentError, 'Must specify from address') {
32
- ::Logging::Appenders::Email.new('email')
31
+ Logging.appenders.email('email')
33
32
  }
34
33
  assert_raise(ArgumentError, 'Must specify to address') {
35
- ::Logging::Appenders::Email.new('email', :from => 'me')
34
+ Logging.appenders.email('email', :from => 'me')
36
35
  }
37
36
  assert_nothing_raised {
38
- ::Logging::Appenders::Email.new('email', :from => 'me', :to => 'you')
37
+ Logging.appenders.email('email', :from => 'me', :to => 'you')
39
38
  }
40
39
 
41
- appender = ::Logging::Appenders::Email.new('email',
40
+ appender = Logging.appenders.email('email',
42
41
  'from' => 'me', 'to' => 'you'
43
42
  )
44
43
 
@@ -53,7 +52,7 @@ module TestAppenders
53
52
  assert_equal(:cram_md5, appender.authtype)
54
53
  assert_equal("Message of #{$0}", appender.subject)
55
54
 
56
- appender = ::Logging::Appenders::Email.new('email',
55
+ appender = Logging.appenders.email('email',
57
56
  'from' => 'lbrinn@gmail.com', 'to' => 'everyone',
58
57
  :buffsize => '1000', :immediate_at => 'error, fatal',
59
58
  :server => 'smtp.google.com', :port => '443',
@@ -75,7 +74,7 @@ module TestAppenders
75
74
  assert_equal(:tls, appender.authtype)
76
75
  assert_equal("I'm rich and you're not", appender.subject)
77
76
 
78
- appender = ::Logging::Appenders::Email.new('email',
77
+ appender = Logging.appenders.email('email',
79
78
  'from' => 'me', 'to' => 'you', :auto_flushing => 42
80
79
  )
81
80
  assert_equal(42, appender.auto_flushing)
@@ -85,8 +84,8 @@ module TestAppenders
85
84
  # with auto_flushing enabled, mail will be sent each time a log event
86
85
  # occurs
87
86
  @appender.auto_flushing = true
88
- event = ::Logging::LogEvent.new('TestLogger', @levels['warn'],
89
- [1, 2, 3, 4], false)
87
+ event = Logging::LogEvent.new('TestLogger', @levels['warn'],
88
+ [1, 2, 3, 4], false)
90
89
  @appender.append event
91
90
  assert_not_equal(@levels.length, @appender.level)
92
91
  assert_equal(0, @appender.buffer.length)
@@ -102,10 +101,10 @@ module TestAppenders
102
101
  assert_equal(0, @appender.buffer.length)
103
102
 
104
103
  # error and fatal messages should be send immediately (no buffering)
105
- error = ::Logging::LogEvent.new('ErrLogger', @levels['error'],
106
- 'error message', false)
107
- fatal = ::Logging::LogEvent.new('FatalLogger', @levels['fatal'],
108
- 'fatal message', false)
104
+ error = Logging::LogEvent.new('ErrLogger', @levels['error'],
105
+ 'error message', false)
106
+ fatal = Logging::LogEvent.new('FatalLogger', @levels['fatal'],
107
+ 'fatal message', false)
109
108
 
110
109
  @appender.append event
111
110
  @appender.append fatal
@@ -140,8 +139,8 @@ module TestAppenders
140
139
  end
141
140
 
142
141
  def test_flush
143
- event = ::Logging::LogEvent.new('TestLogger', @levels['info'],
144
- [1, 2, 3, 4], false)
142
+ event = Logging::LogEvent.new('TestLogger', @levels['info'],
143
+ [1, 2, 3, 4], false)
145
144
  @appender.append event
146
145
  @appender << 'test message'
147
146
  assert_equal(2, @appender.buffer.length)
@@ -152,8 +151,8 @@ module TestAppenders
152
151
  end
153
152
 
154
153
  def test_close
155
- event = ::Logging::LogEvent.new('TestLogger', @levels['info'],
156
- [1, 2, 3, 4], false)
154
+ event = Logging::LogEvent.new('TestLogger', @levels['info'],
155
+ [1, 2, 3, 4], false)
157
156
  @appender.append event
158
157
  @appender << 'test message'
159
158
  assert_equal(2, @appender.buffer.length)