madvertise-logging 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,9 +38,9 @@ module Madvertise
38
38
  attr_accessor :silencer
39
39
  end
40
40
 
41
- def initialize(logfile = nil, progname = nil)
42
- @progname = progname || File.basename($0)
43
- self.logger = logfile
41
+ def initialize(backend = STDERR, progname = nil)
42
+ self.progname = progname || File.basename($0)
43
+ self.logger = backend
44
44
  end
45
45
 
46
46
  # Get the backend logger.
@@ -52,21 +52,19 @@ module Madvertise
52
52
 
53
53
  # Set a different backend.
54
54
  #
55
- # @param [Symbol, String, Logger] value The new logger backend. Either a
56
- # Logger object, a String containing the logfile path or a Symbol to
55
+ # @param [Symbol, String, IO, Logger] value The new logger backend. Either a
56
+ # Logger object, an IO object, a String containing the logfile path or a Symbol to
57
57
  # create a default backend for :syslog or :buffer
58
58
  # @return [Logger] The newly created backend logger object.
59
59
  def logger=(value)
60
- @logger.close rescue nil
61
- @logfile = value.is_a?(String) ? value : nil
62
- @backend = value.is_a?(Symbol) ? value : :logger
63
- @logger = value.is_a?(Logger) ? value : create_backend
60
+ @backend = value
61
+ create_backend
64
62
  end
65
63
 
66
64
  # Close any connections/descriptors that may have been opened by the
67
65
  # current backend.
68
66
  def close
69
- logger.close rescue nil
67
+ @logger.close rescue nil
70
68
  @logger = nil
71
69
  end
72
70
 
@@ -200,30 +198,55 @@ module Madvertise
200
198
  end
201
199
 
202
200
  def create_backend
201
+ self.close
202
+
203
203
  case @backend
204
- when :buffer
205
- create_buffering_backend
206
204
  when :syslog
207
205
  create_syslog_backend
206
+ when :buffer
207
+ create_buffer_backend
208
+ when String
209
+ create_file_backend
210
+ when IO
211
+ create_io_backend
212
+ when Logger
213
+ @backend
208
214
  else
209
- create_standard_backend
215
+ raise "unknown backend: #{@backend.inspect}"
216
+ end
217
+ end
218
+
219
+ def create_syslog_backend
220
+ begin
221
+ require 'syslogger'
222
+ Syslogger.new(progname, Syslog::LOG_PID, Syslog::LOG_LOCAL1)
223
+ rescue LoadError
224
+ self.logger = $stderr
225
+ error("Couldn't load syslogger gem, reverting to STDERR for logging")
210
226
  end
211
227
  end
212
228
 
213
- def create_buffering_backend
229
+ def create_buffer_backend
214
230
  @logfile = StringIO.new
215
231
  create_logger
216
232
  end
217
233
 
218
- def create_standard_backend
234
+ def create_io_backend
235
+ @logfile = @backend
236
+ create_logger
237
+ end
238
+
239
+ def create_file_backend
240
+ @logfile = @backend
241
+
219
242
  begin
220
243
  FileUtils.mkdir_p(File.dirname(@logfile))
221
244
  rescue
222
- $stderr.puts "#{@logfile} not writable, using stderr for logging" if @logfile
223
- @logfile = $stderr
245
+ self.logger = $stderr
246
+ error("#{@logfile} not writable, using STDERR for logging")
247
+ else
248
+ create_logger
224
249
  end
225
-
226
- create_logger
227
250
  end
228
251
 
229
252
  def create_logger
@@ -233,16 +256,6 @@ module Madvertise
233
256
  end
234
257
  end
235
258
 
236
- def create_syslog_backend
237
- begin
238
- require 'syslogger'
239
- Syslogger.new(progname, Syslog::LOG_PID, Syslog::LOG_LOCAL1)
240
- rescue LoadError
241
- self.logger = :logger
242
- self.error("Couldn't load syslogger gem, reverting to standard logger")
243
- end
244
- end
245
-
246
259
  ##
247
260
  # The Formatter class is responsible for formatting log messages. The
248
261
  # default format is:
@@ -1,6 +1,6 @@
1
1
  module Madvertise
2
2
  module Logging
3
3
  # @private
4
- VERSION = "0.4.1"
4
+ VERSION = "0.4.2"
5
5
  end
6
6
  end
@@ -178,7 +178,7 @@ describe ImprovedLogger do
178
178
  end
179
179
 
180
180
  it "should fall back to stderr if logfile is not writable" do
181
- $stderr.should_receive(:puts).with(/not writable.*stderr/)
181
+ $stderr.should_receive(:write).with(/not writable.*STDERR/)
182
182
 
183
183
  @logfile = "/not/writable/spec.log"
184
184
  @logger = ImprovedLogger.new(@logfile)
@@ -192,8 +192,7 @@ describe ImprovedLogger do
192
192
  syslogger_paths = $:.select { |p| p.match(/gems\/.*syslogger-/) }
193
193
  $:.replace($: - syslogger_paths)
194
194
 
195
- $stderr.should_receive(:puts).with(/using stderr for logging/)
196
- $stderr.should_receive(:write).with(/reverting to standard logger/)
195
+ $stderr.should_receive(:write).with(/reverting to STDERR/)
197
196
  @logger = ImprovedLogger.new(:syslog)
198
197
  @logger.logger.should be_instance_of(Logger)
199
198
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: madvertise-logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
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-06-19 00:00:00.000000000 Z
12
+ date: 2012-08-02 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Advanced logging classes with buffer backend, transactions, multi logger,
15
15
  etc
@@ -55,7 +55,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
55
  version: '0'
56
56
  segments:
57
57
  - 0
58
- hash: 3468536438551765291
58
+ hash: -2521361738658986619
59
59
  required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  none: false
61
61
  requirements:
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  version: '0'
65
65
  segments:
66
66
  - 0
67
- hash: 3468536438551765291
67
+ hash: -2521361738658986619
68
68
  requirements: []
69
69
  rubyforge_project:
70
70
  rubygems_version: 1.8.17