h3poteto / logist

A json logger for Rails
MIT License
26 stars 9 forks source link

Custom/Request Keys #5

Open zombor opened 5 years ago

zombor commented 5 years ago

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.

h3poteto commented 5 years ago

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

zombor commented 5 years ago

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.

vik-y commented 4 years ago

@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 :(

md5 commented 3 years ago

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 commented 3 years ago

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.