Giter VIP home page Giter VIP logo

loggable-struct's Introduction

loggable-struct

Implementation of loggable struct using Ruby refinements

Usage

Require the basic file loggable_struct somewhere:

require "loggable_struct"

And then add to module where you want to have logging:

using LoggableStruct

Simple example (using pry or irb)

require_relative "lib/loggable_struct"

SomeStruct = Struct.new :foo, :bar
struct1 = SomeStruct.new foo: 1

begin
  using LoggableStruct
  
  SomeStruct.new.foo
  struct1.foo
end

Output:

I, [2015-05-19T11:50:22.000680 #53964]  INFO -- : :foo
I, [2015-05-19T11:50:22.000790 #53964]  INFO -- : :foo

Notes:

There are few implementation limitations due to refinements implementation:

  • As I mentioned above, monkey patching objects is not supported;
  • If you create a new Struct class(not object) inside the block where using LoggableStruct was used, it’s instances will not have their new methods logged.

Both cases are described in specs.

There are few things to mention:

  • I tried a few AOP gems and found no one working correctly with refinements;
  • Creating Struct descendants does not call inherited method, so I was unable to track descendants this way;
  • It still requires monkey-patching for Struct outside of the refinement to track its descendants and their methods;
  • Even refinement creates a module, this module doesn’t call neither included nor extended when using is called;
  • Using ObjectSpace.each_object allows using this refinement even if loggable_struct and core_ext/struct were included far from the start of the application(e.g. in the middle of the irb or pry session), also it was the cheapest way to track all existing descendants of Struct due to all these limitations.

loggable-struct's People

Contributors

hwo411 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.