Module: Logit::ClassMethods
- Defined in:
- lib/logit.rb
Constant Summary collapse
- DEFAULT_OPTS =
{:write_mode => 'a'}
Instance Method Summary collapse
- #logit_in_rails? ⇒ Boolean
-
#logit_log_name(name, opts) ⇒ Object
Tries to figure out what the fully qualified path name of the log file should be.
- #logit_strip_dot_log(name) ⇒ Object
-
#logs_to(name, opts = {}) ⇒ Object
Options *
:write_mode- mode used when opening the log file.
Instance Method Details
#logit_in_rails? ⇒ Boolean
90 91 92 93 94 95 96 |
# File 'lib/logit.rb', line 90 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.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/logit.rb', line 62 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
83 84 85 86 87 88 89 |
# File 'lib/logit.rb', line 83 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_ageis a number. -
:progname- Logging program name. The:prognamevalue is used in the default logging format if defined.
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
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/logit.rb', line 44 def logs_to(name, opts={}) opts = DEFAULT_OPTS.merge(opts) path = logit_log_name(name, opts) self.send :define_method, :logger do unless @logger @logger = Logit::Logger.new(path, opts) if opts[:progname] @logger.progname = opts[:progname] end end @logger end end |