Class: Ferrum::Log
- Inherits:
-
Object
- Object
- Ferrum::Log
- Includes:
- Paint
- Defined in:
- lib/ferrum/log.rb,
lib/ferrum/log/json.rb,
lib/ferrum/log/paint.rb,
lib/ferrum/log/default.rb,
lib/ferrum/log/annotated.rb,
sig/ferrum/log.rbs,
sig/ferrum/log/json.rbs,
sig/ferrum/log/paint.rbs,
sig/ferrum/log/default.rbs,
sig/ferrum/log/annotated.rbs
Overview
Writes Ferrum's debug output to the :logger given to Browser.new, split
into channels: :protocol (CDP messages) and :browser (the output of the
Chrome process Ferrum launched).
The logger can be an IO-like object responding to puts, or a Logger,
which gets every line at debug level with ferrum:<channel> as program
name. Nothing is built for a disabled channel or a level the Logger skips.
FERRUM_LOG picks what is written, see Log.parse. FERRUM_LOG_FORMAT picks
how CDP messages look: json writes them as JSON alone, see Json, and
annotated puts the command id and method in front of each message and
its response, see Annotated. Without it they look as they always have,
see Default.
Written to a terminal, lines are colored with the basic ANSI colors, which
terminal themes adapt to light and dark backgrounds. Files, pipes and
Loggers get plain text, and so does a terminal when NO_COLOR is set.
FERRUM_LOG_COLOR=always or never overrides both.
Logging never breaks the browser: an error while formatting or writing a line, e.g. a closed IO, is reported once and the line skipped.
Defined Under Namespace
Modules: Paint Classes: Annotated, Default, Json
Constant Summary collapse
- CHANNELS =
%i[protocol browser].freeze
- ALL =
%w[* 1 true].freeze
- OFF =
%w[0 false].freeze
- FORMATS =
{ default: Default, json: Json, annotated: Annotated }.freeze
- VALUE_LENGTH =
Integer(ENV.fetch("FERRUM_LOG_VALUE_LENGTH", 1024))
Constants included from Paint
Instance Attribute Summary collapse
-
#channels ⇒ Array[Symbol]
readonly
Returns the value of attribute channels.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#only ⇒ Array[String]
readonly
Returns the value of attribute only.
-
#skip ⇒ Array[String]
readonly
Returns the value of attribute skip.
Class Method Summary collapse
-
.dump(value) ⇒ String
Encodes a CDP message, or a part of it, as JSON with string values longer than VALUE_LENGTH shortened, e.g.
-
.parse(value) ⇒ Hash{Symbol => Array}?
Parses
FERRUM_LOG, a comma-separated list of:. -
.parse_channel(name) ⇒ Array<Symbol>
Parses one channel item of
FERRUM_LOG, warning about an unknown one. -
.parse_color(value) ⇒ Symbol
Parses
FERRUM_LOG_COLOR, anything butalwaysornevermeans:auto. -
.parse_format(value) ⇒ Symbol
Parses
FERRUM_LOG_FORMAT:json,annotatedordefault, which is also what an empty or unknown value means. -
.truncate(value) ⇒ Object
Shortens string values longer than VALUE_LENGTH anywhere in a CDP message.
Instance Method Summary collapse
-
#browser(line) ⇒ void
Writes a line of the browser process output.
-
#enabled?(channel) ⇒ Boolean
Whether the channel is written to the logger.
-
#initialize(logger, channels: CHANNELS, only: [], skip: [], format: :default, color: :auto) ⇒ Log
constructor
A new instance of Log.
-
#received(message, raw, connection = nil) ⇒ void
Writes a CDP message received from the browser.
-
#sent(message, connection = nil) ⇒ void
Writes a CDP message sent to the browser.
Methods included from Paint
Constructor Details
#initialize(logger, channels: CHANNELS, only: [], skip: [], format: :default, color: :auto) ⇒ Log
Returns a new instance of Log.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ferrum/log.rb', line 143 def initialize(logger, channels: CHANNELS, only: [], skip: [], format: :default, color: :auto) @logger = logger @channels = channels @only = only @skip = skip @color = case color when :always then true when :never then false else logger.respond_to?(:tty?) && logger.tty? && ENV.fetch("NO_COLOR", "").empty? end @formatter = FORMATS.fetch(format).new(color: @color) @commands = Concurrent::Map.new if format == :annotated || only.any? || skip.any? @mutex = Mutex.new end |
Instance Attribute Details
#channels ⇒ Array[Symbol] (readonly)
Returns the value of attribute channels.
41 42 43 |
# File 'lib/ferrum/log.rb', line 41 def channels @channels end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
41 42 43 |
# File 'lib/ferrum/log.rb', line 41 def logger @logger end |
#only ⇒ Array[String] (readonly)
Returns the value of attribute only.
41 42 43 |
# File 'lib/ferrum/log.rb', line 41 def only @only end |
#skip ⇒ Array[String] (readonly)
Returns the value of attribute skip.
41 42 43 |
# File 'lib/ferrum/log.rb', line 41 def skip @skip end |
Class Method Details
.dump(value) ⇒ String
Encodes a CDP message, or a part of it, as JSON with string values longer
than VALUE_LENGTH shortened, e.g. Base64 screenshots.
FERRUM_LOG_VALUE_LENGTH=0 keeps them whole. Messages nest as deep as
Chrome sends them, e.g. a DOM tree, so there's no nesting limit.
121 122 123 |
# File 'lib/ferrum/log.rb', line 121 def self.dump(value) JSON.generate(truncate(value), max_nesting: false) end |
.parse(value) ⇒ Hash{Symbol => Array}?
Parses FERRUM_LOG, a comma-separated list of:
*,1ortrue- all channelsprotocol,browser- that channel,-browserleaves it outPage.*,Runtime.evaluate- only CDP messages whose method matches, which turnsprotocolon-Network.*- leaves out CDP messages whose method matches
Without a channel or CDP pattern to log, all channels are logged, so
-browser means everything but the browser output. Exclusions win over
inclusions, and responses follow the command they answer. 0, false
or an empty value turn logging off.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ferrum/log.rb', line 60 def self.parse(value) items = value.to_s.split(",").map(&:strip).reject(&:empty?) return if items.all? { |item| OFF.include?(item) } patterns, names = items.partition { |item| item.include?(".") } skip, only = patterns.partition { |pattern| pattern.start_with?("-") } removed, added = names.partition { |name| name.start_with?("-") } channels = added.flat_map { |name| parse_channel(name) } channels << :protocol if only.any? channels = CHANNELS if channels.empty? channels -= removed.flat_map { |name| parse_channel(name.delete_prefix("-")) } { channels: channels.uniq, only: only, skip: skip.map { |pattern| pattern.delete_prefix("-") } } end |
.parse_channel(name) ⇒ Array<Symbol>
Parses one channel item of FERRUM_LOG, warning about an unknown one.
81 82 83 84 85 86 87 |
# File 'lib/ferrum/log.rb', line 81 def self.parse_channel(name) return CHANNELS if ALL.include?(name) return [name.to_sym] if CHANNELS.include?(name.to_sym) warn "Ferrum: unknown FERRUM_LOG item #{name.inspect} is ignored" [] end |
.parse_color(value) ⇒ Symbol
Parses FERRUM_LOG_COLOR, anything but always or never means :auto.
109 110 111 |
# File 'lib/ferrum/log.rb', line 109 def self.parse_color(value) %w[always never].include?(value.to_s.strip) ? value.strip.to_sym : :auto end |
.parse_format(value) ⇒ Symbol
Parses FERRUM_LOG_FORMAT: json, annotated or default, which is
also what an empty or unknown value means.
95 96 97 98 99 100 101 102 |
# File 'lib/ferrum/log.rb', line 95 def self.parse_format(value) format = value.to_s.strip return :default if format.empty? return format.to_sym if FORMATS.key?(format.to_sym) warn "Ferrum: unknown FERRUM_LOG_FORMAT #{format.inspect}, using default" :default end |
.truncate(value) ⇒ Object
Shortens string values longer than VALUE_LENGTH anywhere in a CDP message.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ferrum/log.rb', line 131 def self.truncate(value) case value when Hash then value.transform_values { |item| truncate(item) } when Array then value.map { |item| truncate(item) } when String return value if VALUE_LENGTH.zero? || value.size <= VALUE_LENGTH "#{value[0, VALUE_LENGTH]}... (#{value.size - VALUE_LENGTH} more characters)" else value end end |
Instance Method Details
#browser(line) ⇒ void
This method returns an undefined value.
Writes a line of the browser process output.
210 211 212 |
# File 'lib/ferrum/log.rb', line 210 def browser(line) write(:browser) { line } end |
#enabled?(channel) ⇒ Boolean
Whether the channel is written to the logger.
163 164 165 |
# File 'lib/ferrum/log.rb', line 163 def enabled?(channel) channels.include?(channel) end |
#received(message, raw, connection = nil) ⇒ void
This method returns an undefined value.
Writes a CDP message received from the browser.
196 197 198 199 200 201 202 203 |
# File 'lib/ferrum/log.rb', line 196 def received(, raw, connection = nil) return unless enabled?(:protocol) command = answered_command(, connection) return unless show?(&.fetch("method", nil) || command&.first) write(:protocol) { @formatter.received(, raw, Utils::ElapsedTime.elapsed_time, command) } end |
#sent(message, connection = nil) ⇒ void
This method returns an undefined value.
Writes a CDP message sent to the browser.
175 176 177 178 179 180 181 182 |
# File 'lib/ferrum/log.rb', line 175 def sent(, connection = nil) return unless enabled?(:protocol) @commands&.[]=([connection, [:id]], [[:method], Utils::ElapsedTime.monotonic_time]) return unless show?([:method]) write(:protocol) { @formatter.sent(, Utils::ElapsedTime.elapsed_time) } end |