Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update module layout and gemspec

  • Loading branch information...
commit 0b06119b4c93b55a52fddf19f9bec89de3e20a9d 0 parents
@czarneckid czarneckid authored
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
1  .rvmrc
@@ -0,0 +1 @@
+rvm use ruby-1.9.2@better_logging_gem --create
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in better_logging.gemspec
+gemspec
12 Rakefile
@@ -0,0 +1,12 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rake'
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rspec_opts = ['--backtrace']
+end
+
+task :default => :spec
26 better_logging.gemspec
@@ -0,0 +1,26 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "better_logging/version"
+
+Gem::Specification.new do |s|
+ s.name = "better_logging"
+ s.version = BetterLogging::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["TODO: Write your name"]
+ s.email = ["TODO: Write your email address"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "better_logging"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency('activesupport', '~> 3.0.9')
+
+ s.add_development_dependency('rake', '~> 0.9.2')
+ s.add_development_dependency('rspec', '~> 2.6.0')
+end
4 lib/better_logging.rb
@@ -0,0 +1,4 @@
+require 'active_support'
+require 'better_logging/better_logging'
+
+ActiveSupport::BufferedLogger.send(:include, BetterLogging)
135 lib/better_logging/better_logging.rb
@@ -0,0 +1,135 @@
+# This module, when included into ActiveSupport::BufferedLogger, improves the
+# logging format. See the README file for more info.
+#
+# This is distributed under a Creative Commons "Attribution-Share Alike"
+# license: for details see:
+# http://creativecommons.org/licenses/by-sa/3.0/
+#
+module BetterLogging
+ LENGTH = ActiveSupport::BufferedLogger::Severity.constants.map{|c| c.to_s.length}.max
+
+ def self.included(base)
+ base.class_eval do
+ alias_method_chain :add, :extra_info
+ alias_method_chain :error, :exception_param
+ alias_method_chain :warn, :exception_param
+ end
+
+ # Get the length to format the output so that the pid column lines up.
+ # The severity levels probably won't change but this avoids hard-coding
+ # them anyway, just in case.
+ # Most of this is done with class_eval so it should only be done once
+ # while the class is being loaded.
+ if_stmts = ""
+ for c in ActiveSupport::BufferedLogger::Severity.constants
+ if_stmts += <<-EOT
+ if severity == #{c}
+ severity_name = sprintf("%1$*2$s", "#{c}", #{LENGTH * -1})
+ use_colour = false
+ if Rails.version.to_i >= 3
+ use_colour = true if ActiveSupport::LogSubscriber.colorize_logging
+ else
+ use_colour = true if defined?(ActiveRecord) && ActiveRecord::Base.colorize_logging
+ end
+ if use_colour
+ if severity == INFO
+ severity_name = "\033[32m" + severity_name + "\033[0m"
+ elsif severity == WARN
+ severity_name = "\033[33m" + severity_name + "\033[0m"
+ elsif severity == ERROR || severity == FATAL
+ severity_name = "\033[31m" + severity_name + "\033[0m"
+ end
+ end
+ return severity_name
+ end
+ EOT
+ end
+ base.class_eval <<-EOT, __FILE__, __LINE__
+ def self.severity_name(severity)
+ #{if_stmts}
+ return "UNKNOWN"
+ end
+ EOT
+ end
+
+
+ def self.verbose=(boolean)
+ @@verbose = boolean
+ end
+
+ def self.custom=(string)
+ @@custom = string
+ @@line_prefix = format_line_prefix
+ end
+
+ def self.hostname_maxlen=(integer)
+ @@hostname_maxlen = integer
+ @@line_prefix = format_line_prefix
+ end
+
+ def self.format_line_prefix
+ if @@full_hostname.length < @@hostname_maxlen
+ hostname = @@full_hostname
+ else
+ hostname = @@full_hostname[-(@@hostname_maxlen)..-1]
+ end
+
+ line_prefix = sprintf("%1$*2$s", "#{hostname}.#{@@pid} ", -(7 + hostname.length))
+ line_prefix = "#{@@custom} #{line_prefix}" if @@custom
+ return line_prefix
+ end
+
+ def self.get_hostname
+ `hostname -s`.strip
+ end
+
+ # The following are cached as class variables for speed.
+
+ # These are configurable, put something like the following in an initializer:
+ # PaulDowman::RailsPlugins::BetterLogging.verbose = false
+ @@verbose = Rails.env != "development"
+ @@full_hostname = get_hostname
+ @@hostname_maxlen = 10
+ @@custom = nil
+
+ # These are not configurable
+ @@pid = $$
+ @@line_prefix = format_line_prefix
+
+
+ # the cached pid can be wrong after a fork(), this checks if it has changed and
+ # re-caches the line_prefix
+ def update_pid
+ if @@pid != $$
+ @@pid = $$
+ @@line_prefix = BetterLogging.format_line_prefix
+ end
+ end
+
+ def add_with_extra_info(severity, message = nil, progname = nil, &block)
+ update_pid
+ time = @@verbose ? "#{Time.new.strftime('%H:%M:%S')} " : ""
+ message = "#{time}#{ActiveSupport::BufferedLogger.severity_name(severity)} #{message}"
+
+ # Make sure every line has the PID and hostname and custom string
+ # so we can use grep to isolate output from one process or server.
+ # gsub works even when the output contains "\n", though there's
+ # probably a small performance cost.
+ message = message.gsub(/^/, @@line_prefix) if @@verbose
+
+ add_without_extra_info(severity, message, progname, &block)
+ end
+
+
+ # add an optional second parameter to the error & warn methods to allow a stack trace:
+
+ def error_with_exception_param(message, exception = nil)
+ message += "\n#{exception.inspect}\n#{exception.backtrace.join("\n")}" if exception
+ error_without_exception_param(message)
+ end
+
+ def warn_with_exception_param(message, exception = nil)
+ message += "\n#{exception.inspect}\n#{exception.backtrace.join("\n")}" if exception
+ warn_without_exception_param(message)
+ end
+end
3  lib/better_logging/version.rb
@@ -0,0 +1,3 @@
+module BetterLogging
+ VERSION = "0.0.1"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.