Giter VIP home page Giter VIP logo

fluent_logger_rails's Introduction

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

fluent_logger_rails's People

Contributors

bwillis avatar reppiee avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.