Module: Logit::ClassMethods

Defined in:
lib/logit.rb

Instance Method Summary collapse

Instance Method Details

#logit_in_rails?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
# File 'lib/logit.rb', line 108

def logit_in_rails?
  begin
    Module.const_get(:Rails)
    return true
  rescue NameError
  end
end

#logit_log_name(name, opts) ⇒ Object

Tries to figure out what the fully qualified path name of the log file should be.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/logit.rb', line 80

def logit_log_name(name, opts)
  path = name.to_s.strip

  #  if they are giving a path like '/var/log/foo.log'
  #  then we shouldn't presume to stick it in the Rails log dir
  unless (path =~ /\/+/) 
    if (logit_in_rails?)
      # take off any trailing .log so we can attach the environment
      # name
      path = logit_strip_dot_log(path)
      path = File.join(RAILS_ROOT, 'log', "#{name}_#{Rails.env}.log")
    end
  end
  # see if we need to append .log
  # is this a bit presumptuous?
  unless (path =~ /\.log$/)
    path << ".log"
  end
  path
end

#logit_strip_dot_log(name) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/logit.rb', line 101

def logit_strip_dot_log(name)
  if (name =~ /\.log$/)
    name.slice(0, name =~ /\.log$/) 
  else
    name
  end
end

#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