Class: Rage::JSONFormatter
- Inherits:
-
Object
- Object
- Rage::JSONFormatter
- Defined in:
- lib/rage/logger/json_formatter.rb
Instance Method Summary collapse
- #call(severity, timestamp, _, message) ⇒ Object
-
#initialize ⇒ JSONFormatter
constructor
A new instance of JSONFormatter.
Constructor Details
#initialize ⇒ JSONFormatter
Returns a new instance of JSONFormatter.
2 3 4 5 6 7 |
# File 'lib/rage/logger/json_formatter.rb', line 2 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
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rage/logger/json_formatter.rb', line 9 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.to_json}," } end if (final = logger[:final]) params, env = final[:params], final[:env] if params && params[:controller] return "{\"tags\":[\"#{[0]}\"],\"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\":[\"#{[0]}\"],\"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 = "{\"tags\":[\"#{[0]}\"],\"timestamp\":\"#{}\",\"pid\":\"#{@pid}\",\"level\":\"#{severity}\"" elsif .length == 2 = "{\"tags\":[\"#{[0]}\",\"#{[1]}\"],\"timestamp\":\"#{}\",\"pid\":\"#{@pid}\",\"level\":\"#{severity}\"" elsif .length == 0 = "{\"timestamp\":\"#{}\",\"pid\":\"#{@pid}\",\"level\":\"#{severity}\"" else = "{\"tags\":[\"#{[0]}\",\"#{[1]}\"" i = 2 while i < .length << ",\"#{[i]}\"" i += 1 end << "],\"timestamp\":\"#{}\",\"pid\":\"#{@pid}\",\"level\":\"#{severity}\"" end "#{},#{context_msg}\"message\":\"#{}\"}\n" end |