代码之家  ›  专栏  ›  技术社区  ›  Andrew Grimm Alex Wayne

在ruby中只记录一次特定事件

  •  2
  • Andrew Grimm Alex Wayne  · 技术社区  · 16 年前

    ruby中是否有允许您只记录一次特定事件类型的日志框架?

    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
    

    另外,是否有一个术语来描述只记录一次特定事件类型的概念?

    :我正在使用 Plain Old Ruby Objects ,而不是轨道。我把“每次脚本运行一次”记为“一次”。

    1 回复  |  直到 16 年前
        1
  •  2
  •   x1a4    16 年前

    我不知道其中一个,但扩展记录器使自己不是太难。它本质上是为日志实现缓存,但不是像使用普通应用程序那样从缓存中提取并返回它,而是在缓存时取消它。这个日志缓存的实现和过期策略留给读者作为练习。

    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