Class: Kafka::TaggedLogger
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Kafka::TaggedLogger
- Defined in:
- lib/kafka/tagged_logger.rb
Class Method Summary collapse
Instance Method Summary collapse
- #clear_tags! ⇒ Object
- #current_tags ⇒ Object
- #flush ⇒ Object
-
#initialize(logger_or_stream = nil) ⇒ TaggedLogger
constructor
A new instance of TaggedLogger.
- #pop_tags(size = 1) ⇒ Object
- #push_tags(*tags) ⇒ Object
- #tagged(*tags) ⇒ Object
- #tags_text ⇒ Object
Constructor Details
#initialize(logger_or_stream = nil) ⇒ TaggedLogger
Returns a new instance of TaggedLogger.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/kafka/tagged_logger.rb', line 59 def initialize(logger_or_stream = nil) logger = if %w(info debug warn error).all? { |s| logger_or_stream.respond_to?(s) } logger_or_stream elsif logger_or_stream ::Logger.new(logger_or_stream) else ::Logger.new(nil) end super(logger) end |
Class Method Details
.new(logger_or_stream = nil) ⇒ Object
53 54 55 56 57 |
# File 'lib/kafka/tagged_logger.rb', line 53 def self.new(logger_or_stream = nil) # don't keep wrapping the same logger over and over again return logger_or_stream if logger_or_stream.is_a?(TaggedLogger) super end |
Instance Method Details
#clear_tags! ⇒ Object
36 37 38 |
# File 'lib/kafka/tagged_logger.rb', line 36 def .clear end |
#current_tags ⇒ Object
40 41 42 43 44 |
# File 'lib/kafka/tagged_logger.rb', line 40 def # We use our object ID here to avoid conflicting with other instances thread_key = @thread_key ||= "kafka_tagged_logging_tags:#{object_id}".freeze Thread.current[thread_key] ||= [] end |
#flush ⇒ Object
70 71 72 73 |
# File 'lib/kafka/tagged_logger.rb', line 70 def flush super if defined?(super) end |
#pop_tags(size = 1) ⇒ Object
32 33 34 |
# File 'lib/kafka/tagged_logger.rb', line 32 def (size = 1) .pop size end |
#push_tags(*tags) ⇒ Object
26 27 28 29 30 |
# File 'lib/kafka/tagged_logger.rb', line 26 def (*) .flatten.reject { |t| t.nil? || t.empty? }.tap do || .concat end end |
#tagged(*tags) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/kafka/tagged_logger.rb', line 19 def tagged(*) = (*) yield self ensure (.size) end |
#tags_text ⇒ Object
46 47 48 49 50 51 |
# File 'lib/kafka/tagged_logger.rb', line 46 def = if .any? .collect { |tag| "[#{tag}] " }.join end end |