logist icon indicating copy to clipboard operation
logist copied to clipboard

Custom/Request Keys

Open zombor opened this issue 6 years ago • 5 comments

Any way to modify this to be able to include tagged log entries? I'd like to be able to have output like:

{"msg":"foo","request_id":"<some-uuid>"}

along with some other custom things.

I'm not too familiar with rails internals to be able to do this myself, but if someone could give me a direction I could give it a shot. I like the simplicity of this gem compared to some other rails json loggers I've been trying out.

zombor avatar Dec 24 '18 19:12 zombor

If you provide the tag in your log, it is outputted. For example:

logger.info({
  msg: "foo",
  request_id: "<some-uuid>"
})
#=> {"level": "info", "timestamp": "2018-12-26T00:26:18", "environment": "development", "msg": "foo", "request_id": "<some-uuid>"}

On one hand, if you want to setup custom tag in log format, it is difficult. Because now user can not customize the log format. It is defined here: https://github.com/h3poteto/logist/blob/v0.1.0/lib/logist/formatter/json.rb

h3poteto avatar Dec 25 '18 15:12 h3poteto

Yep, I understand I can pass a hash to the log method, but I meant having things automatically included per request-scope like the built-in rails tagging feature. Wrapping the logist instance in a TaggedLogger doesn't work how I want since the tags come built into the msg in brackets. Other loggers have this feature, but they are all too big and unwieldy for me.

zombor avatar Dec 25 '18 18:12 zombor

@zombor I wanted to do something similar to yours, didn't find a very clean solution for it but this worked


class JsonFormatter < ActiveSupport::Logger::SimpleFormatter
  def call(severity, timestamp, _progname, message)
    { 
      type: severity,
      time: timestamp,
      message: message, 
      request_id: begin current_tags[0] rescue "undefined" end
    }.to_json + "\n"
  end
end 

In config/environments/.rb

config.log_formatter = JsonFormatter.new
config.log_tags = [:request_id]

Again this covers the global tag but gives trouble when you are using custom tags :(

vik-y avatar Jan 04 '20 11:01 vik-y

It seems like it would be good to allow the existing lograge.custom_options functionality to be configured, in addition to what happens in Logist::Railtie. That would allow for global configuration of additional options as @zombor requested

md5 avatar Jul 07 '21 00:07 md5

After thinking some more about this, I realized that lograge.custom_options is not enough, since not all log messages going through logist are originating from lograge. I think something more like a config.logist.custom_options that replaces the current manipulation of lograge.custom_options would be more appropriate.

md5 avatar Jul 07 '21 04:07 md5