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 1

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

https://gist.github.com/AsithaBandara/24fa4191786777a4ad8a1a4bae45486d

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| 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 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 1

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

image 2

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| Time.now }]

image
                                                                  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'
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 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 { 
daily 
missingok 
rotate 14 
compress 
delaycompress 
notifempty 
copytruncate 
}

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

To Check the things are working fine, you can use

sudo logrotate /etc/logrotate.config --debug

It will output the status of the logrotate

imageimage

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

LEAVE A COMMENT







POST COMMENTS


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