madvertise-logging 0.7.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ require 'logger'
2
+
3
+ module Madvertise
4
+ module Logging
5
+
6
+ ##
7
+ # DocumentLogger is a Logger compliant class that keeps a structured
8
+ # document per log message in memory.
9
+ #
10
+ class DocumentLogger < ::Logger
11
+
12
+ attr_accessor :attrs
13
+ attr_accessor :messages
14
+
15
+ def initialize
16
+ super(nil)
17
+ @messages = []
18
+ @attrs = {}
19
+ end
20
+
21
+ def add(severity, message = nil, progname = nil, &block)
22
+ severity ||= UNKNOWN
23
+ if severity < @level
24
+ return true
25
+ end
26
+
27
+ progname ||= @progname
28
+
29
+ if message.nil?
30
+ if block_given?
31
+ message = yield
32
+ else
33
+ message = progname
34
+ progname = @progname
35
+ end
36
+ end
37
+
38
+ @messages << @attrs.merge({
39
+ severity: severity,
40
+ time: Time.now,
41
+ progname: progname,
42
+ message: message,
43
+ })
44
+
45
+ true
46
+ end
47
+ end
48
+ end
49
+ end
@@ -3,6 +3,7 @@ require 'stringio'
3
3
  require 'benchmark'
4
4
 
5
5
  require 'madvertise/logging/improved_io'
6
+ require 'madvertise/logging/document_logger'
6
7
 
7
8
  class String
8
9
  def clean_quote
@@ -94,11 +95,18 @@ module Madvertise
94
95
  @logfile.string if @backend == :buffer
95
96
  end
96
97
 
98
+ # Retrieve collected messages in case this instance is a document logger.
99
+ #
100
+ # @return [Array] An array of logged messages.
101
+ def messages
102
+ logger.messages if @backend == :document
103
+ end
104
+
97
105
  # Get the current logging level.
98
106
  #
99
107
  # @return [Symbol] Current logging level.
100
108
  def level
101
- @logger.level
109
+ logger.level
102
110
  end
103
111
 
104
112
  # Set the logging level.
@@ -236,6 +244,8 @@ module Madvertise
236
244
  create_syslog_backend
237
245
  when :buffer
238
246
  create_buffer_backend
247
+ when :document
248
+ create_document_backend
239
249
  when String
240
250
  create_file_backend
241
251
  when IO
@@ -262,6 +272,13 @@ module Madvertise
262
272
  create_logger
263
273
  end
264
274
 
275
+ def create_document_backend
276
+ DocumentLogger.new.tap do |logger|
277
+ logger.formatter = Formatter.new
278
+ logger.progname = progname
279
+ end
280
+ end
281
+
265
282
  def create_io_backend
266
283
  @logfile = @backend
267
284
  create_logger
@@ -1,6 +1,6 @@
1
1
  module Madvertise
2
2
  module Logging
3
3
  # @private
4
- VERSION = "0.7.3"
4
+ VERSION = "0.8.0"
5
5
  end
6
6
  end
@@ -305,6 +305,37 @@ describe ImprovedLogger do
305
305
  end
306
306
  end
307
307
 
308
+ context "document backend" do
309
+ before { @logger = ImprovedLogger.new(:document) }
310
+
311
+ before do
312
+ @msg = "test"
313
+
314
+ @now = Time.now
315
+ Time.stub(:now).and_return(@now)
316
+
317
+ @expected = {
318
+ severity: Logger::INFO,
319
+ time: @now,
320
+ progname: "rspec",
321
+ message: @msg
322
+ }
323
+ end
324
+
325
+ it "should store all messages as documents" do
326
+ @logger.info(@msg)
327
+ @logger.messages.first.should == @expected
328
+ end
329
+
330
+ it "should add custom attributes" do
331
+ attrs = {txid: 1234}
332
+ @logger.logger.attrs = attrs
333
+ @logger.info(@msg)
334
+ @logger.messages.first.should == attrs.merge(@expected)
335
+ end
336
+
337
+ end
338
+
308
339
  context "syslog backend" do
309
340
  before { @logger = ImprovedLogger.new(:syslog) }
310
341
  its(:sync) { should == true }
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.7.3
4
+ version: 0.8.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-11-08 00:00:00.000000000 Z
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Advanced logging classes with buffer backend, transactions, multi logger,
15
15
  etc
@@ -29,6 +29,7 @@ files:
29
29
  - Rakefile
30
30
  - benchmark.rb
31
31
  - lib/madvertise-logging.rb
32
+ - lib/madvertise/logging/document_logger.rb
32
33
  - lib/madvertise/logging/improved_io.rb
33
34
  - lib/madvertise/logging/improved_logger.rb
34
35
  - lib/madvertise/logging/mask.reek
@@ -56,7 +57,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
57
  version: '0'
57
58
  segments:
58
59
  - 0
59
- hash: 2068495888823290885
60
+ hash: -4209481848694678051
60
61
  required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  none: false
62
63
  requirements:
@@ -65,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
66
  version: '0'
66
67
  segments:
67
68
  - 0
68
- hash: 2068495888823290885
69
+ hash: -4209481848694678051
69
70
  requirements: []
70
71
  rubyforge_project:
71
72
  rubygems_version: 1.8.24