Giter Site home page Giter Site logo

useful_logs's Introduction

Useful Logs

A collection of good practices to handle rails logs

Installation

Add this line to your application's Gemfile:

gem 'useful_logs'

And then execute:

$ bundle

Or install it yourself as:

$ gem install useful_logs

Usage

Add to your ApplicationController:

class ApplicationController < ActionController::Base
  include UsefulLogs::RegisterRequestInfo
end

To customize or add values to the logs, you can extend the class RequestInfo, the important part here is to extend the method MyRequestInfo.field_ids and call super within, for the new fields to be picked up.

class MyRequestInfo < UsefulLogs::RequestInfo
  def self.field_ids
    super.concat(%I(app_install_id))
  end

  def app_install_id
    headers['HTTP_X_APP_INSTALL_IDENTIFIER']
  end
end

Another recommended setup in your config/application.rb file:

module MyApp
  class Application < Rails::Application
    config.log_level = ENV.fetch('RAILS_LOG_LEVEL', :debug).to_sym
    config.log_tags  = [:uuid]
  end
end

The final step is to configure lograge in config/initializer/useful_logs.rb, using your custom implementation of RequestInfo, in this case MyRequestInfo:

UsefulLogs.configure do |config|
  config.request_info_class_name = 'MyRequestInfo'
end

UserHub::Application.configure do
  config.lograge.enabled = true
  config.lograge.custom_options = lambda do |event|
    options = { mark: 'request' }
    MyRequestInfo.field_ids.each_with_object(options) do |field, data|
      data[field] = event.payload[field]
    end
  end
  config.lograge.formatter = lambda do |data|
    UsefulLogs::LogFormatter.key_value_pairs(data)
  end
end

Contributing

Contribution directions go here.

License

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

useful_logs's People

Contributors

fespinoza avatar nashby avatar

Stargazers

Robert Audi avatar Hiroki Sato avatar

Watchers

Gaurav Tiwari avatar James Cloos avatar Damian Łopata avatar  avatar  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.