Class: Logit::Logger

Inherits:
Logger
  • Object
show all
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

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, message = nil, progname = nil, &block)
  super(severity, message, progname, &block)
  flush() if @opts[:flush_mode] == :immediate
end

#flushObject

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 format_message(severity, timestamp, progname, msg)
  
  name = (progname) ? " [#{progname}]" : ""
  
  message = "#{timestamp.strftime('%m-%d-%Y %H:%M:%S')} #{severity.ljust(6)}#{name}: #{msg}\n"
  puts message if @opts[:stdout]
  message
end