Class: TTY::Logger::Handlers::Stream

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/tty/logger/handlers/stream.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#coerce_formatter, #format_filepath, #log_at, #metadata, #raise_formatter_error

Constructor Details

#initialize(output: $stderr, formatter: nil, config: nil, level: nil) ⇒ Stream

Returns a new instance of Stream.


17
18
19
20
21
22
23
# File 'lib/tty/logger/handlers/stream.rb', line 17

def initialize(output: $stderr, formatter: nil, config: nil, level: nil)
  @output = Array[output].flatten
  @formatter = coerce_formatter(formatter || config.formatter).new
  @config = config
  @level = level || @config.level
  @mutex = Mutex.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


13
14
15
# File 'lib/tty/logger/handlers/stream.rb', line 13

def config
  @config
end

#levelObject (readonly)

Returns the value of attribute level


15
16
17
# File 'lib/tty/logger/handlers/stream.rb', line 15

def level
  @level
end

#outputObject (readonly)

Returns the value of attribute output


11
12
13
# File 'lib/tty/logger/handlers/stream.rb', line 11

def output
  @output
end

Instance Method Details

#call(event) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tty/logger/handlers/stream.rb', line 26

def call(event)
  @mutex.lock

  data = {}
  .each do |meta|
    case meta
    when :date
      data["date"] = event.[:time].strftime(config.date_format)
    when :time
      data["time"] = event.[:time].strftime(config.time_format)
    when :file
      data["path"] = format_filepath(event)
    when :pid
      data["pid"] = event.[:pid]
    else
      raise "Unknown metadata `#{meta}`"
    end
  end
  data["level"] = event.[:level]
  data["message"] = event.message.join(' ')
  unless event.fields.empty?
    data.merge!(event.fields)
  end
  unless event.backtrace.empty?
    data.merge!("backtrace" => event.backtrace.join(","))
  end

  output.each do |out|
    out.puts @formatter.dump(data, max_bytes: config.max_bytes,
                             max_depth: config.max_depth)
  end
ensure
  @mutex.unlock
end