Commit 51661091 authored by Maxwell Salzberg's avatar Maxwell Salzberg
Browse files

Merge pull request #3385 from Raven24/federation-colors

Federation logger stuff[ci skip]
parents 0c8621fa bff069ab
......@@ -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
......
......@@ -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
......
#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
......@@ -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
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment