No release in over 3 years
Low commit activity in last 3 years
This is a library that wraps the [fluent-logger gem](https://github.com/fluent/fluent-logger-ruby) and provides easy integration with your Rails application. This includes log formatters that support [Rails tagged logging](https://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html) for JSON format that can be sent to Fluentd (or really any other logging backend).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 3.6

Runtime

>= 5.0, < 8
 Project Readme

Fluent Logger Rails

This is a library that wraps the fluent-logger gem and provides easy integration with your Rails application. This includes a log formatter that supports Rails tagged logging so your output JSON format that can be sent to Fluentd (or really any other logging backend).

Installation

gem install fluent_logger_rails

How to configure

Rails.application.configure do
  config.logger = FluentLoggerRails::Logger.new(
    ::Fluent::Logger::FluentLogger.new(
      nil,
      host: 'localhost',
      port: 24224,
    ),
    level: config.log_level
  )
  # if you are using tagged logging, you need a formatter that supports it
  config.logger.formatter = FluentLoggerRails::TaggedHashFormatter.new
end

Formatter Configuration

This gem includes a formatter that supports Rails tagged logging.

Hash aka JSON logger formatter

This is a JSON formatter that supports tagged logging.

config.logger.formatter = FluentLoggerRails::TaggedHashFormatter.new
config.logger.formatter.datetime_format = '%Y-%m-%d %H:%M:%S.%3N%z'
config.logger.formatter.parent_key = 'payload'

Standard Rails Tagged Logger aka default logger format

The standard Rails tagged logger works as well for standard output.

ActiveSupport::TaggedLogging.new(config.logger)

Examples

Hash formatter with tagged Logging

Rails.logger.tagged(user.id) do
  Rails.logger.warn('UserUpdateJob failed')
end

#
# Outputs:
#
# {
#   "tags": [1234],
#   "message": "UserUpdateJob failed", 
#   "severity": "WARN",
#   "timestamp": "2019-01-08 14:51:39.701-0800", 
# }

Hash formatter with hash tagged Logging

Rails.logger.tagged(user_id: user.id, session_id: user_session.id) do
  Rails.logger.info(message: 'UserUpdateJob failed', args: args)
end

#
# Outputs:
#
# {
#   "user_id": 1234,
#   "session_id": 883839,
#   "message": {
#     "message": "UserUpdateJob failed",
#     "args": [1,2,3]
#   }, 
#   "severity": "INFO",
#   "timestamp": "2019-01-08 14:51:39.701-0800", 
# }

Standard Rails formatter with tagged Logger

Rails.logger.tagged(user.id, user_session.id) do
  Rails.logger.info('UserUpdateJob failed')
end

#
# Outputs:
#
# [1234] [883839] UserUpdateJob failed

How to test this locally

You can setup Fluentd with ruby gems as described on Fluentd docs. Once that is running, simply configure your environment with the example above and the logs should appear.

Related Projects