languages / ruby / installation / manual / rails

Ruby Manual Installation - Rails

⚠ We highly recommend using the Ruby installer to get Timber setup in your Ruby / Rails application. If that is not possible, please follow the instructions below.


Installing Timber into Rails is fairly straight forward:

  1. In your Gemfile, add the timber gem:

    1
    2
     # Gemfile
     gem "timber", "~> 2.0"
    
  2. In your shell, run bundle install.

  3. In config/environments/development.rb install the Timber logger to log to STDOUT:

    1
    2
    3
    4
    5
    6
    7
    # config/environments/development.rb
    Rails.application.configure do
      # ...other configuration here...
      logger = Timber::Logger.new(STDOUT)
      logger.level = config.log_level
      config.logger = ActiveSupport::TaggedLogging.new(logger)
    end
    

    This is required to ensure the Rails.logger object follows the same API as the one that will be in production. Timber adds a slight deviation by allowing a second argument (a Hash) to the logger methods. This allows you to easily log structured metadata with your logs. Plus, it is generally good practice to make your development / test environment to match as close as possible to production.

  4. In config/environments/test.rb install the Timber logger to log to nil.

    1
    2
    3
    4
    5
    6
    7
    # config/environments/test.rb
    Rails.application.configure do
      # ...other configuration here...
      logger = Timber::Logger.new(nil)
      logger.level = config.log_level
      config.logger = ActiveSupport::TaggedLogging.new(logger)
    end
    

    We install the logger into the test environment for the same reason we installed it into the development environment.

  5. Last step! In config/environments/production.rb install the Timber logger to log to your chosen IO device (HTTP, STDOUT, a file, etc). If you aren't sure, we recommend the HTTP device as it is the simplest. For a more in-depth explanation please read our HTTP, STDOUT, or Log files doc.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # config/environments/production.rb
    Rails.application.configure do
      # ...other configuration here...
    
      # --------------------------
      # PLEASE CHOOSE THE APPROPRIATE LOG DEVICE BELOW.
      # You only need one. Please delete the others you are not using.
      # --------------------------
    
      # Option 1: HTTP (recommended / simplest)
      log_device = Timber::LogDevices::HTTP.new("{{your_timber_api_key}}")
    
      # Option 2: STDOUT
      log_device = STDOUT
    
      # Option 3: A file
      log_device = Logger::LogDevice.new("#{Rails.root}/log/#{Rails.env}.log")
    
      # Now instantiate the Timber::Logger
      logger = Timber::Logger.new(log_device)
      logger.level = config.log_level
      config.logger = ActiveSupport::TaggedLogging.new(logger)
    end
    
  6. All done!

Questions?

Chat with us, just click the talk button in the bottom right of this page.

Chat with us
Sign Up

Log better and solve problems faster, today. Install in under a minute.

Sign Up