cabin 0.7.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d42c4bca378a65128dd011758c5ba0fc3427dbf
4
- data.tar.gz: 9c2916616932b6e6a6b89aa9594f6c854075c95d
3
+ metadata.gz: a4915c746a3d520ad14afb1c7e0ca4ba6c7fa83d
4
+ data.tar.gz: 6d4ef30f8c4cd39fa68abb1999f9c02c7aa21ff2
5
5
  SHA512:
6
- metadata.gz: 3a6e45ba8f188e20d3fc02fe7585aecfb8574907f414aba1b028c4e3ac0707e4794e11a11d4863c3ccd3a0db92fa50918394db70fe804eaa68f375bcfab835b4
7
- data.tar.gz: bfc18231653001bef7f445e61d4dfe52b6aeab1baa1d04839efa22d3abd109d0826150bc59c78e548cebbfc9e8b60d6c24a356a40ff6a488d80c695faee762e7
6
+ metadata.gz: 0aa797866dca2939f349df4a898fa87e00d36c5adfcce1d213f894cda342a4df8751a2e3a3a655d3375108bb32d3a7e6400faa2ceb422bb6cfddf1b55173932c
7
+ data.tar.gz: 27e8f16b7893b2db64f582a28572d091c25578d26027a8f2d673cddb4264cd641a623e0286a6e9adbde6b8669d8385ecc0fe63cbe110b9ccaf6f73840ae12a07
@@ -7,6 +7,7 @@ require "cabin/namespace"
7
7
  require "cabin/context"
8
8
  require "cabin/outputs/stdlib-logger"
9
9
  require "cabin/outputs/io"
10
+ require "cabin/subscriber"
10
11
  require "cabin/metrics"
11
12
  require "logger" # stdlib
12
13
  require "thread"
@@ -75,16 +76,33 @@ class Cabin::Channel
75
76
  end
76
77
  end # def Cabin::Channel.each
77
78
 
78
- # Get a list of filters included in this class.
79
- def filters
80
- @filters ||= []
81
- end # def Cabin::Channel.filters
82
-
83
- # Register a new filter. The block is passed the event. It is expected to
79
+ # Get a list of actions included in this class.
80
+ def actions
81
+ @actions ||= []
82
+ end # def Cabin::Channel.actions
83
+ alias_method(:filters, :actions) # DEPRECATED
84
+
85
+ # Register a new action. The block is passed the event. It is expected to
84
86
  # modify that event or otherwise do nothing.
85
- def filter(&block)
86
- @filters ||= []
87
- @filters << block
87
+ def action(&block)
88
+ actions << block
89
+ end
90
+ alias_method(:filter, :action) # DEPRECATED
91
+
92
+ # Get a list of conditions included in this class.
93
+ def conditions
94
+ @conditions ||= []
95
+ end
96
+
97
+ # Register a new condition. The block must expect an event and a subscription.
98
+ # It is expected to either return true (allow the event) or false (reject it).
99
+ def condition(&block)
100
+ conditions << block
101
+ end
102
+
103
+ # Decide to publish the event based on conditions and subscription options
104
+ def allow_event?(event, subscription)
105
+ conditions.all? { |condition| condition.call(event, subscription) }
88
106
  end
89
107
  end # class << self
90
108
 
@@ -115,7 +133,7 @@ class Cabin::Channel
115
133
  # foo << event
116
134
  #
117
135
  # Returns a subscription id you can use later to unsubscribe
118
- def subscribe(output)
136
+ def subscribe(output, options = {})
119
137
  # Wrap ruby stdlib Logger if given.
120
138
  if output.is_a?(::Logger)
121
139
  output = Cabin::Outputs::StdlibLogger.new(output)
@@ -123,7 +141,7 @@ class Cabin::Channel
123
141
  output = Cabin::Outputs::IO.new(output)
124
142
  end
125
143
  @subscriber_lock.synchronize do
126
- @subscribers[output.object_id] = output
144
+ @subscribers[output.object_id] = Cabin::Subscriber.new(output, options)
127
145
  end
