When it comes to application development, few languages give programmers greater happiness than Ruby—a language designed for programmer happiness—and when it comes to bringing Ruby to the world of web applications, the most common answer is Rails. For those who don’t know, Ruby and Rails have been working together for years to power many of the applications we use on a daily basis. From web applications to mobile, Ruby is everywhere – there are even libraries specifically for IoT and drone application building! When it comes to application building, one of the key technologies Ruby developers can’t live without is logging. Logging is a key piece of the application development process and luckily, there are features like the Ruby Logger class to follow specific bits of code, allowing us to push information to stdout. This allows for easy stack tracing and troubleshooting insights. 

But what happens once we release our code to the wild? Can we still use Logger to grab the information we need in a simple and succinct way? 

Unfortunately, the answer is not that simple. While Logger statements can be left in code and will be pushed out to your logs, there is a cost of keeping this in production. The first cost is on the speed of the application. This may not be overbearing, but it can cause slowness to creep into the application over time. The secondary cost is on the person parsing through all the logs in order to discover relevant information. This can be almost a full-time job. 

Luckily, there is a better way to get a glimpse of our logs without dealing with the cycle of heading to the server, attempting to view logs as root, parsing the Logger messages, and hoping for the best. 

Ruby Logstash Logger Gem

If you have ELK stack familiarity, the easiest solution is to use the Ruby Logstash-Logger Gem, which was last updated on February 14, 2018 with version 0.26.1.  This gem was developed with Logger in mind, but instead of sending the information to a local log, it pushes it to a Logstash Instance. This can then be used to ship logs to Kibana so the information is presented in a more visual, user-friendly way.  

Install this by adding the following to the gemfile:

gem 'logstash-logger'

then executing:

$ bundle

and finally installing:

$ gem install logstash-logger

Another great gem is the Logstash-Formatter. In addition to extending the Logger class, the Logstash-Formatter also seems to format for use with tools other than Logstash (e.g. Redis, Kafka). While it looks like a useful option, this gem has not been updated in over 3 years. 

Gems can often be the first go-to solution for Ruby developers, but if they aren’t well maintained, it may be better to look for options outside the gem ecosystem. 

Rails Logger

Another option is the Rails Logger, (a.k.a. the Ruby Logger). Go to your config/application.rb file—or any environment.rb file—or in the Initializer section and add the following:

Rails.logger = Logger.new(STDOUT)
config.logger = ActiveSupport::Logger.new("log/#{Rails.env}.log")

You can further configure the details. An example logger configuration might look like this:

Rails.logger = Logger.new(STDOUT)
Rails.logger.level = Logger::DEBUG
Rails.logger.datetime_format = "%Y-%m-%d %H:%M:%S"

Heroku

Many Ruby developers have found Heroku to be the first answer to their application cloud hosting needs. This is usually true in the initial release stages before an application hits a larger audience or sees massive traffic. Heroku makes logging easy, especially if you are using a service like Logz.io that is linked to the Heroku Elements marketplace 

Simple add-ons make life much easier to get the logs and insights into the application, helping us to move our applications to the next level. 

Other Cloud Services

The Cloud is still the ultimate answer when it comes to application deployment in this day and age. Furthermore, many cloud providers make it easy to access logs and monitor your application.

Similar to Heroku, Amazon Web Services has many ways to access and use logs for better insights. Other Platforms, like Engine Yard, have been working on better log access for Rails developers as part of their focus for a while. 

Conclusion

Ruby is an essential piece of modern observability—a tool like Fluentd requires Ruby on Rails be installed for it to work. Ruby developers need the best access to their logs in order to better serve their end users. With plenty of options, from gems right in the code to use the tools provided by hosts, it should be easier than ever to see how your application is functioning and discover ways to make it better.

Get started for free

Completely free for 14 days, no strings attached.