lib/pushyd/endpoint.rb in pushyd-0.3.4 vs lib/pushyd/endpoint.rb in pushyd-0.4.0
- old
+ new
@@ -8,75 +8,103 @@
class EndpointSubscribeError < StandardError; end
class Endpoint
def initialize
+ # Prepare logger
+ init_logger Conf[:log]
+
+ # Done
+ info "endpoint initialized"
+ # loop do
+ # info "info"
+ # info ["info1", "info2", "info3"]
+ # error "error"
+ # debug "debug"
+ # sleep 1
+ # end
+ end
+
+ protected
+
+ def init_logger logconf
+ # Extract context
+ logfile = logconf[:file]
+ loglevel = logconf[:level]
+ me = self.class.name
+
# Prepare logger (may be NIL > won't output anything)
- logfile = Conf[:log]
+ @logger = Logger.new(logfile, LOG_ROTATION)
+ @logger.formatter = Formatter
- # Create the logger
- @logger = PushyLogger.new(logfile, LOG_ROTATION)
- @logger.add Logger::INFO, "starting #{self.class.name}"
+ # Set progname
+ @logger.progname = me.split('::').last
- # Declare we're now logging
- puts "#{self.class} logging to #{logfile}"
+ # Set expected level
+ @logger.level = case loglevel
+ when "debug"
+ Logger::DEBUG
+ when "info"
+ Logger::INFO
+ when "warn"
+ Logger::WARN
+ else
+ Logger::INFO
+ end
+
+ # Announce on STDOUT we're now logging to file
+ if logfile
+ puts "#{self.class} logging loglevel [#{loglevel} > #{@logger.level}] to [#{logfile}]"
+ else
+ puts "#{self.class} logging disabled"
+ end
end
- protected
+ def error messages
+ @logger.error messages
+ end
- def error message, lines = {}
- @logger.add Logger::ERROR, "#{self.class}: #{message}", lines
+ def info messages
+ @logger.info messages
end
- def info message, lines = {}
- @logger.add Logger::INFO, "#{self.class}: #{message}", lines
+ def debug messages
+ @logger.debug messages
end
def message params = {}
- # Indenting
- lines = []
-
# Header
- message = sprintf(
+ @logger.info sprintf(
"%3s %-15s %s",
params[:way],
params[:exchange],
params[:key]
)
# Attributes
- if (params[:attrs].is_a? Hash)
- # lines.merge params[:attrs]
- params[:attrs].each do |name, value|
- lines << sprintf("%-15s %s", name, value)
- end
- end
+ @logger.debug params[:attrs] if params[:attrs].is_a?(Hash)
# Body (split in lines to log them separately)
- if params[:body] && params[:body].is_a?(Enumerable)
+ if params[:body].is_a?(Enumerable) && !params[:body].empty?
body_json = JSON.pretty_generate(params[:body])
- body_json.each_line do |line|
- lines << line.rstrip
- end
+ #puts "log? #{params[:body]} "
+ @logger.debug body_json.lines
end
-
- # Send the info
- @logger.add Logger::INFO, message, lines
- # @logger.log_info message, lines
end
# Start connexion to RabbitMQ
def connect_channel busconf
fail PushyDaemon::EndpointConnexionContext, "invalid bus host/port" unless (busconf.is_a? Hash) &&
busconf[:host] && busconf[:port]
info "connecting to #{busconf[:host]} port #{busconf[:port]}"
- conn = Bunny.new host: (busconf[:host].to_s || "localhost").to_s,
+ conn = Bunny.new host: busconf[:host].to_s,
port: busconf[:port].to_i,
user: busconf[:user].to_s,
pass: busconf[:pass].to_s,
- heartbeat: :server
+ heartbeat: :server,
+ logger: @logger
conn.start
# Create channel
channel = conn.create_channel
@@ -114,10 +142,11 @@
# Bind exchange to queue
queue.bind topic_exchange, routing_key: route
info "subscribe: bind [#{rule_topic}/#{route}] \t> #{rule_queue}"
# Add row to config table
- @table.add_row [rule_name, rule_topic, route, rule[:relay].to_s, rule[:title].to_s ]
+ # ["rule", "topic", "route", "relay", "queue", "description"]
+ @table.add_row [rule_name, rule_topic, route, rule[:relay].to_s, rule_queue, rule[:title].to_s ]
end
# Subscribe to our new queue
queue.subscribe(block: false, manual_ack: PROXY_USE_ACK, message_max: PROXY_MESSAGE_MAX) do |delivery_info, metadata, payload|