Wilson Silva
Wilson Silva
All Posts Apr 13 2024

Nostr 0.7.0 - Logging

I’m excited to announce the release of version 0.7.0 of the nostr gem, which introduces logging and debugging capabilities to help you monitor and troubleshoot your Nostr client’s interactions with relays.

As an open-source maintainer, I appreciate community engagement. On March 13th, a developer asked for my help to debug an issue with integrating the nostr gem into his application:

Hello Wilson,

I’m Matej Lukášik a RoR developer. Recently I became interested in nostr and found your client. It looks pretty neat and I really appreciate the care you took around documentation and examples.

I would like to help. However, I can’t make it work :-D This are of websockets and event machine are quite new to me and I don’t even know how to debug things properly.

Would you ever have some time to show me around and make it work on my computer so that I can tinker with it later? I would pay you your normal hourly rate if you like.

What do you think? Would you find time to give me a paid introduction to the gem for an hour or so?

I was happy to assist for free. We jumped on a call a few hours later.

While debugging, I realized that the nostr gem needed default logging to provide developers with visibility into their application’s relay interactions. This realization led to prioritizing logging in this release.

I also realized Matej’s code was immediately exiting, potentially before the client could handle all asynchronous operations.

You see, the Nostr client relies on EventMachine, which uses a reactor pattern. For the client to process all the events and messages properly, the main thread needs to be kept alive while the EventMachine reactor runs in the background. By adding gets, we pause the main thread, waiting for user input, and giving the background thread the time it needs to do its job.

This small addition to the example code ensures that the client can connect to relays, send messages, and receive responses as expected. It’s a tiny but important detail, especially for newcomers who might be wondering why their client disconnects unexpectedly.

To make sure developers have a smooth experience with the nostr gem, I’ve updated the README with this helpful tidbit.

Built-in Logging Functionality

The Nostr::Client class now comes with built-in logging functionality. By default, it uses the ColorLogger, which logs events in color. Here’s an example of how to use the default logger:

client = Nostr::Client.new
Logging with colors
Logging with colors

If you prefer plain text logging without colors, you can use the PlainLogger:

require 'nostr/client/plain_logger'

client = Nostr::Client.new(logger: Nostr::Client::PlainLogger.new)
Logging without colors
Logging without colors

To disable logging entirely, simply pass logger: nil when creating the client instance:

client = Nostr::Client.new(logger: nil)

Customizable Loggers

In addition to the default loggers, you can create your own custom logger by subclassing Nostr::Client::Logger and overriding its methods to tailor the logging format to your needs:

class CustomLogger < Nostr::Client::Logger
  def on_connect(relay)
    puts "Connected to relay: #{relay.url}"
  def on_message(message)
    puts "Received message: #{message}"
  def on_send(message)
    puts "Sent message: #{message}"
  def on_error(message)
    puts "Error: #{message}"
  def on_close(code, reason)
    puts "Connection closed: #{reason} (Code: #{code})"

client = Nostr::Client.new(logger: CustomLogger.new)

More Changes in 0.7.0

This release includes several other improvements and updates. For a full list of changes, please refer to the CHANGELOG.

I would like to express my sincere gratitude to Matej Lukášik for reaching out, providing valuable feedback. His input directly influenced the implementation of logging in this release.

I encourage all users to continue sharing their experiences, suggestions, and feature requests. Your feedback is invaluable in shaping the future of the nostr gem. Together, we can make it an even more powerful and developer-friendly tool for building Nostr applications in Ruby.