Class: Maze::Loggers::LogUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/loggers/log_util.rb

Overview

A collection of logging utilities

Class Method Summary collapse

Class Method Details

.linkify(url, text) ⇒ Object

Produces a clickable link when logged in Buildkite

Parameters:

  • url (String)

    Link URL

  • text (String)

    Link text



49
50
51
52
53
54
55
# File 'lib/maze/loggers/log_util.rb', line 49

def linkify(url, text)
  if ENV['BUILDKITE']
    "\033]1339;url='#{url}';content='#{text}'\a"
  else
    "#{text}: #{url}"
  end
end

.log_hash(severity, data) ⇒ Object

Logs Hash-based data, accounting for things like file upload requests that are too big to log meaningfully.

Parameters:

  • severity (Integer)

    A constant from Logger::Severity

  • data (Hash)

    The data to log (currently needs to be a Hash)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/maze/loggers/log_util.rb', line 12

def log_hash(severity, data)
  return unless data.is_a? Hash

  # Try to pretty print as JSON, if not too big
  begin
    json = JSON.pretty_generate data
    if json.length < 128 * 1024
      $logger.add severity, json
    else
      log_hash_by_field severity, data
    end
  rescue Encoding::UndefinedConversionError => error
    # Just give up, we don't want to risk a further error trying to log garbage
    Bugsnag.notify error
    $logger.error 'Unable to log hash as JSON'
  end
end

.log_hash_by_field(severity, hash) ⇒ Object

Logs a hash field by field,

Parameters:

  • severity (Integer)

    A Logger::Severity

  • hash (Hash)

    The Hash



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/maze/loggers/log_util.rb', line 34

def log_hash_by_field(severity, hash)
  hash.keys.each do |key|
    value = hash[key].to_s
    if value.length < 1024
      $logger.add severity, "  #{key}: #{value}"
    else
      $logger.add severity, "  #{key} (length): #{value.length}"
      $logger.add severity, "  #{key} (start): #{value[0, 1024]}"
    end
  end
end