Class: Logger::LogDevice

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

Overview

Device used for logging messages.

Defined Under Namespace

Classes: LogDeviceMutex

Constant Summary

Constants included from Period

Period::SiD

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Period

next_rotate_time, previous_period_end

Constructor Details

#initialize(log = nil, opt = {}) ⇒ LogDevice

Returns a new instance of LogDevice.



578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/logger.rb', line 578

def initialize(log = nil, opt = {})
  @dev = @filename = @shift_age = @shift_size = nil
  @mutex = LogDeviceMutex.new
  if log.respond_to?(:write) and log.respond_to?(:close)
    @dev = log
  else
    @dev = open_logfile(log)
    @dev.sync = true
    @filename = log
    @shift_age = opt[:shift_age] || 7
    @shift_size = opt[:shift_size] || 1048576
    @next_rotate_time = next_rotate_time(Time.now, @shift_age) unless @shift_age.is_a?(Integer)
  end
end

Instance Attribute Details

#devObject (readonly)

Returns the value of attribute dev



571
572
573
# File 'lib/logger.rb', line 571

def dev
  @dev
end

#filenameObject (readonly)

Returns the value of attribute filename



572
573
574
# File 'lib/logger.rb', line 572

def filename
  @filename
end

Instance Method Details

#closeObject



614
615
616
617
618
619
620
621
622
# File 'lib/logger.rb', line 614

def close
  begin
    @mutex.synchronize do
      @dev.close rescue nil
    end
  rescue Exception
    @dev.close rescue nil
  end
end

#write(message) ⇒ Object



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/logger.rb', line 593

def write(message)
  begin
    @mutex.synchronize do
      if @shift_age and @dev.respond_to?(:stat)
        begin
          check_shift_log
        rescue
          warn("log shifting failed. #{$!}")
        end
      end
      begin
        @dev.write(message)
      rescue
        warn("log writing failed. #{$!}")
      end
    end
  rescue Exception => ignored
    warn("log writing failed. #{ignored}")
  end
end