128
146
  return output.object_id
129
147
  end # def subscribe
@@ -159,8 +177,9 @@ class Cabin::Channel
159
177
  # is a string ISO8601 timestamp with microsecond precision.
160
178
  def publish(data, &block)
161
179
  event = {}
162
- self.class.filters.each do |filter|
163
- filter.call(event)
180
+
181
+ self.class.actions.each do |action|
182
+ action.call(event)
164
183
  end
165
184
 
166
185
  if data.is_a?(String)
@@ -171,9 +190,11 @@ class Cabin::Channel
171
190
  event.merge!(@data) # Merge any logger context
172
191
 
173
192
  @subscriber_lock.synchronize do
174
- @subscribers.each do |id, output|
175
- append = block_given? ? block.call(output, event) : true
176
- output << event if append
193
+ @subscribers.each do |_, subscriber|
194
+ append = block_given? ? block.call(subscriber, event) : true
195
+ if append && self.class.allow_event?(event, subscriber)
196
+ subscriber << event
197
+ end
177
198
  end
178
199
  end
179
200
  end # def publish
@@ -3,6 +3,17 @@ require "cabin/namespace"
3
3
  # This module implements methods that act somewhat like Ruby's Logger class
4
4
  # It is included in Cabin::Channel
5
5
  module Cabin::Mixins::Logger
6
+
7
+ def self.included(klass)
8
+ klass.condition do |event, subscription|
9
+ if subscription.nil?
10
+ true
11
+ else
12
+ LEVELS[(subscription.options[:level] || :debug)] >= LEVELS[event[:level]].to_i
13
+ end
14
+ end
15
+ end
16
+
6
17
  attr_accessor :level
