Class: MLogger::LogDeveices::LogDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/mlogger.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.



511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/mlogger.rb', line 511

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[:age] || 7
    @shift_size = opt[:size] || 1048576
  end
end

Instance Attribute Details

#devObject (readonly)

Returns the value of attribute dev.



504
505
506
# File 'lib/mlogger.rb', line 504

def dev
  @dev
end

#filenameObject (readonly)

Returns the value of attribute filename.



505
506
507
# File 'lib/mlogger.rb', line 505

def filename
  @filename
end

Instance Method Details

#closeObject



546
547
548
549
550
551
552
553
554
555
556
# File 'lib/mlogger.rb', line 546

def close
  return if @dev == STDOUT || @dev == STDERR

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

#write(message) ⇒ Object



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/mlogger.rb', line 525

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