Logging Ruby apps with Logtail

To help you get started with using Logtail in your Ruby projects, here is an overview of how to use the Logtail logger in your Ruby apps.

Setup

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:

# Include logtail library
require "logtail"

# Create logger
http_device = Logtail::LogDevices::HTTP.new("<SOURCE-TOKEN>")
logger = Logtail::Logger.new(http_device)
Copied!

Don’t forget to replace YOUR_LOGTAIL_SOURCE_TOKEN with your actual source token which you can find in the source settings. This will generate config/initializers/logtail.rb.

Logging example

The logger instance we created in the installation section is used to send log messages to Logtail. It provides 5 logging methods for the 5 default log levels. The log levels and their method are:

  • DEBUG¬†- Send debug messages using the¬†debug()¬†method
  • INFO¬†- Send informative messages about the application progress using the¬†info()¬†method
  • WARN¬†- Report non-critical issues using the¬†warn()¬†method
  • ERROR¬†- Send messages about serious problems using the¬†error()¬†method
  • FATAL - Send messages about fatal events that caused the app to crash using the fatal() method

In this example, we will send two logs - DEBUG and INFO:

# Send debug logs messages using the debug() method
logger.debug("Logtail is ready!")

# Send informative messages about interesting events using the info() method
logger.info("I am using Logtail!")
Copied!

This will create the following output:

{
    "dt": "2021-03-29T11:24:54.788Z",
    "level": "debug",
    "message": "Logtail is ready!",
    "context": {
        "runtime": {
            "thread_id": 123,
            "file": "main.rb",
            "line": 6,
            "frame": null,
            "frame_label": "<main>"
        },
        "system": {
            "hostname": "hostname"
            "pid": 1234
        }
    }
}

{
    "dt": "2021-03-29T11:24:54.788Z",
    "level": "info",
    "message": "I am using Logtail!",
    "context": {
        "runtime": {
            "thread_id": 123,
            "file": "main.rb",
            "line": 6,
            "frame": null,
            "frame_label": "<main>"
        },
        "system": {
            "hostname": "hostname"
            "pid": 1234
        }
    }
}
Copied!

Logging structured data

You can also log additional structured data. This can help you provide additional information when debugging and troubleshooting your application. You can provide this data as the second argument to any logging method.

# Send messages about worrying events using the warn() method
# You can also log additional structured data
logger.warn(
    "log structured data",
    item: {
        url: "https://fictional-store.com/item-123",
        price: 100.00
    }
)
Copied!

This will create the following output:

{
    "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": "main.rb",
            "line": 7,            
            "frame": null,
            "frame_label": "<main>"
        },
        "system": {
            "hostname": "hostname"
            "pid": 1234
        }
    }
}
Copied!

Adding more context to your logs

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:

# Provide context to the logs
Logtail.with_context(user: { id: 123 }) do
    logger.info('new subscription')
end
Copied!

This will generate the following JSON output:

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