Coder Social home page Coder Social logo

marcgrimme / logger_instrumentation Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 3.0 23 KB

Gem to abstract ActiveSupport::Notifications instrumentations behind a layer so that methods like info,debug,error,warn can be used directly and end up in LogSubscribers.

License: MIT License

Ruby 99.10% Shell 0.90%

logger_instrumentation's Introduction

LoggerInstrumentation

I've seen so many Rails applications using Rails.logger.debug|info|warn|error all around the place. This is ok as long as you use the normal Rails logging infrastructure (which is logging to a discrete file). But if you would like to use it in a way that multiple log files are used or the logs are converted to log data (like logstash or whatever other format) then you are pretty much busted.

To work around this Rails itself uses ActiveSupport::Notifications. Nevertheless many of the developers don't wont to bother with this but use an API as mentioned above.

To fill that gap I created a gem that allows the developer to do something like

LoggerInstrumenation.info("mylog message")

or

LoggerInstrumentation.info({message: "mylog message", otherdata: "this is other data"})

This will automatically as always create the proper log entries in the specified log file but uses ActiveSupport::Notifications in the background and by this supports multiple ActiveSupport::LogSubscribers with different log handling (like for example logstash).

I made an example implementation for LogStasher so that the logs can benifit from the additional information supported by LogStasher like the resource_context or others.

Installation

Add this line to your application's Gemfile:

gem 'logger_instrumentation'

And then execute:

$ bundle

Or install it yourself as:

$ gem install logger_instrumentation

Usage

So if you are convinced this gem helps you, you can easily use it in your Rails app as follows.

Installation setup above

Follow Installation above

Create an initializer to setup your environment

If you are using the logstasher gem too then proceed as follows (TODO: for having setup logstash as logstasher.supress_app_log): config/initializers/logger_instrumenation.rb

if Module.const_defined?('LoggerInstrumentation') && LoggerInstrumentation.enabled?
  if Module.const_defined?('LogStasher') and LogStasher.enabled?
    require 'logger_instrumentation/log_stasher_log_subscriber'
  end
  require 'logger_instrumentation/log_subscriber'
  LoggerInstrumentation.logger = Rails.logger
end

Then enable for your environment (like development here): config/environments/development.rb

..
config.logger_instrumentation.enabled = true

Now whereever you would normally use Rails.logger.info or Rail.logger.debug or friends just use/replace with LoggerInstrumenation::Instrumentation.info or LoggerInstrumenation::Instrumentation.debug or whatever log level you intend to use.

LoggerInstrumenation::Instrumentation.info('hello world.')

It's also instead of just a string possible to pass a Hash to this method. For something like logstash it will just convert the Hash to JSON. For the normal logger it will execute the to_s method.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/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 to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/marcgrimme/logger_instrumentation/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

logger_instrumentation's People

Contributors

marcgrimme avatar samirhodzic avatar

Watchers

 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.