Let's look at the basic setup of the Rails logger and how to customize it for our needs.When setting up our Rails logger, we have several options to consider. In your file, add the following: Of course, we don't want to just start there. And that might be a bit much for normal production use. Once we realize we don't want everything logged all the time, we may decide to set our logger level.Before we move on to best practices, I want to make one additional note.When we're in a Ruby controller or model, we can log by just adding For those of you who've been in the Ruby or Rails ecosystem for a while, that may be a no-brainer. As mentioned earlier, we can set different logging levels for each environment.
However, since in the second example we're technically logging a block of code (indicated by the curly braces), the string concatenation and any other processing required for the log message aren't completed unless the log level is set to debug.In contrast, in the first example, the application concatenates the string before inspecting whether the log level applies.By default, we typically log into environment-specific log files in our Rails app.You can tag user-generated request UUIDs, particular logging use cases, or relevant categories based on the code that executes.
For example, let's consider tagging logs with a request ID. Well, it will provide an easy way to see all the log messages related to a particular request. You're able to see everything that happened to that request with some simple configuration. Now our log messages will include the request ID in every log message that's part of the requesting thread.In fact, we should consider setting different logging levels for different environments.