No release in over 3 years
This library can mimic Ruby's built-in Logger class to forward logs to fluent.You can use this library not only for Rails but also for pure-Ruby apps.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

AwesomeFluentLogger

Gem Version ci

This library can mimic Ruby's built-in Logger class to forward logs to Fluentd. You can use this library not only for Rails but also for pure-Ruby apps.

Commentary: シンプルに Fluentd にログ転送ができる RubyGem "awesome_fluent_logger" をつくった (日本語/Japanese)

Installation

Add this line to your application's Gemfile:

gem 'awesome_fluent_logger'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install awesome_fluent_logger

Usage

Tiny example

Your Ruby program:

require 'awesome_fluent_logger'

logger = AwesomeFluentLogger.new(fluent: { host: 'localhost', port: 24224 })
logger.info('information logging')

Fluentd output:

2021-01-23 13:28:46.000000000 +0000 info: {"severity":"INFO","time":"2021-01-23 13:28:46.336397 +0000","progname":null,"message":"information logging"}

Rails example

Rails configure file (config/application.rb or config/environments/{RAILS_ENV}.rb):

logger = AwesomeFluentLogger.new(fluent: { host: 'localhost', port: 24224 })
config.logger = ActiveSupport::TaggedLogging.new(logger)

Can be set fluent-logger instance in initialize parameters

:fluent of initialize argument can be set an instance of Fluent::Logger::FluentLogger class.

fluent = Fluent::Logger::FluentLogger.new(nil, socket_path: '/tmp/fluent.sock')
logger = AwesomeFluentLogger.new(fluent: fluent)

Fluent tags

If the initialize parameter [:fluent][:tag_prefix] is specified, it will be inserted at the beginning of the Fluentd tag.

logger = AwesomeFluentLogger.new(fluent: { tag_prefix: 'kanan', host: 'localhost', port: 24224 })
logger.info('ご機嫌いかがかなん?')
2021-01-23 13:28:46.000000000 +0000 kanan.info: {"severity":"INFO", ...

The same effect is given by specifying progname.

logger = AwesomeFluentLogger.new(fluent: { host: 'localhost', port: 24224 }, progname: 'chika')
logger.info('かんかんみかん')
2021-01-23 13:28:46.000000000 +0000 chika.info: {"severity":"INFO", ...

If both are specified, they will be nested. In this case, tag_prefix will be added first.

logger = AwesomeFluentLogger.new(fluent: { tag_prefix: 'kanan', host: 'localhost', port: 24224 }, progname: 'chika')
logger.info('2人は幼馴染です')
2021-01-23 13:28:46.000000000 +0000 kanan.chika.info: {"severity":"INFO", ...

Initialize parameters

Other initialization parameters are based on the Logger class of the Ruby standard library.

Key Default Descriptions
fluent (none) Fluent::Logger::FluentLogger initialize paramater hash or Fluent::Logger::FluentLogger class instance
level Logger::DEBUG Logger severity level constant
progname nil Program name to include in log messages and Fluentd tag
formatter AwesomeFluentLogger::Formatter Inherited Logger::Formatter class instance
datetime_format %Y-%m-%d %H:%M:%S.%6N %z %iso8601 or Time#strftime formatted text

Contributing

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

License

MIT License