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.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/kafka/tagged_logger.rb', line 60 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
54 55 56 57 58 |
# File 'lib/kafka/tagged_logger.rb', line 54 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
37 38 39 |
# File 'lib/kafka/tagged_logger.rb', line 37 def .clear end |
#current_tags ⇒ Object
41 42 43 44 45 |
# File 'lib/kafka/tagged_logger.rb', line 41 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
71 72 73 74 |
# File 'lib/kafka/tagged_logger.rb', line 71 def flush super if defined?(super) end |
#pop_tags(size = 1) ⇒ Object
33 34 35 |
# File 'lib/kafka/tagged_logger.rb', line 33 def (size = 1) .pop size end |
#push_tags(*tags) ⇒ Object
27 28 29 30 31 |
# File 'lib/kafka/tagged_logger.rb', line 27 def (*) .flatten.reject { |t| t.nil? || t.empty? }.tap do || .concat end end |
#tagged(*tags) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/kafka/tagged_logger.rb', line 20 def tagged(*) = (*) yield self ensure (.size) end |
#tags_text ⇒ Object
47 48 49 50 51 52 |
# File 'lib/kafka/tagged_logger.rb', line 47 def = if .any? .collect { |tag| "[#{tag}] " }.join end end |