logcast 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,4 +7,8 @@ module Logcast::Broadcast
7
7
  def subscribe(*args, &block)
8
8
  @log.subscribe(*args, &block)
9
9
  end
10
+
11
+ def subscribers
12
+ @log.subscribers
13
+ end
10
14
  end
@@ -14,8 +14,14 @@ class Logcast::Broadcaster < ::Logger
14
14
  end
15
15
 
16
16
  def subscribe(subscriber, &block)
17
+ unless subscriber.respond_to?(:add)
18
+ original_subscriber = subscriber
19
+ subscriber = Logger.new(subscriber)
20
+ subscriber.instance_variable_set(:@logcast_original_subscriber, original_subscriber)
21
+ end
22
+
17
23
  if block
18
- if subscribers.include?(subscriber)
24
+ if already_subscribed?(subscriber)
19
25
  yield
20
26
  else
21
27
  begin
@@ -26,7 +32,7 @@ class Logcast::Broadcaster < ::Logger
26
32
  end
27
33
  end
28
34
  else
29
- subscribers << subscriber unless subscribers.include?(subscriber)
35
+ subscribers << subscriber unless already_subscribed?(subscriber)
30
36
  end
31
37
  end
32
38
 
@@ -37,22 +43,22 @@ class Logcast::Broadcaster < ::Logger
37
43
  # Rails 3
38
44
  def add(*args)
39
45
  super
40
-
41
- subscribers.each do |subscriber|
42
- subscriber.add(*args)
43
- end
46
+ subscribers.each { |subscriber| subscriber.add(*args) }
44
47
  end
45
48
 
46
49
  # Rails 2
47
50
  def write(msg)
48
51
  self << msg
52
+ subscribers.each { |subscriber| subscriber.add(level, msg.rstrip, progname) }
53
+ end
49
54
 
50
- subscribers.each do |subscriber|
51
- if subscriber.respond_to?(:write)
52
- subscriber.write(msg)
53
- else
54
- subscriber.add(level, msg, progname)
55
- end
56
- end
55
+ private
56
+
57
+ def already_subscribed?(logger)
58
+ subscribers.map { |s| log_device(s) }.include?(log_device(logger))
59
+ end
60
+
61
+ def log_device(logger)
62
+ logger.instance_variable_get(:@logcast_original_subscriber) || logger
57
63
  end
58
64
  end
data/lib/logcast/rails.rb CHANGED
@@ -1,11 +1,2 @@
1
1
  require 'logcast'
2
-
3
- logger = if defined?(Rails.logger)
4
- Rails.logger
5
- elsif defined?(RAILS_DEFAULT_LOGGER)
6
- RAILS_DEFAULT_LOGGER
7
- else
8
- ActiveRecord::Base.logger
9
- end
10
-
11
- logger.extend(Logcast::Broadcast)
2
+ Logcast.engage!
data/lib/logcast.rb CHANGED
@@ -1,4 +1,18 @@
1
1
  module Logcast
2
2
  autoload :Broadcaster, 'logcast/broadcaster.rb'
3
3
  autoload :Broadcast, 'logcast/broadcast.rb'
4
+
5
+ def self.engage!
6
+ logger = if defined?(Rails.logger)
7
+ Rails.logger
8
+ elsif defined?(RAILS_DEFAULT_LOGGER)
9
+ RAILS_DEFAULT_LOGGER
10
+ else
11
+ ActiveRecord::Base.logger
12
+ end
13
+
14
+ logger = logger.instance_variable_get(:@logger) if logger.class.name == "ActiveSupport::TaggedLogging"
15
+
16
+ logger.extend(Logcast::Broadcast)
17
+ end
4
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logcast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-25 00:00:00.000000000 Z
13
+ date: 2013-07-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake