Class: Logger::LogDevice

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

Overview

Device used for logging messages.

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.



632
633
634
635
636
637
638
639
640
641
# File 'lib/logger.rb', line 632

def initialize(log = nil, opt = {})
  @dev = @filename = @shift_age = @shift_size = nil
  mon_initialize
  set_dev(log)
  if @filename
    @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



628
629
630
# File 'lib/logger.rb', line 628

def dev
  @dev
end

#filenameObject (readonly)

Returns the value of attribute filename



629
630
631
# File 'lib/logger.rb', line 629

def filename
  @filename
end

Instance Method Details

#closeObject



664
665
666
667
668
669
670
671
672
# File 'lib/logger.rb', line 664

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

#reopen(log = nil) ⇒ Object



674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/logger.rb', line 674

def reopen(log = nil)
  # reopen the same filename if no argument, do nothing for IO
  log ||= @filename if @filename
  if log
    synchronize do
      if @filename and @dev
        @dev.close rescue nil # close only file opened by Logger
        @filename = nil
      end
      set_dev(log)
    end
  end
  self
end

#write(message) ⇒ Object



643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/logger.rb', line 643

def write(message)
  begin
    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