Class: Makit::Logging::Formatters::ConsoleFormatter

Inherits:
Base
  • Object
show all
Defined in:
lib/makit/logging/formatters/console_formatter.rb

Overview

Console formatter for colored log output

Formats log requests for console display with colors, symbols, and optional timestamps/levels. This is specifically designed for human-readable console output.

Examples:

formatter = ConsoleFormatter.new
formatted = formatter.format(log_request)
# => "→ Processing started" (with appropriate color)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(show_timestamp: false, show_level: false) ⇒ ConsoleFormatter

Initialize console formatter

Parameters:

  • show_timestamp (Boolean) (defaults to: false)

    whether to show timestamps (default: false)

  • show_level (Boolean) (defaults to: false)

    whether to show log levels (default: false)



37
38
39
40
# File 'lib/makit/logging/formatters/console_formatter.rb', line 37

def initialize(show_timestamp: false, show_level: false)
  @show_timestamp = show_timestamp
  @show_level = show_level
end

Instance Attribute Details

#show_levelBoolean (readonly)

Returns whether to show log levels.

Returns:

  • (Boolean)

    whether to show log levels



31
32
33
# File 'lib/makit/logging/formatters/console_formatter.rb', line 31

def show_level
  @show_level
end

#show_timestampBoolean (readonly)

Returns whether to show timestamps.

Returns:

  • (Boolean)

    whether to show timestamps



29
30
31
# File 'lib/makit/logging/formatters/console_formatter.rb', line 29

def show_timestamp
  @show_timestamp
end

Instance Method Details

#configHash

Get formatter configuration

Returns:

  • (Hash)

    formatter configuration



85
86
87
88
89
90
# File 'lib/makit/logging/formatters/console_formatter.rb', line 85

def config
  super.merge(
    show_timestamp: @show_timestamp,
    show_level: @show_level,
  )
end

#format(log_request) ⇒ String

Format log request for console display

Parameters:

  • log_request (LogRequest)

    the log request to format

Returns:

  • (String)

    console formatted log entry with colors



46
47
48
49
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
# File 'lib/makit/logging/formatters/console_formatter.rb', line 46

def format(log_request)
  parts = []

  # Add timestamp if enabled
  if @show_timestamp
    timestamp = log_request.timestamp.strftime("%H:%M:%S")
    parts << "[#{timestamp}]".colorize(:light_black)
  end

  # Add log level if enabled
  if @show_level
    level_str = log_request.level.to_s.upcase
    level_color = get_level_color(log_request.level)
    parts << level_str.colorize(level_color)
  end

  # Add symbol and message
  symbol = get_level_symbol(log_request.level)
  symbol_color = get_level_color(log_request.level)
  message = log_request.message

  # Special handling for task messages - no symbol, no context, just the message
  if log_request.context[:task_message]
    parts << message
  else
    # Add context if present (only for non-task messages)
    unless log_request.context.empty?
      context_str = log_request.context.map { |k, v| "#{k}=#{v}" }.join(" ")
      message += " #{context_str}".colorize(:light_black)
    end
    parts << "#{symbol} #{message}".colorize(symbol_color)
  end

  parts.join(" ")
end