This guide provides step-by-step instructions on how to send logs from a Ruby on Rails application to Axiom using the Faraday library.
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.
rbenv
and use it to install the latest Ruby version.gem install rails
command.Create a new Rails app using the rails new myapp
command.
Navigate to the app directory: cd myapp
Open the Gemfile
in your Rails app, and then add the following gems:
Install the dependencies by running bundle install
.
Create a new file named axiom_logger.rb
in the app/services
directory of your Rails app.
Add the following code to axiom_logger.rb
:
Replace API_TOKEN
with the Axiom API token you have generated. For added security, store the API token in an environment variable.
Replace DATASET_NAME
with the name of the Axiom dataset where you want to send data.
Replace AXIOM_DOMAIN
with api.axiom.co
if your organization uses the US region, and with api.eu.axiom.co
if your organization uses the EU region. For more information, see Regions.
Create a new file named axiom_logger_test.rb
in the config/initializers
directory.
Add the following code to axiom_logger_test.rb
:
Each log entry includes a message, level, service, and additional relevant data.
Info logs:
Warn logs:
Error logs:
Debug logs:
Adjust the log messages, services, and additional data according to your application’s specific requirements and context.
log.rake
taskstasks
in the lib
directory of your Rails app.log.rake
in the lib/tasks
directory.log.rake
:This code defines a Rake task that sends a test log to Axiom when invoked.
Start your Rails server by running rails server
.
Go to http://localhost:3000
to trigger the test log from the initializer.
Run the Rake task to send another test log by executing rails log:send_test_log
in your terminal.
In Axiom, go to the Stream tab, and then select the dataset where you send the logs.
You see the test logs appear allowing you to view and analyze your event data coming from your Ruby on Rails application.
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.