Class: Rage::TextFormatter
- Inherits:
-
Object
- Object
- Rage::TextFormatter
- Defined in:
- lib/rage/logger/text_formatter.rb
Overview
Text formatter for Rage logger.
Example log line:
[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::TextFormatter.new
end
Instance Method Summary collapse
- #call(severity, timestamp, _, message) ⇒ Object
-
#initialize ⇒ TextFormatter
constructor
A new instance of TextFormatter.
Constructor Details
#initialize ⇒ TextFormatter
Returns a new instance of TextFormatter.
19 20 21 22 23 24 |
# File 'lib/rage/logger/text_formatter.rb', line 19 def initialize @pid = Process.pid Iodine.on_state(:on_start) do @pid = Process.pid end end |
Instance Method Details
#call(severity, timestamp, _, message) ⇒ Object
26 27 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/text_formatter.rb', line 26 def call(severity, , _, ) logger = Thread.current[:rage_logger] || { tags: [], context: {} } , context = logger[:tags], logger[:context] if !context.empty? context_msg = "" context.each { |k, v| context_msg << "#{k}=#{v} " } end if (final = logger[:final]) params, env = final[:params], final[:env] = .map { |tag| "[#{tag}]" }.join if params && params[:controller] return "#{} 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 "#{} 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 if .length == 1 = "[#{[0]}] timestamp=#{} pid=#{@pid} level=#{severity}" elsif .length == 2 = "[#{[0]}][#{[1]}] timestamp=#{} pid=#{@pid} level=#{severity}" elsif .length == 0 = "timestamp=#{} pid=#{@pid} level=#{severity}" else = "[#{[0]}][#{[1]}]" i = 2 while i < .length << "[#{[i]}]" i += 1 end << " timestamp=#{} pid=#{@pid} level=#{severity}" end "#{} #{context_msg}message=#{}\n" end |