Module: Toro::Logging

Defined in:
lib/toro/logging.rb

Defined Under Namespace

Classes: Formatter

Class Method Summary collapse

Class Method Details

.initialize_logger(log_target = nil) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/toro/logging.rb', line 10

def initialize_logger(log_target=nil)
  if log_target.nil?
    log_target = $TESTING ? '/dev/null' : STDOUT
  end

  old_logger = defined?(@logger) ? @logger : nil
  @logger = Logger.new(log_target)
  @logger.level = $TESTING ? Logger::DEBUG : Logger::INFO
  @logger.formatter = Formatter.new
  old_logger.close if old_logger && !$TESTING # don't want to close testing's STDOUT logging
  Celluloid.logger = @logger
  @logger
end

.loggerObject


24
25
26
# File 'lib/toro/logging.rb', line 24

def logger
  defined?(@logger) ? @logger : initialize_logger
end

.logger=(log) ⇒ Object


28
29
30
31
32
# File 'lib/toro/logging.rb', line 28

def logger=(log)
  @logger = (log ? log : Logger.new('/dev/null'))
  Celluloid.logger = @logger
  @logger
end

.reopen_logsObject

This reopens ALL logfiles in the process that have been rotated using logrotate(8) (without copytruncate) or similar tools. A File object is considered for reopening if it is:

1) opened with the O_APPEND and O_WRONLY flags
2) the current open file handle does not match its original open path
3) unbuffered (as far as userspace buffering goes, not O_SYNC)

Returns the number of files reopened


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/toro/logging.rb', line 41

def reopen_logs
  to_reopen = []
  append_flags = File::WRONLY | File::APPEND

  ObjectSpace.each_object(File) do |fp|
    begin
      if !fp.closed? && fp.stat.file? && fp.sync && (fp.fcntl(Fcntl::F_GETFL) & append_flags) == append_flags
        to_reopen << fp
      end
    rescue IOError, Errno::EBADF
    end
  end

  nr = 0
  to_reopen.each do |fp|
    orig_st = begin
      fp.stat
    rescue IOError, Errno::EBADF
      next
    end

    begin
      b = File.stat(fp.path)
      next if orig_st.ino == b.ino && orig_st.dev == b.dev
    rescue Errno::ENOENT
    end

    begin
      File.open(fp.path, 'a') { |tmpfp| fp.reopen(tmpfp) }
      fp.sync = true
      nr += 1
    rescue IOError, Errno::EBADF
      # not much we can do...
    end
  end
  nr
end