Ruby

Collect logs directly from Ruby on Rails, Sinatra, and plain Ruby projects.

Example

It's very easy to start sending logs to Logtail:

require "logtail"
http_device = Logtail::LogDevices::HTTP.new("<your-source-token>")
logger = Logtail::Logger.new(http_device)
logger.info("logtail is ready")
logger.warn(
"log structured data",
item: {
url: "https://fictional-store.com/item-123",
price: 100.00
}
)

As a result, the following two JSON rows will be ingested by Logtail:

{
"dt": "2021-03-29T11:24:54.788Z",
"level": "info",
"message": "logtail integration is ready",
"context": {
"runtime": {
"thread_id": 123,
"file": "script-file.rb",
"line": 6,
"frame": null,
"frame_label": "<main>"
},
"system": {
"hostname": "hostname"
"pid": 1234
}
}
}
{
"dt": "2021-03-29T11:24:54.788Z",
"level": "warn",
"message": "log structured data",
"item": {
"url": "https://fictional-store.com/item-123",
"price": 100.00
},
"context": {
"runtime": {
"thread_id": 123,
"file": "script-file.rb",
"line": 7,
"frame": null,
"frame_label": "<main>"
},
"system": {
"hostname": "hostname"
"pid": 1234
}
}
}

Installation and setup

Run the following command which will add a reference to the logtail gem to your Gemfile:

Rails
Ruby
Rails

Start by installing the gem to your project:

bundle add logtail-rails

Alternatively, add gem "logtail" to your Gemfile manually and then run bundle install.

Then run the following command to create the default config file:

bundle exec rake logtail:install source_token=YOUR_LOGTAIL_SOURCE_TOKEN

This will generate config/initializers/logtail.rb. You need to use your source token obtained from logtail.com. That's all.

Ruby

Start by installing the gem to your project:

bundle add logtail

Alternatively, add gem "logtail" (or logtail-rails, logtail-rack) to your Gemfile manually and then run bundle install.

Setting up the integration is very easy. All you need is a source-token that you'll get after you create a Source in your logtail.com account. With the source token at hand, you can initialize the logger object:

http_device = Logtail::LogDevices::HTTP.new('<your-source-token>')
logger = Logtail::Logger.new(http_device)

Usage

Rails
Ruby
Rails

There are several levels of log messages: debug, info, warn, error, and fatal. Each of these levels has a method that requires a message string as the first argument and accepts additional information as a hash:

Rails.logger.warn(
'log structured data',
name: {
first: "John",
last: "Smith"
},
id: 123456
)
Ruby

There are several levels of log messages: debug, info, warn, error, and fatal. Each of these levels has a method that requires a message string as the first argument and accepts additional information as a hash:

logger.warn(
'log structured data',
name: {
first: "John",
last: "Smith"
},
id: 123456
)

Context

We add information about the current runtime environment and the current process into a context field of the logged item by default.

If you want to add custom information to all logged items (e.g., the ID of the current user), you can do so by adding a custom context:

Logtail.with_context(user: { id: 123 }) do
logger.info('new subscription')
end

This snipped will produce the following JSON:

{
"dt": "2021-03-29T11:24:54.788Z",
"level": "warn",
"message": "new subscription",
"context": {
"runtime": {
"thread_id": 123456,
"file": "script-file.rb",
"line": 2,
"frame": null,
"frame_label": "<main>"
},
"system": {
"hostname": "hostname"
"pid": 1234
},
"user": {
"id": 123
}
}
}

We will automatically add the information about the current user to each log if you're using Ruby on Rails and the Devise gem.

If you're not using Devise or you want to log some additional information for every request your Rails app handles, you can easily implement this using Rails' around_action in your application controller. A simple implementation could look like this:

class ApplicationController < ActionController::Base
around_action :with_logtail_context
private
def with_logtail_context
if user_signed_in?
Logtail.with_context(user_context) { yield }
else
yield
end
end
def user_context
Logtail::Contexts::User.new(
id: current_user.id,
name: current_user.name,
email: current_user.email
)
end
end