开发者

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 开发者_如何学Cpath!") # => 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".


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