Permalink
Browse files

first

  • Loading branch information...
0 parents commit 8f1959ef232e174ad94bb684e2f049afdee8ae5b @jrochkind committed Aug 4, 2011
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in formatted_rails_logger.gemspec
+gemspec
@@ -0,0 +1 @@
+require 'bundler/gem_tasks'
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "formatted_rails_logger/version"
+
+Gem::Specification.new do |s|
+ s.name = "formatted_rails_logger"
+ s.version = FormattedRailsLogger::VERSION
+ s.authors = ["Jonathan Rochkind"]
+ s.email = ["jonathan@dnil.net"]
+ s.homepage = ""
+ s.summary = %q{Formatting capabilities for Rails BufferedLogger}
+
+ s.rubyforge_project = "formatted_rails_logger"
+
+ 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"]
+end
@@ -0,0 +1,7 @@
+require "formatted_rails_logger/version"
+
+module FormattedRailsLogger
+ def self.patch_rails
+ load File.expand_path("../formatted_rails_logger/buffered_logger_inject.rb", __FILE__)
+ end
+end
@@ -0,0 +1,51 @@
+# intentionally raise if not already defined. For some reason I feel like
+# doing this rather than declaring Rails a dependency.
+ActiveSupport::BufferedLogger.class
+
+class ActiveSupport::BufferedLogger
+ attr_accessor :formatter
+ # use pure ruby class method, Rails provides ones with
+ # better semantics for inheritance, but they keep changing
+ # version to version, so nope.
+ class << self ; attr_accessor :default_formatter ; end
+
+
+
+
+ # Have to over-ride/replace BufferedLogger's usual #add
+ # to use formatter
+ def add(severity, message = nil, progname = nil, &block)
+ return if @level > severity
+ message = (message || (block && block.call) || progname).to_s
+
+ # our custom formatting
+ message = format_message(format_severity(severity), Time.now, progname, message)
+
+ # If a newline is necessary then create a new message ending with a newline.
+ # Ensures that the original message is not mutated.
+ message = "#{message}\n" unless message[-1] == ?\n
+ buffer << message
+ auto_flush
+ message
+ end
+
+ protected
+
+ # why does BufferedLogger not give us a way to translate from integer
+ # constant to actual severity lable? argh. copied from Logger
+ # Severity label for logging. (max 5 char)
+ SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL UNKNOWN)
+ def format_severity(severity)
+ SEV_LABEL[severity] || 'UNKNOWN'
+ end
+
+ def format_message(severity, datetime, progname, msg)
+ if formatter = (@formatter || self.class.default_formatter)
+ formatter.call(severity, datetime, progname, msg)
+ else
+ msg
+ end
+ end
+
+end
+
@@ -0,0 +1,29 @@
+#
+# A Logger::Formatter that includes timestamp and severity, and works
+# well with Rails, taking account of preceding newlines in msg for spacing.
+class FormattedRailsLogger::Formatter
+ # Unlike Logger, not seeing a need to include the PID, the progname,
+ # or the Severity twice.
+ @@format_str = "[%s] %5s %s"
+ @@datetime_format = nil
+
+ def call(severity, time, progname, msg)
+ # see no need for micro-seconds like in Logger, milis suffices.
+ # No idea why documented %L and other such useful things
+ # do not work in strftime.
+ formatted_time = if @@datetime_format
+ time.strftime(@@datetime_format)
+ else
+ time.strftime("%Y-%m-%d %H:%M:%S.") << time.usec.to_s[0..2].rjust(3)
+ end
+
+ # Rails likes to log with some preceding newlines for spacing in the
+ # logfile. We want to preserve those when present,
+ # but prefix actual content with our prefix.
+ msg = "" if msg.nil? # regexp won't like nil, but will be okay with ""
+ matchData = ( /^(\n*)/.match(msg) )
+ matchData[0] + (@@format_str % [formatted_time, severity, matchData.post_match])
+ end
+
+end
+
@@ -0,0 +1,3 @@
+module FormattedRailsLogger
+ VERSION = "0.1.0"
+end

0 comments on commit 8f1959e

Please sign in to comment.