Class: ChronoLogger::TimeBasedLogDevice

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

Constant Summary collapse

DELAY_SECOND_TO_CLOSE_FILE =
5

Constants included from Period

Period::DAILY, Period::SiD

Instance Method Summary collapse

Methods included from Period

#determine_period, #next_start_period

Constructor Details

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

Returns a new instance of TimeBasedLogDevice.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chrono_logger.rb', line 55

def initialize(log = nil, opt = {})
  @dev = @filename = nil
  @mutex = LogDeviceMutex.new
  if log.respond_to?(:write) and log.respond_to?(:close)
    @dev = log
  else
    @pattern = log
    @period = determine_period(@pattern)
    now = Time.now
    @filename = now.strftime(@pattern)
    @next_start_period = next_start_period(now, @period)
    @dev = open_logfile(@filename)
    @dev.sync = true
  end
end

Instance Method Details

#closeObject


78
79
80
# File 'lib/chrono_logger.rb', line 78

def close
  @dev.close rescue nil
end

#write(message) ⇒ Object


71
72
73
74
75
76
# File 'lib/chrono_logger.rb', line 71

def write(message)
  check_shift_log if @pattern
  @dev.write(message)
rescue
  warn("log writing failed. #{$!}")
end