Class: TTY::Logger::Handlers::Console

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

Constant Summary collapse

ARROW =
""
STYLES =
{
  debug: {
    label: "debug",
    symbol: "",
    color: :cyan,
    levelpad: 2
  },
  info: {
    label: "info",
    symbol: "",
    color: :green,
    levelpad: 3
  },
  warn: {
    label: "warning",
    symbol: "",
    color: :yellow,
    levelpad: 0
  },
  error: {
    label: "error",
    symbol: "",
    color: :red,
    levelpad: 2
  },
  fatal: {
    label: "fatal",
    symbol: "!",
    color: :red,
    levelpad: 2
  },
  success: {
    label: "success",
    symbol: "",
    color: :green,
    levelpad: 0
  },
  wait: {
    label: "waiting",
    symbol: "",
    color: :cyan,
    levelpad: 0
  }
}
TEXT_REGEXP =
/([{}()\[\]])?(["']?)(\S+?)(["']?=)/.freeze
JSON_REGEXP =
/\"([^,]+?)\"(?=:)/.freeze
COLOR_PATTERNS =
{
  text: [TEXT_REGEXP, ->(c) { "\\1\\2" + c.("\\3") + "\\4" }],
  json: [JSON_REGEXP, ->(c) { "\"" + c.("\\1") + "\"" }]
}.freeze

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, styles: {}) ⇒ Console

Returns a new instance of Console.


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tty/logger/handlers/console.rb', line 74

def initialize(output: $stderr, formatter: nil, config: nil, level: nil,
               styles: {})
  @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
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


70
71
72
# File 'lib/tty/logger/handlers/console.rb', line 70

def config
  @config
end

#levelObject (readonly)

Returns the value of attribute level


72
73
74
# File 'lib/tty/logger/handlers/console.rb', line 72

def level
  @level
end

#outputObject (readonly)

Returns the value of attribute output


68
69
70
# File 'lib/tty/logger/handlers/console.rb', line 68

def output
  @output
end

Instance Method Details

#call(event) ⇒ Object

Handle log event output in format

Parameters:

  • event (Event)

    the current event logged


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/tty/logger/handlers/console.rb', line 93

def call(event)
  @mutex.lock

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

  fmt = []
  .each do |meta|
    case meta
    when :date
      fmt << @pastel.white("[" + event.[:time].
                           strftime(config.date_format) + "]")
    when :time
      fmt << @pastel.white("[" + event.[:time].
                           strftime(config.time_format) + "]")
    when :file
      fmt << @pastel.white("[#{format_filepath(event)}]")
    when :pid
      fmt << @pastel.white("[%d]" % event.[:pid])
    else
      raise "Unknown metadata `#{meta}`"
    end
  end
  fmt << ARROW unless config..empty?
  fmt << color.(style[:symbol])
  fmt << color.(style[:label]) + (" " * style[:levelpad])
  fmt << "%-25s" % 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