...

/

Log What Something Is and Where It Came From

Log What Something Is and Where It Came From

Learn about logging and what we should log for our Rails application.

Improving log information

Logs are often relevant to a specific Active Record. Logging the ID is a great way to know which Active Record or row is in the database, but we need to know what type of thing that ID refers to. Further, we might want to know where the log message originated so we can dial into what code was acting on what piece of data.

It would be nice if we could get this for free by calling inspect and having the Rails logger figure out what class called the log method:

log "#{widget.inspect} updated"
## => 2020-07-09 11:34:12 [WidgetCreator] <#Widget id=1234> updated

Unfortunately, this doesn’t work the way we want. First, ...