Module: Logit::ClassMethods
- Defined in:
- lib/logit.rb
Constant Summary collapse
- DEFAULT_OPTS =
{:write_mode => 'a', :flush_mode => :default, :stdout => false}
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
92 93 94 95 96 97 98 |
# File 'lib/logit.rb', line 92 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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/logit.rb', line 64 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
85 86 87 88 89 90 91 |
# File 'lib/logit.rb', line 85 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. -
:flush_mode- One of:immediateor:default.:immediatewill cause a write to the log file for each message logged. The default behavior is to use default file buffering. -
:stdout- If set totrue, this will cause logs to be printed to stdout in addition to the log file.
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
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/logit.rb', line 46 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 |