Andrew Grimm
Andrew Grimm

Reputation: 81590

Logging a certain event only once in ruby

Are there any logging frameworks in ruby that allow you to log a specific event type only once?

logger = IdealLogger.new
logger.log(:happy_path, "We reached the happy path") # => logs this message
logger.log(:happy_path, "We reached the happy path yet again") # => Doesn't log this
logger.log(:sad_path, "We've encountered a sad path!") # => logs this message

Also, is there a term for the concept of logging a certain event type only once?

Edit: I'm using Plain Old Ruby Objects, not Rails. I had in mind "once per time the script is run" for "once".

Upvotes: 2

Views: 165

Answers (1)

x1a4
x1a4

Reputation: 19485

I'm not aware of one, but extending Logger to make your own isn't too difficult. It's essentially implementing caching for your logging, but instead of fetching from the cache and returning it like you would with a normal app, you quash it when it's been cached. Implementation and expiration strategy of this log cache are left as an exercise for the reader.

something like:

class IdealLogger < Logger
  def info(event = nil, progname = nil, &block)
    super(progname, &block) unless event_is_cached(event)
  end

  # define debug, warn, error, fatal, and unknown the same way, override others
  # as you wish.
end

Upvotes: 2

Related Questions