Skip to main content
This guide provides step-by-step instructions on how to send logs from a Ruby on Rails application to Axiom using the Faraday library. By following this guide, you configure your Rails app to send logs to Axiom, allowing you to monitor and analyze your application logs effectively.

Prerequisites

Set up the Ruby on Rails application

  1. Create a new Rails app using the rails new myapp command.
  2. Navigate to the app directory: cd myapp

Setting up the Gemfile

Open the Gemfile in your Rails app, and then add the following gems:
Install the dependencies by running bundle install.

Create and configure the Axiom logger

  1. Create a new file named axiom_logger.rb in the app/services directory of your Rails app.
  2. Add the following code to axiom_logger.rb:
In the code above, make the following changes:
  • Replace API_TOKEN with your Axiom API key.
  • Replace DATASET_NAME with the name of the Axiom dataset where you want to send data.

Test with the Axiom logger

  1. Create a new file named axiom_logger_test.rb in the config/initializers directory.
  2. Add the following code to axiom_logger_test.rb:
Each log entry includes a message, level, service, and additional relevant data.
  • Info logs:
    • Application started successfully
    • User authentication successful
    • Data fetched from external API
    • Email notification sent
  • Warn logs:
    • API request took longer than expected (including duration)
    • User authentication token expiring soon (including user ID)
    • Low disk space warning (including disk usage percentage)
    • Non-critical configuration issue detected
  • Error logs:
    • Database connection error (including error message)
    • Failed to process payment (including user ID and error message)
    • Unhandled exception occurred (including exception type)
    • Third-party API returned an error (including status code)
  • Debug logs:
    • Request parameters (including parameter values)
    • Response headers (including header key-value pairs)
    • User object details (including user attributes)
    • Cache hit for key (including cache key)
Adjust the log messages, services, and additional data according to your application’s specific requirements and context.

Create the log.rake tasks

  1. Create a new directory named tasks in the lib directory of your Rails app.
  2. Create a new file named log.rake in the lib/tasks directory.
  3. Add the following code to log.rake:
This code defines a Rake task that sends a test log to Axiom when invoked.

View logs in Axiom

  1. Start your Rails server by running rails server.
  2. Go to http://localhost:3000 to trigger the test log from the initializer.
  3. Run the Rake task to send another test log by executing rails log:send_test_log in your terminal.
  4. In Axiom, go to the Stream tab, and then select the dataset where you send the logs.
  5. You see the test logs appear allowing you to view and analyze your event data coming from your Ruby on Rails application.

Conclusion

You have successfully set up your Ruby on Rails application to send logs to Axiom using the Faraday library. With this configuration, you can centralize your application logs and use Axiom’s powerful features like APL for log querying, monitoring, and observing various log levels and types effectively.