lib/pushyd/endpoint.rb in pushyd-0.5.3 vs lib/pushyd/endpoint.rb in pushyd-0.6.0
- old
+ new
@@ -6,24 +6,19 @@
class EndpointConnectionError < StandardError; end
class EndpointSubscribeContext < StandardError; end
class EndpointSubscribeError < StandardError; end
class Endpoint
+ include Shared::LoggerHelper
+ attr_reader :logger
def initialize
# Prepare logger
init_logger Conf[:logs]
# Done
- info "endpoint initialized"
- # loop do
- # info "info"
- # info ["info1", "info2", "info3"]
- # error "error"
- # debug "debug"
- # sleep 1
- # end
+ log_info "endpoint initialized"
end
protected
def init_logger logconf
@@ -36,11 +31,11 @@
loglevel = logconf[:level]
me = self.class.name
# Prepare logger (may be NIL > won't output anything)
@logger = Logger.new(logfile, LOG_ROTATION)
- @logger.formatter = Formatter
+ @logger.formatter = Shared::LoggerFormatter
# Set progname
@logger.progname = me.split('::').last
# Set expected level
@@ -61,33 +56,22 @@
else
puts "#{self.class} logging disabled"
end
end
- def debug messages
- @logger.debug messages
- end
- def info message, lines = []
- @logger.info message
- debug_lines lines
- end
- def error messages
- @logger.error messages
- end
-
def log_message msg_way, msg_exchange, msg_key, msg_body = [], msg_attrs = {}
# Message header
- @logger.info sprintf("%3s %-15s %s", msg_way, msg_exchange, msg_key)
+ info sprintf("%3s %-15s %s", msg_way, msg_exchange, msg_key)
# Body lines
if msg_body.is_a?(Enumerable) && !msg_body.empty?
body_json = JSON.pretty_generate(msg_body)
- debug_lines body_json.lines
+ log_debug nil, body_json.lines
end
# Attributes lines
- debug_lines msg_attrs
+ log_debug nil, msg_attrs if msg_attrs
end
# Start connexion to RabbitMQ
def connect_channel busconf
fail PushyDaemon::EndpointConnexionContext, "invalid bus host/port" unless (busconf.is_a? Hash) &&
@@ -162,17 +146,13 @@
end
def handle_message rule, delivery_info, metadata, payload
end
- private
-
- def debug_lines lines, prefix = ''
- if lines.is_a? Array
- @logger.debug lines.map{ |line| sprintf(LOG_FORMAT_ARRAY, prefix, line) }
- elsif lines.is_a? Hash
- @logger.debug lines.map{ |key, value| sprintf(LOG_FORMAT_HASH, prefix, key, value) }
- end
+ def identifier len
+ rand(36**len).to_s(36)
end
+
+ private
end
end