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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chrono_logger.rb', line 56

def initialize(log = nil, opt = {})
  @dev = @filename = @pattern = nil
  if defined?(LogDeviceMutex) # Ruby < 2.3
    @mutex = LogDeviceMutex.new
  else
    mon_initialize
    @mutex = self
  end
  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



84
85
86
# File 'lib/chrono_logger.rb', line 84

def close
  @dev.close rescue nil
end

#write(message) ⇒ Object



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

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