lograge icon indicating copy to clipboard operation
lograge copied to clipboard

How to add the log level (warn, info, debug, etc) to the log?

Open mauricioabreu opened this issue 5 years ago • 4 comments

My message looks like this:

[2020-06-08 14:43:08 +0000] - method=GET request_uri=/ status=200 request_id=0b78baef-5e75-4c79-832e-ce075c533571 request_time=0.0

But I would like to have my message to contain the log level.

[2020-06-08 14:43:08 +0000] [WARN] - method=GET request_uri=/ status=200 request_id=0b78baef-5e75-4c79-832e-ce075c533571 request_time=0.0

I tried a lot of things: inspect the controller, the event payload and the data from call method.

mauricioabreu avatar Jun 08 '20 14:06 mauricioabreu

You can call them using: Lograge.logger.send(:LOG_LEVEL, PUT_MESSAGE_HERE).

teekenl avatar Jun 29 '20 07:06 teekenl

You can call them using: Lograge.logger.send(:LOG_LEVEL, PUT_MESSAGE_HERE).

@teekenl Where would you add this line?

I probably misunderstand something. It sounds like that's a direct call to the lograge logger, as opposed to adding the log level into the formatter. Please correct me if I'm wrong.

I'm also interested in knowing how to add some elements from standard rails log format to the formatter.

fieldse avatar Sep 23 '20 02:09 fieldse

I think semantic_logger takes it straight from the Rails logger: https://github.com/rocketjob/semantic_logger/blob/master/lib/semantic_logger/formatters/raw.rb#L35 and it would be the desired behaviour here. Having the log "level" value in the json output is important to properly organize log entries in systems such as datadog: https://docs.datadoghq.com/logs/log_collection/ruby/ https://docs.datadoghq.com/logs/processing/processors/?tab=ui#log-status-remapper

Datadog has some mapping mechanisms built in already but I'm not sure if they kick-in always, anyway it should be possible https://docs.datadoghq.com/logs/faq/how-to-remap-custom-severity-values-to-the-official-log-status/

januszm avatar Feb 01 '21 20:02 januszm

Currently you have to use a workaround. Override private method in the ApplicationController of your app:

  private

  def append_info_to_payload(payload)
    super
    payload[:level] =
      case payload[:status]
      when (200..399)
        "INFO"
      when (400..499)
        "WARN"
      else
        "ERROR"
      end
    payload[:host] = request.host
    payload[:remote_ip] = request.remote_ip
    payload[:ip] = request.ip
  end
end

and then use this information in config.lograge.custom_options = lambda do |event| in lograge.rb initializer or config/environments/production.rb (depending how you use lograge)

# Lograge config
config.colorize_logging = false
config.lograge.formatter = Lograge::Formatters::Json.new
config.lograge.enabled = true
config.lograge.keep_original_rails_log = true
config.lograge.logger = ActiveSupport::Logger.new "#{Rails.root}/log/#{Rails.env}_json.log"
config.lograge.custom_options = lambda do |event|
  {
    application: Rails.application.class.parent_name.downcase,
    exception: event.payload[:exception]&.first,
    host: event.payload[:host],
    ip: event.payload[:ip],
    level: event.payload[:level],
    params: event.payload[:params].except(:action, :controller).to_json,
    process_id: Process.pid,
    rails_env: Rails.env,
    remote_ip: event.payload[:remote_ip],
    request_id: event.payload[:headers]['action_dispatch.request_id'],
    request_time: Time.now,
    x_forwarded_for: event.payload[:x_forwarded_for],
  }.compact
end

januszm avatar Feb 01 '21 22:02 januszm