Method: Logit::ClassMethods#logs_to

Defined in:
lib/logit.rb

#logs_to(name, opts = {}) ⇒ Object

Options

  • :write_mode - mode used when opening the log file. You’ll ususually just want ‘a’ for append or ‘w’ for overwrite. Defaults to ‘a’.

  • :shift_age - Number of old logs to keep or frequency of rotation.

  • :shift_size - Maximum logfile size that only applies when :shift_age is a number.

  • :progname - Logging program name. The :progname value is used in the default logging format if defined.

  • :flush_mode - One of :immediate or :default. :immediate will cause a write to the log file for each message logged. The default behavior is to use default file buffering.

  • :stdout - If set to true, this will cause logs to be printed to stdout in addition to the log file.

  • :log_method - The name of the instance method to define to access the logger instance. Defaults to :logger.

Examples

class Publisher
  include Logit

  logs_to "/tmp/publisher.log"

  def do_it
    logger.info("doing something")
  end
end

class Publisher2
  include Logit

  logs_to :publisher, :progname => "Publisher #{Process.pid}"
                      :shift_age => 'daily'
  def do_it
    logger.info("doing something")
  end
end

class Publisher3
  include Logit

  logs_to :publisher, :log_method => :pub_log

  def do_it
    pub_log.info("doing something")
  end
end


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/logit.rb', line 57

def logs_to(name, opts={})
  opts = DEFAULT_OPTS.merge(opts)
  path = logit_log_name(name, opts)

  self.class.send :define_method, :logit_logger do
    unless @logger
      @logger =  Logit::Logger.new(path, opts)
      if opts[:progname]
        @logger.progname = opts[:progname]
      end
    end
    @logger
  end

  self.send :define_method, opts[:log_method] do
    self.class.send :logit_logger
  end
end