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.

Instance Method Summary collapse

Constructor Details

#initialize(log_path, opts = DEFAULT_OPTS) ⇒ Logger

Returns a new instance of Logger.



133
134
135
136
137
138
139
140
# File 'lib/logit.rb', line 133

def initialize(log_path, opts = DEFAULT_OPTS)
  @opts = DEFAULT_OPTS.merge(opts)
  f = File.open(log_path, @opts[:write_mode])
  if (opts[:flush_mode] == :immediate)
    f.sync = true
  end
  super f
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



150
151
152
# File 'lib/logit.rb', line 150

def add(severity, message = nil, progname = nil, &block)
  super(severity, message, progname, &block)
end

#flushObject

flushes any buffered content to the log



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/logit.rb', line 155

def flush
  # get a handle to the actual file and then synchronize on the mutex
  # that LogDevice is using (so we don't have the file closed or
  # swapped out from under us)
  if @logdev 
    mutex = @logdev.instance_variable_get('@mutex')
    if (mutex)
      mutex.synchronize do 
        dev = @logdev.dev
        if (dev and !dev.closed?)
          dev.flush()
        end
      end
    end
  end
  true
end

#format_message(severity, timestamp, progname, msg) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/logit.rb', line 142

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