From 0ce411c11c9f4eaebecbee7bc786f99e06b5b7e5 Mon Sep 17 00:00:00 2001 From: Florian Staudacher Date: Fri, 15 Jun 2012 01:15:45 +0200 Subject: [PATCH 1/3] fix startup error for integration envs --- config/environments/integration1.rb | 2 +- config/environments/integration2.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/environments/integration1.rb b/config/environments/integration1.rb index a3a5ee606..974d6b472 100644 --- a/config/environments/integration1.rb +++ b/config/environments/integration1.rb @@ -32,7 +32,7 @@ Diaspora::Application.configure do "<#{self.class.name} - tooooo long>" end end - [ActionController::Base, ActionDispatch::RemoteIp::RemoteIpGetter, OmniAuth::Strategy, Warden::Proxy].each do |klazz| + [ActionController::Base, OmniAuth::Strategy, Warden::Proxy].each do |klazz| klazz.send(:include, SmallInspect) end end diff --git a/config/environments/integration2.rb b/config/environments/integration2.rb index 8b4b88521..184a05302 100644 --- a/config/environments/integration2.rb +++ b/config/environments/integration2.rb @@ -32,7 +32,7 @@ Diaspora::Application.configure do "<#{self.class.name} - tooooo long>" end end - [ActionController::Base, ActionDispatch::RemoteIp::RemoteIpGetter, OmniAuth::Strategy, Warden::Proxy].each do |klazz| + [ActionController::Base, OmniAuth::Strategy, Warden::Proxy].each do |klazz| klazz.send(:include, SmallInspect) end end From 8f216571791b847c7ced1957049322f8a51a388c Mon Sep 17 00:00:00 2001 From: Florian Staudacher Date: Fri, 15 Jun 2012 01:21:53 +0200 Subject: [PATCH 2/3] add all the colors of the rainbow, log into the same file and throw in some formatting --- lib/federation_logger.rb | 113 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 5 deletions(-) diff --git a/lib/federation_logger.rb b/lib/federation_logger.rb index c5d2af1c0..c9ac91a5f 100644 --- a/lib/federation_logger.rb +++ b/lib/federation_logger.rb @@ -1,15 +1,118 @@ #custom_logger.rb -class FederationLogger < Logger - def format_message(severity, timestamp, progname, msg) - "#{Rails.env}-#{timestamp}: #{msg}\n" + +class ActiveSupport::BufferedLogger + def formatter=(formatter) + @log.formatter = formatter end end +class Formatter + COLORS = { + 'FG' => { + 'black' => '30', + 'red' => '31', + 'green' => '32', + 'yellow' => '33', + 'blue' => '34', + 'violet' => '35', + 'cyan' => '36', + 'white' => '37', + }, + 'BG' => { + 'black' => '40', + 'red' => '41', + 'green' => '42', + 'yellow' => '43', + 'blue' => '44', + 'violet' => '45', + 'cyan' => '46', + 'white' => '47', + } + } + + DULL = '0' + BRIGHT = '1' + NULL = '00' + + ESC = "\e" + RESET = "#{ESC}[00;0;00m" + # example: \033[40;0;37m white text on black background + + SEVERITY_TO_TAG_MAP = {'DEBUG'=>'meh', 'INFO'=>'fyi', 'WARN'=>'hmm', 'ERROR'=>'wtf', 'FATAL'=>'omg', 'UNKNOWN'=>'???'} + SEVERITY_TO_COLOR_MAP = {'DEBUG'=>'white', 'INFO'=>'green', 'WARN'=>'yellow', 'ERROR'=>'red', 'FATAL'=>'red', 'UNKNOWN'=>'white'} + + def initialize + @colors_enabled = true + end + + def random + @random ||= COLORS['FG'].keys[Random.rand(8)] + end + + def colors? + @colors_enabled + end + + def fg name + COLORS['FG'][name] + end + + def bg name + COLORS['BG'][name] + end + + def colorize(message, c_fg='white', c_bg='black', strong=0) + if colors? + "#{ESC}[#{fg(c_fg)};#{bg(c_bg)};#{strong==0 ? DULL : BRIGHT}m#{message}#{RESET}" + else + message + end + end + + def call(severity, time, progname, msg) + fmt_prefix = pretty_prefix(severity, time) + fmt_msg = pretty_message(msg) + + "#{fmt_prefix} #{fmt_msg}\n" + end + + def pretty_prefix(severity, time) + color = SEVERITY_TO_COLOR_MAP[severity] + fmt_severity = colorize(sprintf("%-3s","#{SEVERITY_TO_TAG_MAP[severity]}"), color, 'black', 1) + fmt_time = colorize(time.strftime("%s.%L")) + fmt_env = colorize(Rails.env, random, 'black', 1) + + "#{fmt_env} - #{fmt_time} [#{fmt_severity}] (pid:#{$$})" + end + + def pretty_message msg + w = 130 + txt_w = (w - Rails.env.size - 3) + + # get the prefix without colors, just for the length + @colors_enabled = false + prefix = pretty_prefix("DEBUG", Time.now).size + 1 + @colors_enabled = true # restore value + + if (msg.size <= (w-prefix)) + msg + else + output = msg.strip.scan(/.{1,#{txt_w}}/).flatten.map { |line| sprintf("%#{w}s", sprintf("%-#{txt_w}s", line)) }.join("\n") + "\n#{output}" + end + end + +end + +class FederationLogger < ActiveSupport::BufferedLogger +end + if Rails.env.match(/integration/) puts "using federation logger" - logfile = File.open(Rails.root.join("log", "#{Rails.env}_federation.log"), 'a') #create log file + logfile = File.open(Rails.root.join("log", "federation_logger.log"), 'a') #create log file logfile.sync = true #automatically flushes data to file FEDERATION_LOGGER = FederationLogger.new(logfile) #constant accessible anywhere + FEDERATION_LOGGER.formatter = Formatter.new else FEDERATION_LOGGER = Rails.logger -end \ No newline at end of file +end From bff069ab1930be64074f8a735a6f29448e1970fe Mon Sep 17 00:00:00 2001 From: Florian Staudacher Date: Fri, 15 Jun 2012 01:22:50 +0200 Subject: [PATCH 3/3] tinker with log messages for readability --- lib/postzord/receiver/private.rb | 14 +++++++------- lib/webfinger.rb | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/postzord/receiver/private.rb b/lib/postzord/receiver/private.rb index b323d5090..be786657a 100644 --- a/lib/postzord/receiver/private.rb +++ b/lib/postzord/receiver/private.rb @@ -28,7 +28,7 @@ class Postzord::Receiver::Private < Postzord::Receiver end rescue => e #this sucks - FEDERATION_LOGGER.info("Failure to receive #{@object.inspect} for sender:#{@sender.id} for user:#{@user.id}: #{e.message}") + FEDERATION_LOGGER.error("Failure to receive #{@object.class} from sender:#{@sender.id} for user:#{@user.id}: #{e.message}\n#{@object.inspect}") raise e end end @@ -42,9 +42,9 @@ class Postzord::Receiver::Private < Postzord::Receiver if self.validate_object set_author! receive_object - FEDERATION_LOGGER.info("object received #{@object.class}") + FEDERATION_LOGGER.info("object received: [#{@object.class}#{@object.respond_to?(:text) ? ":'#{@object.text}'" : ''}]") else - FEDERATION_LOGGER.info("failed to receive object from #{@object.author}: #{@object.inspect}") + FEDERATION_LOGGER.error("failed to receive object from #{@object.author}: #{@object.inspect}") raise "not a valid object:#{@object.inspect}" end end @@ -53,7 +53,7 @@ class Postzord::Receiver::Private < Postzord::Receiver def receive_object obj = @object.receive(@user, @author) Notification.notify(@user, obj, @author) if obj.respond_to?(:notification_type) - FEDERATION_LOGGER.info("user:#{@user.id} successfully received private post from person#{@sender.guid} #{@object.inspect}") + FEDERATION_LOGGER.info("user:#{@user.id} successfully received private post from person #{@sender.guid}: #{@object.inspect}") obj end @@ -95,21 +95,21 @@ class Postzord::Receiver::Private < Postzord::Receiver #validations def relayable_without_parent? if @object.respond_to?(:relayable?) && @object.parent.nil? - FEDERATION_LOGGER.info("event=receive status=abort reason='received a comment but no corresponding post' recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle} payload_type=#{@object.class})") + FEDERATION_LOGGER.error("event=receive status=abort reason='received a comment but no corresponding post' recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle} payload_type=#{@object.class})") return true end end def author_does_not_match_xml_author? if (@author.diaspora_handle != xml_author) - FEDERATION_LOGGER.info("event=receive status=abort reason='author in xml does not match retrieved person' payload_type=#{@object.class} recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle}") + FEDERATION_LOGGER.error("event=receive status=abort reason='author in xml does not match retrieved person' payload_type=#{@object.class} recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle}") return true end end def contact_required_unless_request unless @object.is_a?(Request) || @user.contact_for(@sender) - FEDERATION_LOGGER.info("event=receive status=abort reason='sender not connected to recipient' recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle}") + FEDERATION_LOGGER.error("event=receive status=abort reason='sender not connected to recipient' recipient=#{@user_person.diaspora_handle} sender=#{@sender.diaspora_handle}") return true end end diff --git a/lib/webfinger.rb b/lib/webfinger.rb index 5c6e0f7ef..25f4002c3 100644 --- a/lib/webfinger.rb +++ b/lib/webfinger.rb @@ -2,12 +2,12 @@ require Rails.root.join('lib', 'hcard') require Rails.root.join('lib', 'webfinger_profile') class Webfinger - attr_accessor :host_meta_xrd, :webfinger_profile_xrd, - :webfinger_profile, :hcard, :hcard_xrd, :person, + attr_accessor :host_meta_xrd, :webfinger_profile_xrd, + :webfinger_profile, :hcard, :hcard_xrd, :person, :account, :ssl def initialize(account) - self.account = account + self.account = account self.ssl = true end @@ -56,7 +56,7 @@ class Webfinger else person = make_person_from_webfinger end - FEDERATION_LOGGER.info("successfully webfingered#{@account}") if person + FEDERATION_LOGGER.info("successfully webfingered #{@account}") if person person end @@ -95,7 +95,7 @@ class Webfinger def webfinger_profile_xrd @webfinger_profile_xrd ||= get(webfinger_profile_url) - FEDERATION_LOGGER.info "#{@account} doesn't exists anymore" if @webfinger_profile_xrd == false + FEDERATION_LOGGER.warn "#{@account} doesn't exists anymore" if @webfinger_profile_xrd == false @webfinger_profile_xrd end