Class: TestaLogger::LogDevice

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

Overview

Device used for logging messages.

Defined Under Namespace

Modules: Period

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, shift_age: nil, shift_size: nil, shift_period_suffix: nil, binmode: false, before_shift: nil) ⇒ LogDevice

Returns a new instance of LogDevice.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/testa_logger/log_device.rb', line 13

def initialize(log, shift_age: nil, shift_size: nil, shift_period_suffix: nil, binmode: false, before_shift: nil)
  @dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
  @before_shift = before_shift
  @binmode = binmode
  mon_initialize
  set_dev(log)

  return unless @filename

  @shift_age = shift_age || 7
  @shift_size = shift_size || 1_048_576
  @shift_period_suffix = shift_period_suffix || "%Y%m%d"

  return if @shift_age.is_a?(Integer)

  base_time = @dev.respond_to?(:stat) ? @dev.stat.mtime : Time.now
  @next_rotate_time = next_rotate_time(base_time, @shift_age)
end

Instance Attribute Details

#devObject (readonly)

Returns the value of attribute dev.



11
12
13
# File 'lib/testa_logger/log_device.rb', line 11

def dev
  @dev
end

#filenameObject (readonly)

Returns the value of attribute filename.



11
12
13
# File 'lib/testa_logger/log_device.rb', line 11

def filename
  @filename
end

Instance Method Details

#closeObject



53
54
55
56
57
58
59
60
61
# File 'lib/testa_logger/log_device.rb', line 53

def close
  synchronize do
    @dev.close rescue nil
  end
rescue SystemExit
  exit 1
rescue Exception
  @dev.close rescue nil
end

#reopen(log = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/testa_logger/log_device.rb', line 63

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 && @dev
        @dev.close rescue nil # close only file opened by Logger
        @filename = nil
      end
      set_dev(log)
    end
  end
  self
end

#write(message) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/testa_logger/log_device.rb', line 32

def write(message)
  synchronize do
    if @shift_age && @dev.respond_to?(:stat)
      begin
        check_shift_log
      rescue StandardError => e
        warn("log shifting failed. #{e}")
      end
    end
    begin
      @dev.write(message)
    rescue StandardError => e
      warn("log writing failed 1. #{e}")
    end
  end
rescue SystemExit
  exit 1
rescue Exception => e
  warn("log writing failed 2. #{e}")
end