class TTY::Logger::Handlers::Console

Constants

ARROW
COLOR_PATTERNS
JSON_REGEXP
STYLES
TEXT_REGEXP

Attributes

config[R]

The configuration options @api private

level[R]

The logging level @api private

message_format[R]

The format for the message @api private

output[R]

The output stream @api private

Public Class Methods

new(output: $stderr, formatter: nil, config: nil, level: nil, styles: {}, enable_color: nil, message_format: "%-25s") click to toggle source
# File lib/tty/logger/handlers/console.rb, line 84
def initialize(output: $stderr, formatter: nil, config: nil, level: nil,
               styles: {}, enable_color: nil, message_format: "%-25s")
  @output = Array[output].flatten
  @formatter = coerce_formatter(formatter || config.formatter).new
  @formatter_name = @formatter.class.name.split("::").last.downcase
  @color_pattern = COLOR_PATTERNS[@formatter_name.to_sym]
  @config = config
  @styles = styles
  @level = level || @config.level
  @mutex = Mutex.new
  @pastel = Pastel.new(enabled: enable_color)
  @message_format = message_format
end

Public Instance Methods

call(event) click to toggle source

Handle log event output in format

@param [Event] event

the current event logged

@api public

# File lib/tty/logger/handlers/console.rb, line 104
def call(event)
  @mutex.lock

  style = configure_styles(event)
  color = configure_color(style)

  fmt = []
  metadata.each do |meta|
    case meta
    when :date
      fmt << @pastel.white("[" + event.metadata[:time].
                           strftime(config.date_format) + "]")
    when :time
      fmt << @pastel.white("[" + event.metadata[:time].
                           strftime(config.time_format) + "]")
    when :file
      fmt << @pastel.white("[#{format_filepath(event)}]")
    when :pid
      fmt << @pastel.white("[%d]" % event.metadata[:pid])
    else
      raise "Unknown metadata `#{meta}`"
    end
  end
  fmt << ARROW unless config.metadata.empty?
  unless style.empty?
    fmt << color.(style[:symbol])
    fmt << color.(style[:label]) + (" " * style[:levelpad])
  end
  fmt << message_format % event.message.join(" ")
  unless event.fields.empty?
    pattern, replacement = *@color_pattern
    fmt << @formatter.dump(event.fields, max_bytes: config.max_bytes,
                                         max_depth: config.max_depth)
                     .gsub(pattern, replacement.(color))
  end
  unless event.backtrace.empty?
    fmt << "\n" + format_backtrace(event)
  end

  output.each { |out| out.puts fmt.join(" ") }
ensure
  @mutex.unlock
end

Private Instance Methods

configure_color(style) click to toggle source
# File lib/tty/logger/handlers/console.rb, line 171
def configure_color(style)
  color = style.fetch(:color) { :cyan }
  @pastel.send(color).detach
end
configure_styles(event) click to toggle source

Merge default styles with custom style overrides

@return [Hash]

the style matching log type

@api private

# File lib/tty/logger/handlers/console.rb, line 163
def configure_styles(event)
  return {} if event.metadata[:name].nil?

  STYLES.fetch(event.metadata[:name].to_sym, {})
        .dup
        .merge!(@styles[event.metadata[:name].to_sym] || {})
end
format_backtrace(event) click to toggle source
# File lib/tty/logger/handlers/console.rb, line 150
def format_backtrace(event)
  indent = " " * 4
  event.backtrace.map do |bktrace|
    indent + bktrace.to_s
  end.join("\n")
end