Logging in Rails Applications
Log levels
From 0 to 5 in severity:
debug
- for debugging purposes, onlyinfo
- an informative messagewarn
- this may or may not be an issueerror
- this is an issue, but the application can continue runningfatal
- this is and issue, and the application should haltunknown
- we don’t know (?)
A corresponding Rails.logger.level
method exists for each of these levels.
Logging important events in the application
When writing to the log, give the developer some “breadcrumbs” to follow. Specify the class name and method name that is writing to the log.
msg = "[#{self.class.name}##{__method__}] something important happened that we should log."
Rails.logger.info(msg)
If you are rescue
ing, it probably makes sense to log the error, as well.
begin
# some code that might fail
rescue StandardError => err
Rails.logger.error("[#{self.class.name}##{__method__}] raised an error.")
Rails.logger.error(err)
# handle the error
end
Download logs from a remote source using Secure Copy (scp)
$ scp -p username@hostname:~/path/to/log/* ~/path/to/log
Searching Logs
The following command will show you three lines before and three lines after any
occurrence of ClassName
.
$ cat production.log | grep -B 3 -A 3 "ClassName"