Module: Whoop::Main

Defined in:
lib/whoop.rb

Constant Summary collapse

COLORS =
%i[black red green yellow blue magenta cyan white light_black light_red light_green light_yellow light_blue light_magenta light_cyan light_white default].freeze
FORMATS =
%i[plain pretty json sql].freeze
PATTERN =
"-"
COUNT =
80
INDENT =
""
TOP_LINE_CHAR =
""
BOTTOM_LINE_CHAR =
""

Instance Method Summary collapse

Instance Method Details

#whoop(label = nil, pattern: PATTERN, count: COUNT, color: :default, format: :pretty, caller_depth: 0, explain: false, tags: [], context: nil) ⇒ Object

Log the message to the logger

Parameters:

  • label (String) (defaults to: nil)

    (optional) - the label or object to log

  • pattern (String) (defaults to: PATTERN)
    • the pattern to use for the line (e.g. ‘-’)

  • count (Integer) (defaults to: COUNT)
    • the number of times to repeat the pattern per line (e.g. 80)

  • color (Symbol) (defaults to: :default)
    • the color to use for the line (e.g. :red)

  • format (Symbol) (defaults to: :pretty)
    • the format to use for the message (one of :json, :sql, :plain, :semantic, :pretty (default))

  • caller_depth (Integer) (defaults to: 0)
    • the depth of the caller to use for the source (default: 0)

  • explain (Boolean) (defaults to: false)
    • whether to explain the SQL query (default: false)

  • tags (Array<String, Symbol>) (defaults to: [])
    • Any tags you’d like to include in the log

  • context (Hash) (defaults to: nil)
    • Any additional context you’d like to include in the log



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/whoop.rb', line 50

def whoop(
  label = nil,
  pattern: PATTERN,
  count: COUNT,
  color: :default,
  format: :pretty,
  caller_depth: 0,
  explain: false,
  tags: [],
  context: nil
)
  message = block_given? ? yield : label

  if Whoop.logger.is_a?(SemanticLogger::Logger)
    context ||= {}

    if tags.length > 0
      Whoop.logger.tagged(*tags) do
        Whoop.logger.send(Whoop.level.to_sym, message, **context)
      end
    else
      Whoop.logger.send(Whoop.level.to_sym, message, **context)
    end

    return
  end

  if tags.length > 0
    context ||= {}
    context[:tags] = tags
  end

  logger_method = detect_logger_method
  color_method = detect_color_method(color)
  formatter_method = detect_formatter_method(format, colorize: color.present?, explain: explain)
  caller_path = clean_caller_path(caller[caller_depth])
  line = pattern * count
  caller_path_line = [color_method.call(INDENT), "source:".colorize(:light_black).underline, caller_path].join(" ")
  timestamp_line = [color_method.call(INDENT), "timestamp:".colorize(:light_black).underline, Time.now].join(" ")

  context_lines =
    if context.is_a?(Hash) && context.keys.length > 0
      context.map do |k, v|
        [color_method.call(INDENT), "#{k}:".colorize(:light_black).underline, v].join(" ")
      end
    else
      []
    end

  top_line =
    if label.present? && label.is_a?(String)
      wrapped_line(label.to_s, pattern: pattern, count: count)
    else
      pattern * count
    end

  logger_method.call color_method.call top_line
  logger_method.call color_method.call "\n\n#{TOP_LINE_CHAR}#{top_line}"
  logger_method.call timestamp_line
  logger_method.call caller_path_line
  context_lines.each { |l| logger_method.call l }
  logger_method.call ""
  logger_method.call formatter_method.call(message)
  logger_method.call ""
  display_invalid_format_message(format, color_method, logger_method)
  logger_method.call color_method.call "#{BOTTOM_LINE_CHAR}#{line}\n\n"
end