Class: Logit::Logger
- Inherits:
-
Logger
- Object
- Logger
- Logit::Logger
- Defined in:
- lib/logit.rb
Overview
The actual logger.
Example:
c = Logit::Logger.new('custom')
such that the resulting log, for example, is
#{RAILS_ROOT}/log/custom_development.log
when running under Rails.
You can flush the log by calling logger.flush().
Constant Summary collapse
- DEFAULT_OPTS =
{:write_mode => 'a', :flush_mode => :default}
Instance Method Summary collapse
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
-
#flush ⇒ Object
Causes any pending writes to be flushed to disk.
- #format_message(severity, timestamp, progname, msg) ⇒ Object
-
#initialize(log_path, opts = DEFAULT_OPTS) ⇒ Logger
constructor
A new instance of Logger.
Constructor Details
#initialize(log_path, opts = DEFAULT_OPTS) ⇒ Logger
Returns a new instance of Logger.
121 122 123 124 125 |
# File 'lib/logit.rb', line 121 def initialize(log_path, opts = DEFAULT_OPTS) @opts = DEFAULT_OPTS.merge(opts) @f = File.open(log_path, @opts[:write_mode]) super @f end |
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
136 137 138 139 |
# File 'lib/logit.rb', line 136 def add(severity, = nil, progname = nil, &block) super(severity, , progname, &block) flush() if @opts[:flush_mode] == :immediate end |
#flush ⇒ Object
Causes any pending writes to be flushed to disk
142 143 144 |
# File 'lib/logit.rb', line 142 def flush() @f.flush() end |
#format_message(severity, timestamp, progname, msg) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/logit.rb', line 127 def (severity, , progname, msg) name = (progname) ? " [#{progname}]" : "" = "#{timestamp.strftime('%m-%d-%Y %H:%M:%S')} #{severity.ljust(6)}#{name}: #{msg}\n" puts if @opts[:stdout] end |