Class: Logger::LogDevice

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

Overview

Device used for logging messages.

Defined Under Namespace

Classes: LogDeviceMutex

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of LogDevice.



547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/logger.rb', line 547

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
  end
end

Instance Attribute Details

#devObject (readonly)

Returns the value of attribute dev



540
541
542
# File 'lib/logger.rb', line 540

def dev
  @dev
end

#filenameObject (readonly)

Returns the value of attribute filename



541
542
543
# File 'lib/logger.rb', line 541

def filename
  @filename
end

Instance Method Details

#closeObject



582
583
584
585
586
587
588
589
590
# File 'lib/logger.rb', line 582

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

#write(message) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/logger.rb', line 561

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