Class: ThinService::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/thin_service/logger.rb

Constant Summary collapse

Levels =
{ 
  :name   => { :emergency => 0, :alert => 1, :critical => 2, :error => 3, 
               :warning => 4, :notice => 5, :info => 6, :debug => 7 },
  :id     => { 0 => :emergency, 1 => :alert, 2 => :critical, 3 => :error, 
               4 => :warning, 5 => :notice, 6 => :info, 7 => :debug }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, log_level = :debug) ⇒ Log

Returns a new instance of Log.



17
18
19
20
21
22
23
24
25
26
# File 'lib/thin_service/logger.rb', line 17

def initialize(log, log_level = :debug)
  @logger    = initialize_io(log)
  @log_level = Levels[:name][log_level] || 7

  if !RUBY_PLATFORM.match(/java|mswin|mingw/) && !(@log == STDOUT) && 
       @log.respond_to?(:write_nonblock)
    @aio = true
  end
  $ThinServiceLogger = self
end

Instance Attribute Details

#log_levelObject

Returns the value of attribute log_level.



8
9
10
# File 'lib/thin_service/logger.rb', line 8

def log_level
  @log_level
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/thin_service/logger.rb', line 7

def logger
  @logger
end

Instance Method Details

#log(*args) ⇒ Object

Writes a string to the logger. Writing of the string is skipped if the string’s log level is higher than the logger’s log level. If the logger responds to write_nonblock and is not on the java or windows platforms then the logger will use non-blocking asynchronous writes.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/thin_service/logger.rb', line 31

def log(*args)
  if args[0].is_a?(String)
    level, string = 6, args[0]
  else
    level = (args[0].is_a?(Fixnum) ? args[0] : Levels[:name][args[0]]) || 6
    string = args[1]
  end
  
  return if (level > log_level)

  if @aio
    @log.write_nonblock("#{Time.now} | #{Levels[:id][level]} | #{string}\n")
  else
    @log.write("#{Time.now} | #{Levels[:id][level]} | #{string}\n")
  end
end