Skip to content

A formatted output of all methods called in your rails application of code created by the developer, with the complete path to the class/module, including passed params.

Notifications You must be signed in to change notification settings

carlosdanielpohlod/rails_tracepoint_stack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Install

gem install rails_tracepoint_stack

set env

RAILS_TRACEPOINT_STACK_ENABLED=true

if you wanna enable it globally on your Rails app

Description

This project aims to create a logger for method calls in Ruby , excluding methods from gems, internal classes, etc.

By utilizing Ruby's TracePoint functionality, it allows monitoring and displaying the methods called during the application's execution, filtering to show only the methods defined in the application's own code.

Usage

Global use:

By using the global tracing, just configuring RAILS_TRACEPOINT_STACK as true, you can have a sample scenario and output as:

module Foo
  def puts_message(message)
    puts message
  end
end

class Bar
  include Foo

  def initialize(message:)
    @message = message
  end

  def perform
    puts_message(message)
  end
end

then

Bar.new(message: "Hello World").call

will produce an output as:

called: Bar#initialize in COMPLETE_PATH_TO_YOUR_FILE/app/services/bar.rb:METHOD_LINE with params: {:message=>"Hello World"}
called: Bar#perform in COMPLETE_PATH_TO_YOUR_FILE/app/services/bar.rb:METHOD_LINE with params: {}
called: Foo#puts_message in COMPLETE_PATH_TO_YOUR_FILE/app/services/foo:METHOD_LINE with params: {:message=>"Hello World"}

Enabling Locally with .enable_trace

You can also enable the tracer locally (even with RAILS_TRACEPOINT_STACK not defined) by passing a block of code to be traced:

class Bar
  include Foo

  def initialize(message:)
    @message = message
  end

  def perform
    RailsTracepointStack.enable_trace do
      puts_message(@message)
    end
  end
end

wich will produce a similar result

Configuration

You can also implement custom configuration for RailsTracepointStack by passing your custom configurations as follows:

Configuration Description
file_path_to_filter_patterns Include configuration allowing filter traces only when the origin file path matches a pattern, Example: /services\/foo.rb/
ignore_patterns Pass a regex pattern to filter (ignore) matched traces. Example: /services\/foo.rb/
log_format Inform what kind of format you wanna to use, text (default), or json
log_external_sources Log the external sources to the file, like libraries, gems and bundler
logger Pass your custom logger. Example: Rails.logger. If not used, logs are saved in log/rails_tracepoint_stack.log.

Complete example:

# config/initializers/rails_tracepoint_stack.rb

RailsTracepointStack.configure do |config|
  config.file_path_to_filter_patterns << /services\/foo.rb/
  config.ignore_patterns << /services\/foo.rb/
  config.log_format = :text
  config.log_external_sources = false
  config.logger = YourLogger
end

About

A formatted output of all methods called in your rails application of code created by the developer, with the complete path to the class/module, including passed params.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages