Skip to main content

How I used Rails Logger to see through

This article will explain about using the logging facility with a Ruby on Rails application.

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.

Rails Logger

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

Image removed.

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.

Log Levels

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

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

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

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 be 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:

Image removed.

Even I can see the asset loading lines as well!( I actually donÔÇÖt need to see them ) 

Image removed.

For a better understanding, we need some additional information with the controller actions/ Rails requests and minimize less useful information like asset loading data.

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

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

Image removed.
                                                                  log with the UUID, IP Address and the Time it happens

To avoid displaying asset pipeline logs

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

group :development do 
gem 'quiet_assets'

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 with better care we can use log rotation to keep the latest log files safe, compress others and remove them after some time. So, space will be utilized efficiently and 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 { 
rotate 14 

Attributes we can use

1. dailyÔÇèÔÇöÔÇèRotate the log files each day. You can also use weekly or monthly here instead.

2. missingokÔÇèÔÇöÔÇèIf the log file doesnÔÇÖt exist, ignore it

3. rotate 14ÔÇö Only keep 14days of logs around

4. compressÔÇèÔÇöÔÇèGZip the log file on rotation

5. 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

6. notifemptyÔÇèÔÇöÔÇèDonÔÇÖt rotate the file if the logs are empty

7. copytruncateÔÇèÔÇöÔÇèCopy the log file and then empties it. This makes sure that the log file Rails writing to always exists so you wonÔÇÖt get problems because the file does not 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,

Image removed.

To Check the things are working fine, you can use

sudo logrotate /etc/logrotate.config --debug

It will output the status of the logrotate

Image removed.Image removed.

Alright then :)

ThatÔÇÖs all I want to share about this,

Check this video to see this as a Sinhala Tutorial.


03 / January / 2019 Rails by Asitha Bandara



© 2019 Creative Software. All Rights Reserved | Privacy | Terms of Use