Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 04d810bb03bcf7d914c31a97234cf9b7769a3453 0 parents
Jason Stirk authored
17 .gitignore
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in logbang.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Jason Stirk
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 README.md
@@ -0,0 +1,47 @@
+# logbang
+
+Logbang provides `log!`, which is `Rails.logger.debug` on steroids.
+
+Logbang allows you to scatter `log!` calls throughout your Rails
+application (views, controller, helpers) and appends the message to the
+logs in a pretty, coloured format.
+
+It also keeps track of the timing information, and generates a timing
+table at the end of the request showing the time between calls, and
+unique tokens you can use to jump directly to the relevant position in
+the logs.
+
+It is useful for any kind of debugging where navigating full stack
+traces is hard, or where you are hunting performance issues and want to
+selectively drill down into pieces of your code, rather than profile the
+entire execution.
+
+It's also great for wrapping around a piece of functionality to be able
+to pinpoint which SQL queries are related.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'logbang'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install logbang
+
+## Usage
+
+Simply call `log!` (with a message or without) anywhere that is called
+in the context of +ActionController::Base+ (controller, views, helpers).
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
2  Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
3  lib/logbang.rb
@@ -0,0 +1,3 @@
+require "logbang/version"
+require "logbang/helper"
+require "logbang/railtie"
80 lib/logbang/helper.rb
@@ -0,0 +1,80 @@
+require 'active_support/concern'
+
+module Logbang
+ # Adds the +log!+ functionality to the Rails ActionController::Base
+ # method, and sets up an after_filter to dump the timing statistics at
+ # the end of each request.
+ module Helper
+ extend ActiveSupport::Concern
+
+ included do
+ helper_method :log!
+ after_filter :dump_log!
+ end
+
+ # Create a log entry with timing data and a unique ID.
+ #
+ # This will show as a yellow log entry like :
+ # = Marked: container-start - 6d5c4c5
+ #
+ # If +message+ is left blank, the caller filename will be used.
+ def log!(message=nil)
+ call_list = caller.find_all { |x| x =~ /#{Rails.root}/ }
+ mycaller = call_list[0].to_s.gsub(/^([^:]+):.*/, '\1')
+
+ message = mycaller.gsub(/#{Rails.root}/,'') if message.nil?
+
+ call_list.reject! { |x| x =~ Regexp.new("^#{mycaller}") }
+ parent = call_list[0].to_s.gsub(/^([^:]+):.*/, '\1') if call_list[0]
+
+ @log_markers ||= []
+ time = Time.now.to_f
+ id = Digest::MD5.hexdigest("#{message}#{time}")
+ @log_markers << { :time => time,
+ :caller => mycaller,
+ :parent => parent,
+ :marker => message,
+ :id => id
+ }
+ Logbang::Railtie.log "Marked: #{message} - #{id[0..6]}"
+ end
+
+ private
+
+ # Dumps out the logs and timing data at the end of the request, like:
+ #
+ # = 56e83ce @ 0ms (+ 0ms) /app/views/agreements/index.html.erb
+ # = 8933225 @ 391ms (+ 391ms) helper-start
+ # = 8dc03cc @ 444ms (+ 53ms) helper-end
+ # = 4380ee5 @ 625ms (+ 180ms) container-start
+ # = bcec099 @ 694ms (+ 69ms) container-end
+ def dump_log!
+ return nil if @log_markers.blank?
+ known_markers = {}
+ first = nil
+ last = nil
+ @log_markers.each do |marker|
+ first = marker[:time] if first.blank?
+ last = first if last.nil?
+ idx = known_markers[marker[:caller]]
+ if idx.nil? then
+ idx = known_markers[marker[:parent]]
+ if idx.nil? then
+ idx = 0
+ else
+ idx += 1
+ end
+ known_markers[marker[:caller]] = idx
+ end
+
+ duration = ((marker[:time] - last) * 1000).to_i
+ total = ((marker[:time] - first) * 1000).to_i
+
+ message = "#{marker[:id][0..6]} @ #{sprintf('% 5d',total)}ms (+#{sprintf('% 5d', duration)}ms) #{" " * idx} #{marker[:marker]}"
+ Logbang::Railtie.log message
+ last = marker[:time]
+ true
+ end
+ end
+ end
+end
20 lib/logbang/railtie.rb
@@ -0,0 +1,20 @@
+module Logbang
+ class Railtie < Rails::Railtie
+ initializer "logbang.configure_rails_initialization" do
+ ActionController::Base.send(:include, Logbang::Helper)
+ end
+
+ # Writes the log string out to the Rails logger in a pretty yellow
+ # colour that is almost impossible to miss!
+ def self.log(message)
+ out = ""
+ out << ActiveSupport::LogSubscriber::YELLOW
+ out << ActiveSupport::LogSubscriber::BOLD
+ out << " = "
+ out << message
+ out << ActiveSupport::LogSubscriber::CLEAR
+ Rails.logger.info out
+ end
+ end
+end
+
3  lib/logbang/version.rb
@@ -0,0 +1,3 @@
+module Logbang
+ VERSION = "0.0.1"
+end
19 logbang.gemspec
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/logbang/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Jason Stirk"]
+ gem.email = ["jason@reinteractive.net"]
+ gem.description = %q{Scatter "log!" calls in your Rails app to make debugging and timing easy}
+ gem.summary = %q{Scatter "log!" calls in your Rails app to make debugging and timing easy}
+ gem.homepage = "http://github.com/jstirk/logbang"
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "logbang"
+ gem.require_paths = ["lib"]
+ gem.version = Logbang::VERSION
+
+ gem.add_dependency('activesupport')
+end
Please sign in to comment.
Something went wrong with that request. Please try again.