Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rails tagged logging, how can I log the log level of a message?

My rails logger configuration currently logs the current time and UUID, like this:

config.logger = ActiveSupport::TaggedLogging.new(Logger.new("#{Rails.root}/log/#{ENV['RAILS_ENV']}.log", 'daily'))
config.log_tags = [Proc.new {Time.now.strftime('%Y-%m-%d %H:%M:%S.%L')}, :uuid]

Is there a way to also log the current message's log level?

i.e., if I call logger.debug, the tag [DEBUG] is added to the message.

like image 350
Ovesh Avatar asked May 17 '12 01:05

Ovesh


People also ask

Where can I see rails logs?

In a Rails app, logs are stored under the /log folder. In development mode, the development. log file is used & you see log output on the terminal you're running rails server on.

How do I use logger in Ruby on rails?

To write in the current log use the logger. (debug|info|warn|error|fatal|unknown) method from within a controller, model, or mailer: logger. debug "Person attributes hash: #{@person.

What is the log to check for errors in rails?

Rails uses six different log levels: debug, info, warn, error, fatal, and unknown. Each level defines how much information your application will log: Debug: diagnostic information for developers and system administrators, including database calls or inspecting object attributes.


1 Answers

Railscasts shows how to override the logger to output the severity of the log message by overriding the SimpleFormatter#call method:

class Logger::SimpleFormatter
  def call(severity, time, progname, msg)
    "[#{severity}] #{msg}\n"
  end
end

See http://railscasts.com/episodes/56-the-logger-revised for the details.

like image 106
Jacob Atzen Avatar answered Oct 05 '22 11:10

Jacob Atzen