Configure Ruby Logging
  • Updated on 13 Mar 2020
  • 2 minutes to read
  • Contributors
  • Print
  • Share
  • Dark
    Light

Configure Ruby Logging

  • Print
  • Share
  • Dark
    Light

To configure your Ruby application, an Application Programming Interface (API) can also be used to send Errors and Logs to Retrace. The step-by-step process is presented below and for more information, visit this Stackify API for Ruby Github page: https://github.com/stackify/stackify-api-ruby.

Installation and Configuration

Requirements:

  • Ruby 1.9/2.0/2.1
  • Rails 3.x/4.x (optional)

Rails

Add the Stackify gem to your Gemfile. In the Gemfile:

gem 'stackify-api-ruby'

Run the bundle command to install it using the following command:

$ bundle install

Or install it manually:

$ gem install stackify-api-ruby

Once you are done installing stackify-api-ruby, you need to run the generator:

$ rails g stackify --api_key=your_api_key

The generator creates a file 'config/initializers/stackify.rb' that configures stackify-api-ruby with your API key. You can change the default settings in there.

Non-Rails

When using stackify-api-ruby gem within any Ruby application, add the following code snippet on the top of your main file:

require 'stackify-api-ruby'

Once you are done, you need to make a base configuration:

Stackify.setup do |config|
  config.api_key = "your_api_key"
  config.env = :development
  config.app_name = "Your app name"
end

Optional Configuration

The gem has three modes of configuration: "both", "logging", and "metrics". The mode "both" turns on both parts of gem - logging and metrics. If you only need one, either logging or metrics, choose "logging" or "metrics" mode accordingly.

config.mode = :metrics

If you want to use a proxy for sending a request, you can configure it like this:

config.proxy = { uri: '127.0.0.1:8118', user: 'user_name', password: 'some_password' }

Log API Usage

Rails

The stackify-api-ruby runs when Rails server is started. Every error, which occurs within your application, will be caught and sent to Retrace automatically. It has the same process with logs - you simply use the Rails logger using the following code snippet:

Rails.logger.info "Some log message"

If you want to redefine the logger, you should add the following

config.logger = ::Stackify::LoggerProxy.new(ActiveSupport::Logger.new(Rails.root.join('log', "#{Rails.env}.log")))

to your config/environments/%environment%.

In this case, Stackify#config.log_level will affect entire system.

Non-Rails

Once you are done with logs configuration, you should wrap up your logger:

logger = Logger.new('mylog.log')
logger = Stackify::LoggerProxy.new(logger)

And the last thing you need to do is to call the method "run":

Stackify.run #remember that this call is running in the background of your main script

Metrics API Usage

Prerequisite

Metrics requires Ruby APM to be enabled.

There are four different types of metrics:

  • Gauge: Keeps track of the last value that was set in the current minute
Stackify::Metrics.set_gauge "MyCategory", "MyGauge", 100
  • Counter: Calculates the rate per minute
Stackify::Metrics.count "MyCategory", "MyCounter"
  • Average: Calculates the average of all values in the current minute
Stackify::Metrics.average "MyCategory", "AverageSpeed", 200
  • Timer: Calculates the average elapsed time for an operation in the current minute
t = Time.now
Stackify::Metrics.time "MyCategory", "ElapsedTime", t
# or
Stackify::Metrics.time_duration "MyCategory", "ElapsedTime", 5.seconds
  • Counter and Timer: Composite of the Counter and Timer metrics for convenience
t = Time.now
Stackify::Metrics.count_and_time "Metric", "CounterWithTime", t<

We can configure every metric with settings:

settings = Stackify::Metrics::MetricSettings.new
settings.autoreport_zero_if_nothing_reported = true
# or
settings.autoreport_last_value_if_nothing_reported = true
Stackify::Metrics.set_gauge "MyCategory", "MyGauge", 100 , settings

The "autoreport_last_value_if_nothing_reported" property has influence only on "average" metric.

Troubleshooting

If there are problems, you can enable internal logging of the stackify-api-ruby project. You need to uncomment the config.logger and config.logger.level lines in the 'config/initializers/stackify.rb' file:

config.logger = Logger.new(Rails.root.join("log", "stackify.log"))
config.logger.level = Logger::DEBUG
Was this article helpful?