Add plain text and coloured logging to the Client

Logs websocket events (connect, message, error and close events)
This commit is contained in:
Wilson Silva
2024-04-13 13:55:36 +01:00
parent a0cf41bfb4
commit 90ab1a6149
19 changed files with 742 additions and 10 deletions

View File

@@ -0,0 +1,69 @@
# frozen_string_literal: true
module Nostr
class Client
# Logs connection events, messages sent and received, errors, and connection closures in color.
class ColorLogger < Logger
# Logs connection to a relay
#
# @api private
#
# @param [Nostr::Relay] relay The relay the client connected to.
#
# @return [void]
#
def on_connect(relay)
puts "\u001b[32m\u001b[1mConnected to the relay\u001b[22m #{relay.name} (#{relay.url})\u001b[0m"
end
# Logs a message received from the relay
#
# @api private
#
# @param [String] message The message received.
#
# @return [void]
#
def on_message(message)
puts "\u001b[32m\u001b[1m◄-\u001b[0m #{message}"
end
# Logs a message sent to the relay
#
# @api private
#
# @param [String] message The message sent.
#
# @return [void]
#
def on_send(message)
puts "\u001b[32m\u001b[1m-►\u001b[0m #{message}"
end
# Logs an error message
#
# @api private
#
# @param [String] message The error message.
#
# @return [void]
#
def on_error(message)
puts "\u001b[31m\u001b[1mError: \u001b[22m#{message}\u001b[0m"
end
# Logs a closure of connection with a relay
#
# @api private
#
# @param [String] code The closure code.
# @param [String] reason The reason for the closure.
#
# @return [void]
#
def on_close(code, reason)
puts "\u001b[31m\u001b[1mConnection closed: \u001b[22m#{reason} (##{code})\u001b[0m"
end
end
end
end

View File

@@ -0,0 +1,85 @@
# frozen_string_literal: true
module Nostr
class Client
# Logs connection events, messages sent and received, errors, and connection closures.
class Logger
# Attaches event handlers to the specified Nostr client for logging purposes
#
# @api public
#
# @example Attaching the logger to a client
# client = Nostr::Client.new
# logger = Nostr::Client::Logger.new
# logger.attach_to(client)
#
# # Now, actions like connecting, sending messages, receiving messages,
# # errors, and closing the connection will be logged to the console.
#
# @param [Nostr::Client] client The client to attach logging functionality to.
#
# @return [void]
#
def attach_to(client)
logger_instance = self
client.on(:connect) { |relay| logger_instance.on_connect(relay) }
client.on(:message) { |message| logger_instance.on_message(message) }
client.on(:send) { |message| logger_instance.on_send(message) }
client.on(:error) { |message| logger_instance.on_error(message) }
client.on(:close) { |code, reason| logger_instance.on_close(code, reason) }
end
# Logs connection to a relay
#
# @api private
#
# @param [Nostr::Relay] relay The relay the client connected to.
#
# @return [void]
#
def on_connect(relay); end
# Logs a message received from the relay
#
# @api private
#
# @param [String] message The message received.
#
# @return [void]
#
def on_message(message); end
# Logs a message sent to the relay
#
# @api private
#
# @param [String] message The message sent.
#
# @return [void]
#
def on_send(message); end
# Logs an error message
#
# @api private
#
# @param [String] message The error message.
#
# @return [void]
#
def on_error(message); end
# Logs a closure of connection with a relay
#
# @api private
#
# @param [String] code The closure code.
# @param [String] reason The reason for the closure.
#
# @return [void]
#
def on_close(code, reason); end
end
end
end

View File

@@ -0,0 +1,69 @@
# frozen_string_literal: true
module Nostr
class Client
# Logs connection events, messages sent and received, errors, and connection closures.
class PlainLogger < Logger
# Logs connection to a relay
#
# @api private
#
# @param [Nostr::Relay] relay The relay the client connected to.
#
# @return [void]
#
def on_connect(relay)
puts "Connected to the relay #{relay.name} (#{relay.url})"
end
# Logs a message received from the relay
#
# @api private
#
# @param [String] message The message received.
#
# @return [void]
#
def on_message(message)
puts "◄- #{message}"
end
# Logs a message sent to the relay
#
# @api private
#
# @param [String] message The message sent.
#
# @return [void]
#
def on_send(message)
puts "-► #{message}"
end
# Logs an error message
#
# @api private
#
# @param [String] message The error message.
#
# @return [void]
#
def on_error(message)
puts "Error: #{message}"
end
# Logs a closure of connection with a relay
#
# @api private
#
# @param [String] code The closure code.
# @param [String] reason The reason for the closure.
#
# @return [void]
#
def on_close(code, reason)
puts "Connection closed: #{reason} (##{code})"
end
end
end
end