-
-
Notifications
You must be signed in to change notification settings - Fork 300
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
Comments
You can call them using: |
@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. |
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: Datadog has some mapping mechanisms built in already but I'm not sure if they kick-in always, anyway it should be possible |
Currently you have to use a workaround. Override private method in the 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 # 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 |
My message looks like this:
But I would like to have my message to contain the log level.
I tried a lot of things: inspect the controller, the event payload and the
data
fromcall
method.The text was updated successfully, but these errors were encountered: