module Roundhouse::Logging

Public Class Methods

initialize_logger(log_target = STDOUT) click to toggle source
# File lib/roundhouse/logging.rb, line 35
def self.initialize_logger(log_target = STDOUT)
  oldlogger = defined?(@logger) ? @logger : nil
  @logger = Logger.new(log_target)
  @logger.level = Logger::INFO
  @logger.formatter = ENV['DYNO'] ? WithoutTimestamp.new : Pretty.new
  oldlogger.close if oldlogger && !$TESTING # don't want to close testing's STDOUT logging
  @logger
end
logger() click to toggle source
# File lib/roundhouse/logging.rb, line 44
def self.logger
  defined?(@logger) ? @logger : initialize_logger
end
logger=(log) click to toggle source
# File lib/roundhouse/logging.rb, line 48
def self.logger=(log)
  @logger = (log ? log : Logger.new('/dev/null'))
end
reopen_logs() click to toggle source

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

# File lib/roundhouse/logging.rb, line 59
def self.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
rescue RuntimeError => ex
  # RuntimeError: ObjectSpace is disabled; each_object will only work with Class, pass -X+O to enable
  puts "Unable to reopen logs: #{ex.message}"
end
with_context(msg) { || ... } click to toggle source
# File lib/roundhouse/logging.rb, line 27
def self.with_context(msg)
  Thread.current[:roundhouse_context] ||= []
  Thread.current[:roundhouse_context] << msg
  yield
ensure
  Thread.current[:roundhouse_context].pop
end

Public Instance Methods

logger() click to toggle source
# File lib/roundhouse/logging.rb, line 100
def logger
  Roundhouse::Logging.logger
end