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

Example

It's very easy to start sending logs to Logtail:
1
require "logtail"
2
​
3
http_device = Logtail::LogDevices::HTTP.new("<your-source-token>")
4
logger = Logtail::Logger.new(http_device)
5
​
6
logger.info("logtail is ready")
7
logger.warn(
8
"log structured data",
9
item: {
10
url: "https://fictional-store.com/item-123",
11
price: 100.00
12
}
13
)
Copied!
As a result, the following two JSON rows will be ingested by Logtail:
1
{
2
"dt": "2021-03-29T11:24:54.788Z",
3
"level": "info",
4
"message": "logtail integration is ready",
5
"context": {
6
"runtime": {
7
"thread_id": 123,
8
"file": "script-file.rb",
9
"line": 6,
10
"frame": null,
11
"frame_label": "<main>"
12
},
13
"system": {
14
"hostname": "hostname"
15
"pid": 1234
16
}
17
}
18
}
19
​
20
{
21
"dt": "2021-03-29T11:24:54.788Z",
22
"level": "warn",
23
"message": "log structured data",
24
"item": {
25
"url": "https://fictional-store.com/item-123",
26
"price": 100.00
27
},
28
"context": {
29
"runtime": {
30
"thread_id": 123,
31
"file": "script-file.rb",
32
"line": 7,
33
"frame": null,
34
"frame_label": "<main>"
35
},
36
"system": {
37
"hostname": "hostname"
38
"pid": 1234
39
}
40
}
41
}
Copied!

Installation and setup

Run the following command which will add a reference to the logtail gem to your Gemfile:
Rails
Ruby
Start by installing the gem to your project:
1
bundle add logtail-rails
Copied!
Alternatively, add gem "logtail" to your Gemfile manually and then run bundle install.
Then run the following command to create the default config file:
1
bundle exec rake logtail:install source_token=YOUR_LOGTAIL_SOURCE_TOKEN
Copied!
This will generate config/initializers/logtail.rb. You need to use your source token obtained from logtail.com. That's all.
Start by installing the gem to your project:
1
bundle add logtail
Copied!
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:
1
http_device = Logtail::LogDevices::HTTP.new('<your-source-token>')
2
logger = Logtail::Logger.new(http_device)
Copied!

Usage

Rails
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:
1
Rails.logger.warn(
2
'log structured data',
3
name: {
4
first: "John",
5
last: "Smith"
6
},
7
id: 123456
8
)
Copied!
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:
1
logger.warn(
2
'log structured data',
3
name: {
4
first: "John",
5
last: "Smith"
6
},
7
id: 123456
8
)
Copied!

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:
1
Logtail.with_context(user: { id: 123 }) do
2
logger.info('new subscription')
3
end
Copied!
This snipped will produce the following JSON:
1
{
2
"dt": "2021-03-29T11:24:54.788Z",
3
"level": "warn",
4
"message": "new subscription",
5
"context": {
6
"runtime": {
7
"thread_id": 123456,
8
"file": "script-file.rb",
9
"line": 2,
10
"frame": null,
11
"frame_label": "<main>"
12
},
13
"system": {
14
"hostname": "hostname"
15
"pid": 1234
16
},
17
"user": {
18
"id": 123
19
}
20
}
21
}
Copied!
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:
1
class ApplicationController < ActionController::Base
2
around_action :with_logtail_context
3
​
4
private
5
​
6
def with_logtail_context
7
if user_signed_in?
8
Logtail.with_context(user_context) { yield }
9
else
10
yield
11
end
12
end
13
14
def user_context
15
Logtail::Contexts::User.new(
16
id: current_user.id,
17
name: current_user.name,
18
email: current_user.email
19
)
20
end
21
end
22
​
Copied!
Last modified 5mo ago