title: Define class and instance methods at once - Ruby
body: <p>I would like:</p>
  
  <pre><code>module MyLog
  
    def log
      unless @log
        @log = Logger.new(log_path)
        @log.formatter =  proc do |severity, datetime, progname, msg|
          "#{datetime} #{msg}\n"
        end
      end
      @log
    end
  
  end
  </code></pre>
  
  <p>To be reused between other classes like this:</p>
  
  <pre><code>Class A
    def self.log_path; 'log/a.log' end
    def log_path; 'log/a.log' end
    include MyLog
    extend  MyLog
  
    def some_method
      log.debug 'some thing'
    end
  
    def self.some_class_method
      log.debug 'in a class method'
    end
  
  end
  </code></pre>
  
  <p>Is there a shorter way than those four lines at start of class A?</p>
  
  <h1>Another thing</h1>
  
  <p>I would like to log by batches:</p>
  
  <pre><code>def expire
    expired_ids = []
    failed_ids  = []
  
    all.each do |event|
      if event.expire # saves record
        expired_ids &lt;&lt; event.id
      else
        failed_ids &lt;&lt; event.id
      end
    end
  
    log.debug "These ids were expired: #{ expired_ids }"
    log.debug "These ids failed to expire: #{ failed_ids }"
  end
  </code></pre>
  
  <p>Is there a way I can do this cleanly? Separating logging from method logic?</p>