7
18
  LEVELS = {
8
19
  :fatal => 0,
@@ -3,7 +3,8 @@ require "cabin/namespace"
3
3
  module Cabin::Mixins::Terminal
4
4
 
5
5
  def terminal(message)
6
- publish(message) do |output, event|
6
+ publish(message) do |subscriber, event|
7
+ output = subscriber.output
7
8
  output.respond_to?(:tty?) && output.tty?
8
9
  end
9
10
  end
@@ -6,7 +6,7 @@ module Cabin::Mixins::Timestamp
6
6
  self.included(instance.class)
7
7
  end
8
8
  def self.included(klass)
9
- klass.filter do |event|
9
+ klass.action do |event|
10
10
  event[:timestamp] = Time.now.strftime("%Y-%m-%dT%H:%M:%S.%6N%z")
11
11
  end
12
12
  end
@@ -0,0 +1,11 @@
1
+ class Cabin::Subscriber
2
+ attr :output, :options
3
+ def initialize(output, options = {})
4
+ @output = output
5
+ @options = options
6
+ end
7
+
8
+ def <<(data)
9
+ @output << data
10
+ end
11
+ end
@@ -1,13 +1,8 @@
1
- $: << File.join(File.dirname(__FILE__), "..", "lib")
1
+ require "test_helper"
2
2
 
3
- require "rubygems"
4
- require "minitest/autorun"
5
- require "simplecov"
3
+ base_dir = File.join(File.expand_path(File.dirname(__FILE__)), "cabin")
6
4
 
7
- SimpleCov.start
8
-
9
- dir = File.dirname(File.expand_path(__FILE__))
10
- Dir.glob(File.join(dir, "**", "test_*.rb")).each do |path|
5
+ Dir.glob(File.join(base_dir, "test_*.rb")).each do |path|
11
6
  puts "Loading tests from #{path}"
12
7
  if path =~ /test_zeromq/
13
8
  puts "Skipping zeromq tests because they force ruby to exit if libzmq is not found"
@@ -1,11 +1,4 @@
1
- $: << File.dirname(__FILE__)
2
- $: << File.join(File.dirname(__FILE__), "..", "lib")
3
-
4
- require "rubygems"
5
- require "minitest-patch"
6
- require "cabin"
7
- require "stringio"
8
- require "minitest/autorun" if __FILE__ == $0
1
+ require "test_helper"
9
2
 
10
3
  describe Cabin::Channel do
11
4
 
@@ -69,6 +62,19 @@ describe Cabin::Channel do
69
62
  assert_equal("Hello world", @target.data[0][:message])
70
63
  end
71
64
 
65
+ test "subscribe with a level impacts log publishing" do
66
+ sub1 = Receiver.new
67
+ sub2 = Receiver.new
68
+ @logger.subscribe(sub1, :level => :info)
69
+ @logger.subscribe(sub2, :level => :error)
70
+ @logger.debug("test debug")
71
+ @logger.info("test info")
72
+ @logger.error("test error")
73
+
74
+ assert_equal("test info", sub1.data[0][:message])
75
+ assert_equal("test error", sub1.data[1][:message])
76
+ assert_equal("test error", sub2.data[0][:message])
77
+ end
72
78
  test "context values" do
73
79
  context = @logger.context
74
80
  context["foo"] = "hello"
@@ -1,11 +1,5 @@
1
- $: << File.dirname(__FILE__)
2
- $: << File.join(File.dirname(__FILE__), "..", "lib")
3
-
4
- require "rubygems"
5
- require "minitest-patch"
6
- require "cabin"
1
+ require "test_helper"
7
2
  require "cabin/metrics"
8
- require "minitest/autorun" if __FILE__ == $0
9
3
 
10
4
  describe Cabin::Metrics do
11
5
  before do
@@ -1,11 +1,4 @@
1
- $: << File.dirname(__FILE__)
2
- $: << File.join(File.dirname(__FILE__), '..', 'lib')
3
-
4
- require 'rubygems'
5
- require 'minitest-patch'
6
- require 'cabin'
7
- require 'stringio'
8
- require 'minitest/autorun' if __FILE__ == $0
1
+ require "test_helper"
9
2
 
10
3
  describe Cabin::Channel do
11
4
  class Receiver
@@ -1,10 +1,5 @@
1
- $: << File.dirname(__FILE__)
2
- $: << File.join(File.dirname(__FILE__), "..", "lib")
3
-
4
- require "rubygems"
5
- require "minitest-patch"
1
+ require "test_helper"
6
2
  require "cabin/outputs/zeromq"
7
- require "minitest/autorun" if __FILE__ == $0
8
3
 
9
4
  describe Cabin::Outputs::ZeroMQ do
10
5
 
@@ -0,0 +1,8 @@
1
+ require "rubygems"
2
+ require "support/minitest-patch"
3
+ require "minitest/autorun"
4
+ require "cabin"
5
+ require "stringio"
6
+ require "simplecov"
7
+
8
+ SimpleCov.start
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cabin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Sissel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-29 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2015-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 10.4.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 10.4.2
13
27
  description: This is an experiment to try and make logging more flexible and more
14
28
  consumable. Plain text logs are bullshit, let's emit structured and contextual logs.
15
29
  Metrics, too!
@@ -53,13 +67,15 @@ files:
53
67
  - lib/cabin/outputs/stdlib-logger.rb
54
68
  - lib/cabin/outputs/zeromq.rb
55
69
  - lib/cabin/publisher.rb
70
+ - lib/cabin/subscriber.rb
56
71
  - lib/cabin/timer.rb
57
72
  - test/all.rb
58
- - test/minitest-patch.rb
59
- - test/test_logging.rb
60
- - test/test_metrics.rb
61
- - test/test_pipe.rb
62
- - test/test_zeromq.rb
73
+ - test/cabin/test_logging.rb
74
+ - test/cabin/test_metrics.rb
75
+ - test/cabin/test_pipe.rb
76
+ - test/cabin/test_zeromq.rb
77
+ - test/support/minitest-patch.rb
78
+ - test/test_helper.rb
63
79
  homepage: https://github.com/jordansissel/ruby-cabin
64
80
  licenses:
65
81
  - Apache License (2.0)
@@ -86,4 +102,3 @@ signing_key:
86
102
  specification_version: 4
87
103
  summary: Experiments in structured and contextual logging
88
104
  test_files: []
89
- has_rdoc: