1

I have one app that is a Sinatra app. I would like to have the log of each request on one line, Currently only 1 line per request is shown(the request itself). But I want to append custom info to it.

This is my current line:

[02/Feb/2018:11:52:32 +0000] "GET /feed/name.csv HTTP/1.0" 200 - 0.1620
2400:gh654:32:6532:0:0:a43e:404f, 0.0.0.0:2017 - - [05/Feb/2018:13:32:48 +0000] "HEAD / HTTP/1.0" 404 18 0.0676
2400:gh654:32:6532:0:0:a43e:414d, 0.0.0.0:2017 - - [05/Feb/2018:13:32:48 +0000] "HEAD / HTTP/1.0" 404 18 0.0041

I want to transform it in something like:

INFO  2018-03-02T14:03:36Z [main] [066843] [8a6e846f-45b7-433d-9d98-21c8c0766ad7] method=GET path= /some/path.csv format=csv action=action_name status=200 duration=459.97 view=154.94 db=172.96 user=user_id ip=user_id on=protocol_used agent=agent_used params=params_used

In the past to do this in rails applications I have used lograge but it seems it is not supported by sinatra apps.

Have you guys ever had to append custom info to the sinatra logs?

This is my configuration for logging:

configure do
  enable :logging
  file = File.new("#{RAILS_ROOT}/log/#{settings.environment}.log", 'a+')
  file.sync = true
  use Rack::CommonLogger, file
  set :show_exceptions, true
  set :logging,  true

  ActiveRecord::Base.logger = Logger.new("#{RAILS_ROOT}/log/#{settings.environment}.log")
end
Lucian Tarna
  • 1,806
  • 4
  • 25
  • 48
  • 1
    Can you post an example of what Sinatra is logging now vs. what you want it to do? – Joe Mar 01 '18 at 17:57
  • It’s possible that the webserver you’re using is also logging the request, which could explain multiple lines per request. – matt Mar 01 '18 at 18:11
  • I have updated the question @Joe – Lucian Tarna Mar 02 '18 at 14:38
  • That's weird, this value should just be the ip: `2400:gh654:32:6532:0:0:a43e:404f, ip` so it's weird to see different IPv6 addresses and the string ", ip" there as well... – Joe Mar 02 '18 at 14:49
  • oh sorry i just replaced the ip with the word IP , the other string are random(they were there, i kept the form but changed the values) things I put also :D – Lucian Tarna Mar 02 '18 at 14:52
  • My problem is that I can't find a library that will put on a single line all the info and the only way I can do it is on multiple lines which is not what I want at all – Lucian Tarna Mar 02 '18 at 14:57
  • Is that syslog format? – ian Mar 02 '18 at 19:25
  • This will probably answer your question https://stackoverflow.com/a/2240842/335847 – ian Mar 02 '18 at 19:35
  • seems so, will test it on monday and will let you know! – Lucian Tarna Mar 02 '18 at 21:01
  • @iain but this would get me to write on 2 lines I think in the log. I would like to append it to the first line somehow – Lucian Tarna Mar 05 '18 at 10:20
  • 1
    @LucianTarna Either monkeypatch/prepend the call to `log`, but as [Rack Commonlogger is so small](https://github.com/rack/rack/blob/master/lib/rack/common_logger.rb) I'd copy it and change the bits I need changed to come up with my own logger. Rack::Syslogger sound good? :) – ian Mar 05 '18 at 10:33
  • @iain ok, that was what I thought I just hoped there is a better way. Thank you! Please post it as the answer – Lucian Tarna Mar 05 '18 at 10:34

0 Answers0