Composite Logger

One log to rule them all.

Installation

Add this line to your application's Gemfile:

gem "composite_logger"

And then execute:

$ bundle

Or install it yourself as:

$ gem install composite_logger

Usage

CompositeLogger implements the standard Logger interface so it can be a drop-in replacement for your current logger:

require "composite_logger"

stdout_logger = Logger.new(STDOUT)
stderr_logger = Logger.new(STDERR)
file_logger = Logger.new("logfile.log")

stdout_logger.level = Logger::DEBUG
stdout_logger.level = Logger::ERROR
file_logger.level = Logger::ERROR

composite_logger = CompositeLogger.new(stdout_logger, stderr_logger)
composite_logger.loggers << file_logger
composite_logger.level = Logger::INFO
composite_logger.debug("This won't be logged becase logger level is INFO")
composite_logger.info("This will be logged")
composite_logger << "Message using 'dump' syntax"
composite_logger.error do
  "An error message using block syntax"
end

Rails

CompositeLogger can be integrated within your Rails application:

# config/initializers/logger.rb
Rails.logger = CompositeLogger.new(Rails.logger, RollbarLogger.new)

That's all you need to use CompositeLogger with Rails.

Development

After checking out the repo, run script/setup to install dependencies. Then, run rake test to run the tests. You can also run script/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fertapric/composite_logger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Author

Fernando Tapia Rico, @fertapric