> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# The ultimate guide to Ruby logging: best libraries and practices

export const BlogAuthor = ({date, avatar, alt, name}) => {
  return <div className="-mb-4">
      <p className="m-0 text-base font-semibold">
        <em>Published {date}</em>
      </p>
      <div className="flex items-center -mt-4">
        {avatar && <img src={avatar} alt={alt} className="h-8 w-8 rounded-full m-0" />}
        <p className="ml-[5px] text-lg">{name}</p>
      </div>
    </div>;
};

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

<BlogAuthor date="February 9, 2024" avatar="/images/authors/vadim-korolik.png" alt="portrait of Vadim Korolik." name="by Vadim Korolik" />

## Introduction

In any Ruby application, logging is more than just a means to record errors or track operations. It is a powerful tool that provides insight into the performance, behavior, and health of your application. This comprehensive guide will delve into the nuances of Ruby logging, highlighting the top libraries and practices that will elevate your logging strategy.

## The Importance of Logging in Ruby

Ruby developers often find themselves sifting through logs to diagnose issues or optimize performance. Understanding the significance of logging is key to harnessing its full potential. Logs are not just for error reporting; they are invaluable for monitoring application health, understanding user interactions, and making data-driven decisions.

## Leveraging Ruby’s Built-in Logger

The Ruby standard library comes with a built-in Logger class, which provides a basic yet effective logging mechanism. Let's dive into a comprehensive example:

```ruby lines wrap theme={null}
require 'logger'

# Creating a new logger instance that outputs to the console
logger = Logger.new(STDOUT)
logger.level = Logger::INFO  # Setting the log level to INFO

# Customizing the log format for better readability
logger.formatter = proc do |severity, datetime, progname, msg|
  "#{datetime}: #{severity} - #{msg}\n"
end

# Example log messages at different severity levels
logger.info("Application initialized")
logger.warn("Low disk space warning")
logger.error("Error processing user data")
```

This snippet demonstrates the creation of a logger, setting a severity level, and customizing the log format. Each log message is now more informative and easier to read, aiding in quicker debugging and analysis.

## Exploring Advanced Ruby Logging Libraries

1. Lograge for Rails Applications

Lograge is an excellent choice for Rails developers who prefer a cleaner and more condensed logging format. Here's an in-depth look at setting up Lograge:

```ruby lines wrap theme={null}
# In config/environments/production.rb
config.lograge.enabled = true
config.lograge.base_controller_class = 'ActionController::API'
config.lograge.logger = ActiveSupport::Logger.new(STDOUT)
config.lograge.formatter = Lograge::Formatters::Json.new
```

This configuration streamlines Rails logs into a single line per request, with JSON formatting for easier parsing and analysis.

2. The Logging Gem: A Flexible Alternative

The Logging gem offers a robust and customizable logging solution. Here's a highly-customized setup that emits `INFO` level logs to a file named `development.log` and `WARN` level logs to a file named `warn.log`:

```ruby expandable lines wrap theme={null}
require 'logging'

# Configuring the root logger
Logging.logger.root.appenders = Logging.appenders.file('development.log')
Logging.logger.root.level = :info

# Setting up a logger for a specific class
my_logger = Logging.logger['MyClass']
my_logger.level = :warn
my_logger.add_appenders(
  Logging.appenders.file('warn.log')
)

# Utilizing the logger in a Ruby class
class MyClass
  def initialize
    @logger = Logging.logger[self]
  end

  def perform_action
    @logger.warn "Action performed with warning"
  end
end
```

This example illustrates how to set up different loggers for specific parts of your application, providing granular control over logging outputs.

[LaunchDarkly](https://app.launchdarkly.com/sign_up?ref=blog-ruby) supports all of these logging libraries and other ways to ingest your logs.
3\. Log4r: Multi-Output Logging

Log4r extends the flexibility of logging by allowing multiple outputs. Here's how you can set it up:

```ruby expandable lines wrap theme={null}
require 'log4r'
include Log4r

# Creating a logger
logger = Logger.new('application_logger')

# Adding a console outputter
stdout_outputter = Outputter.stdout
stdout_outputter.formatter = PatternFormatter.new(pattern: "[%l] %d :: %m")
logger.add(stdout_outputter)

# Adding a file outputter
file_outputter = FileOutputter.new('file_outputter', filename: 'app.log', trunc: false)
file_outputter.formatter = PatternFormatter.new(pattern: "[%l] %d :: %m")
logger.add(file_outputter)

# Logging an informational message
logger.info('Application is up and running')
```

This setup allows you to direct your logs to both the console and a file, offering more flexibility in log management.

4. Semantic Logger: Structured and Asynchronous Logging

Semantic Logger is ideal for creating structured, JSON-formatted logs, particularly useful in asynchronous environments. Here's a practical example:

```ruby lines wrap theme={null}
require 'semantic_logger'

# Adding a JSON-formatted file appender
SemanticLogger.add_appender(file_name: 'application.log', formatter: :json)

# Logging an informational message with additional context
SemanticLogger['MyApp'].info('Application started', { environment: 'production', version: '1.2.3' })
```

This example demonstrates the utility of Semantic Logger in producing logs that are easily parseable by modern log analysis tools.

## Conclusion

Mastering logging in Ruby is an art that involves selecting the right tools and practices for your specific needs. Through this guide, we have explored various libraries and methods that can enhance the logging capabilities of your Ruby applications. Effective logging is key to gaining insights into your applications and ensuring their optimal performance.

We invite you to [integrate these logging strategies](https://app.launchdarkly.com/sign_up?ref=blog-ruby) into your Ruby projects. Share your experiences and insights in the comments below.
