Class: TTY::Logger::Handlers::Console
- Inherits:
-
Object
- Object
- TTY::Logger::Handlers::Console
- 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 } }.freeze
- 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
-
#config ⇒ Object
readonly
private
The configuration options.
-
#level ⇒ Object
readonly
private
The logging level.
-
#message_format ⇒ Object
readonly
private
The format for the message.
-
#output ⇒ Object
readonly
private
The output stream.
Instance Method Summary collapse
-
#call(event) ⇒ Object
Handle log event output in format.
-
#initialize(output: $stderr, formatter: nil, config: nil, level: nil, styles: {}, enable_color: nil, message_format: "%-25s") ⇒ Console
constructor
A new instance of Console.
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: {}, enable_color: nil, message_format: "%-25s") ⇒ Console
Returns a new instance of Console.
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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 = end |
Instance Attribute Details
#config ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The configuration options
74 75 76 |
# File 'lib/tty/logger/handlers/console.rb', line 74 def config @config end |
#level ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The logging level
78 79 80 |
# File 'lib/tty/logger/handlers/console.rb', line 78 def level @level end |
#message_format ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The format for the message
82 83 84 |
# File 'lib/tty/logger/handlers/console.rb', line 82 def @message_format end |
#output ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The output stream
70 71 72 |
# File 'lib/tty/logger/handlers/console.rb', line 70 def output @output end |
Instance Method Details
#call(event) ⇒ Object
Handle log event output in format
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 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/tty/logger/handlers/console.rb', line 104 def call(event) @mutex.lock style = configure_styles(event) color = configure_color(style) fmt = [] .each do || case 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 `#{}`" end end fmt << ARROW unless config..empty? unless style.empty? fmt << color.(style[:symbol]) fmt << color.(style[:label]) + (" " * style[:levelpad]) end fmt << % event..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 |