Class: Tracee::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/tracee/stream.rb

Direct Known Subclasses

IndifferentStream

Defined Under Namespace

Classes: TargetError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Stream

@ target : IO | String | {<level name> => < level log file path | IO >, … } | => < level log file path pattern > pattern example : “log/development.%level.log”



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tracee/stream.rb', line 12

def initialize(target)
  if target.is_a? Hash
    raise TargetError if target.values.any? {|val| !( val.is_a? String or val.is_a? IO or val.is_a? StringIO )}
    
    if pattern = target[:cascade]
      target = Tracee::Logger::LEVEL_NAMES.map {|name|
        [name, pattern % {level: name}]
      }.to_h
    else
      target = target.with_indifferent_access
    end
  
  else
    raise TargetError unless target.is_a? String or target.is_a? IO or target.is_a? StringIO
  end

  @target = target
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



4
5
6
# File 'lib/tracee/stream.rb', line 4

def target
  @target
end

Instance Method Details

#write(msg, msg_level = nil, log_level = nil) ⇒ Object Also known as: <<

cascade principle:

logger.log_level = :debug logger.warn msg

development.debug.log << msg
development.info.log << msg
development.warn.log << msg

logger.log_level = :warn logger.error msg

development.warn.log << msg
development.error.log << msg


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tracee/stream.rb', line 43

def write(msg, msg_level=nil, log_level=nil)
  return if msg.nil?
  
  case @target
  when Hash # cascade
    Tracee::Logger::LEVEL_NAMES[log_level..msg_level].each do |name|
      if target = @target[name]
        io_write target, msg
      end
    end
  else
    io_write @target, msg
  end
end