Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, and more...

Twitter:
@daniel_moralesp

2020-03-06

Logs with Lograge, Syslog and Papertrail

This is a tutorial on how to have a better logging with Rails

1- Lograge


Lograge is an attempt to bring sanity to Rails' noisy and unusable, unparsable and, in the context of running multiple processes and servers, unreadable default logging output. Rails' default approach to log everything is great during development, it's terrible when running it in production. It pretty much renders Rails logs useless to me.

GitHub - roidrage/lograge: An attempt to tame Rails' default policy to log everything.

Let's play with lograge

Install the gem

gem 'lograge'

# bundle install

Create the file /config/initializers/lograge.rb

# /config/initializers/lograge.rb

Rails.application.configure do
    config.lograge.enabled = true
    config.lograge.base_controller_class = 'ActionController::API'

    config.lograge.custom_options = lambda do |event|
        event.payload

        # Following this tutorial: <https://dev.to/mikerogers0/lorgage-taming-rails-logs-video-25il>
        # Rails.logger.debug(event.payload.inspect)
        # exceptions = [:controller, :action]
        # {
        #     params: event.payload[:params].except(*exceptions)
        #     # exception: event.payload[:exception], # ["ExceptionClass", "the message"]
        #     # exception_object: event.payload[:exception_object] # the exception instance
        # }
    end
end

The commented lines was to play following this tutorial:

Lorgage - Taming Rails Logs (Video)

Inside the application controller add

# app/controllers/application_controller.rb

def append_info_to_payload(payload)
    super
    payload[:user_id] = current_user_parso.try(:id)
    payload[:host] = request.host
    payload[:source_ip] = request.remote_ip
end

Turn on rails server, make a request to rooth_path / from your browser and now you have a log in just one line, something like this

method=GET path=/ format=html controller=Rails::WelcomeController action=index status=200 duration=127.37 view=71.80 db=0.00 params={"controller"=>"rails/welcome", "action"=>"index"} headers=#<ActionDispatch::Http::Headers:0x00000000048ddae8> view_runtime=71.80 db_runtime=0


2- Adding gem 'remote_syslog_logger' and connect with Papertrail


At this point we're ready to play with this other blog post:

Introduction To Logging In Ruby On Rails - Papertrail

And we'll start from here: Troubleshoot Problems Faster Using Papertrail

Now we're going to add this gem

gem 'remote_syslog_logger'

# bundle install

Once we've this gem installed, we need to go to this link, https://www.papertrail.com/plans/ and create a free account inside the free plan (they don't require credit card)

Screen Shot 2022-05-29 at 1.54.30 PM.png 603.06 KB

Create your account and you end up in a dashboard like this

Screen Shot 2022-05-29 at 1.54.39 PM.png 425.32 KB

Got to this menu and click in log destinations

Screen Shot 2022-05-29 at 1.54.47 PM.png 250.78 KB

Then you'll see your DNS and Port





Now go to your Rails app again and change this accordingly in each of this files

# config/environments/development.rb

config.logger = RemoteSyslogLogger.new('logs6.papertrailapp.com', 
                                            36377, 
                                            program: "rails-development")

# config/environments/production.rb
config.logger = RemoteSyslogLogger.new('logs6.papertrailapp.com', 
                                          36377, 
                                          program: "rails-production")

# config/environments/staging.rb
config.logger = RemoteSyslogLogger.new('logs6.papertrailapp.com', 
                                            36377, 
                                            program: "rails-staging")

# config/environments/test.rb
config.logger = RemoteSyslogLogger.new('logs6.papertrailapp.com', 
                                          36377, 
                                          program: "rails-test")


3- Test your logs in all environments


Now we need to check if everything is in place. Turn on your rails server and make a request in any route, something like this



Screen Shot 2022-05-29 at 1.55.12 PM.png 362.34 KB




Screen Shot 2022-05-29 at 1.55.29 PM.png 322.09 KB
A route that doesn't exist


Screen Shot 2022-05-29 at 1.55.43 PM.png 384.2 KB