Class: Rage::JSONFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/logger/json_formatter.rb

Overview

JSON formatter for Rage logger.

Produces log lines in JSON format, including tags, context, and request details if available.

Example log line:

{"tags":["fecbba0735355738"],"timestamp":"2025-10-19T11:12:56+00:00","pid":"1825","level":"info","method":"GET","path":"/api/v1/resource","controller":"Api::V1::ResourceController","action":"index","status":200,"duration":0.15}

Use Rage.configure to set the formatter:

Rage.configure do |config|
  config.log_formatter = Rage::JSONFormatter.new
end

Instance Method Summary collapse

Constructor Details

#initializeJSONFormatter

Returns a new instance of JSONFormatter.



21
22
23
24
25
26
# File 'lib/rage/logger/json_formatter.rb', line 21

def initialize
  @pid = Process.pid.to_s
  Iodine.on_state(:on_start) do
    @pid = Process.pid.to_s
  end
end

Instance Method Details

#call(severity, timestamp, _, message) ⇒ Object



28
29
30
31
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
59
60
61
62
63
64
# File 'lib/rage/logger/json_formatter.rb', line 28

def call(severity, timestamp, _, message)
  logger = Thread.current[:rage_logger] || { tags: [], context: {} }
  tags, context = logger[:tags], logger[:context]

  if !context.empty?
    context_msg = ""
    context.each { |k, v| context_msg << "\"#{k}\":#{v.to_json}," }
  end

  tags_msg = if tags.length == 1
    "{\"tags\":[\"#{tags[0]}\"],"
  elsif tags.length == 2
    "{\"tags\":[\"#{tags[0]}\",\"#{tags[1]}\"],"
  elsif tags.length == 0
    "{"
  else
    msg = "{\"tags\":[\"#{tags[0]}\",\"#{tags[1]}\""
    i = 2
    while i < tags.length
      msg << ",\"#{tags[i]}\""
      i += 1
    end
    msg << "],"
  end

  if (final = logger[:final])
    params, env = final[:params], final[:env]
    if params && params[:controller]
      return "#{tags_msg}\"timestamp\":\"#{timestamp}\",\"pid\":\"#{@pid}\",\"level\":\"info\",\"method\":\"#{env["REQUEST_METHOD"]}\",\"path\":\"#{env["PATH_INFO"]}\",\"controller\":\"#{Rage::Router::Util.path_to_name(params[:controller])}\",\"action\":\"#{params[:action]}\",#{context_msg}\"status\":#{final[:response][0]},\"duration\":#{final[:duration]}}\n"
    else
      # no controller/action keys are written if there are no params
      return "#{tags_msg}\"timestamp\":\"#{timestamp}\",\"pid\":\"#{@pid}\",\"level\":\"info\",\"method\":\"#{env["REQUEST_METHOD"]}\",\"path\":\"#{env["PATH_INFO"]}\",#{context_msg}\"status\":#{final[:response][0]},\"duration\":#{final[:duration]}}\n"
    end
  end

  "#{tags_msg}\"timestamp\":\"#{timestamp}\",\"pid\":\"#{@pid}\",\"level\":\"#{severity}\",#{context_msg}\"message\":\"#{message}\"}\n"
end