Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mauricioabreu opened this issue Jun 8, 2020 · 4 comments
Open

Comments

@mauricioabreu
Copy link

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.

@teekenl
Copy link

teekenl commented Jun 29, 2020

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

@fieldse
Copy link

fieldse commented Sep 23, 2020

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.

@januszm
Copy link

januszm commented Feb 1, 2021

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
Copy link

januszm commented Feb 1, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants