How I used Rails Logger to see through

First of all, as developers we love logging. It is the best place to see what’s actually happening inside. So This article will explain about using the logging facility with a Ruby on Rails application.

We can use the rails default logger facility to see and save the information about the process during the run time. This logger file is inherited from ActiveSupport class as a the Logger [ActiveSupport::Logger ].

If you go to the environment directory in your rails application’s config directory you’ll be able to see the configuration file

There are configuration files for each environment. here is the configuration file for the development environment.

In the above file I have added new two lines, first two lines will embed the Logger facilities into your rails application while it run locally. I mean in the development environment.

config.log_level = :debug

The available log levels are: :debug, :info, :warn, :error, :fatal, and :unknown, corresponding to the log level numbers from 0 up to 5, respectively.

The hierarchy can be defined as

Unknown > Fatal > Error > Warn > Info > Debug

Log Tags are the phrases that will be printed in front of each rails request.

config.log_tags = [:uuid, :remote_ip, lambda { |req| Time.now }]

Here I have updated the tags as above. UUID will print a unique ID for each reuqest, Remote IP will show the Internet protocol address where the request came in, Lambda function will is used to print the Current time when the request happens.

Now I will show you how the above changes helped me.

Default rails logger will give the output as follows,

Even I can see the asset loading lines as well, Errr ( I actually don’t need to see them )

Actually for a better understanding we need some additional information with the controller actions/ Rails requests and minimize useless information like asset loading information.

This is what happen when I add the following line in to the log configuration.

config.log_tags = [:uuid, :remote_ip, lambda { |req| Time.now }]
log with the UUID, IP Address and the Time it happens

For the asset loading lines, we can simply add GEM in the gem-file development group.

group :development do
gem 'quiet_assets'
end

Log Rotation

These log files will be filled with lots of information. Once the size gets too large it’ll be unable open the file.

We can always use the tail command to see the current log

tail -f development.log

But, If we do this will more care we can use log rotation to keep the latest log files safe and compress others and remove them after some time. So, space will be utilized and also developers can easily search for the log activities.

I am using Ubuntu 18.04 LTS for the Rails development.

There is a default log rotation mechanism with the Linux OS. We can use that to rotate our rails application log files.

Default logrotate files can be seen in the following folder

cd etc/logrotate.d

Create a separate file for the rails log rotate mechanism

nano rails_diary_web

I have used my project name as the name of the config file. I added following attributes in the config file,

~/Workspace/diary_web/log/*.log {   
daily
missingok
rotate 14
compress
delaycompress
notifempty
copytruncate
}

Attributes we can use

  • daily — Rotate the log files each day. You can also use weekly or monthly here instead.
  • missingok — If the log file doesn’t exist, ignore it
  • rotate 14— Only keep 14days of logs around
  • compress — GZip the log file on rotation
  • delaycompress — Rotate the file one day, then compress it the next day so we can be sure that it won’t interfere with the Rails server
  • notifempty — Don’t rotate the file if the logs are empty
  • copytruncate — Copy the log file and then empties it. This makes sure that the log file Rails is writing to always exists so you won’t get problems because the file does not actually change. If you don’t use this, you would need to restart your Rails application each time.

And the just save the file,

the folder will look like this,

To Check the things are working fine, you can use

sudo logrotate /etc/logrotate.config --debug

it will output the status of the logrotate

Alright then :)

That’s all I want to share about this,

Check this video to see this as a Sinhala Tutorial.

Senior Software Engineer ❤️ Node | Ruby