Class: Makit::Logging::Formatters::PlainTextFormatter

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

Overview

Plain text formatter with just the message

Formats log requests as plain text containing only the message content. This is useful for simple log files where only the message matters.

Examples:

formatter = PlainTextFormatter.new
formatted = formatter.format(log_request)
# => 'User logged in'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include_context: false) ⇒ PlainTextFormatter

Initialize plain text formatter

Parameters:

  • include_context (Boolean) (defaults to: false)

    whether to include context in output



24
25
26
# File 'lib/makit/logging/formatters/plain_text_formatter.rb', line 24

def initialize(include_context: false)
  @include_context = include_context
end

Instance Attribute Details

#include_contextBoolean (readonly)

Returns whether to include context in output.

Returns:

  • (Boolean)

    whether to include context in output



19
20
21
# File 'lib/makit/logging/formatters/plain_text_formatter.rb', line 19

def include_context
  @include_context
end

Instance Method Details

#configHash

Get formatter configuration

Returns:

  • (Hash)

    formatter configuration



63
64
65
66
67
# File 'lib/makit/logging/formatters/plain_text_formatter.rb', line 63

def config
  super.merge(
    include_context: @include_context,
  )
end

#format(log_request) ⇒ String

Format log request as plain text

Parameters:

  • log_request (LogRequest)

    the log request to format

Returns:

  • (String)

    plain text formatted log entry



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/makit/logging/formatters/plain_text_formatter.rb', line 32

def format(log_request)
  message = log_request.message

  # Add context if enabled and present
  if @include_context && !log_request.context.empty?
    # Create a more readable context representation
    context_parts = log_request.context.map do |k, v|
      # Format values nicely based on their type
      formatted_value = case v
        when String
          v.include?(" ") ? "\"#{v}\"" : v
        when Hash, Array
          v.inspect
        when Time
          v.strftime("%Y-%m-%d %H:%M:%S")
        else
          v.to_s
        end
      "#{k}: #{formatted_value}"
    end

    context_str = context_parts.join(", ")
    message += " (#{context_str})"
  end

  message
end