Module: Resqued::Logging

Included in:
Listener, ListenerProxy, Master, Worker
Defined in:
lib/resqued/logging.rb

Overview

Mixin for any class that wants to write messages to the log file.

Defined Under Namespace

Classes: ResquedLogFormatter, ResquedLoggingIOWrapper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_loggerObject



13
14
15
16
17
# File 'lib/resqued/logging.rb', line 13

def build_logger
  MonoLogger.new(ResquedLoggingIOWrapper.new).tap do |logger|
    logger.formatter = ResquedLogFormatter.new
  end
end

.close_logObject

Public: Make sure the log IO is closed.



54
55
56
57
58
59
# File 'lib/resqued/logging.rb', line 54

def close_log
  if @logging_io && @logging_io != $stdout
    @logging_io.close
    @logging_io = nil
  end
end

.log_fileObject

Public.



68
69
70
# File 'lib/resqued/logging.rb', line 68

def log_file
  ENV['RESQUED_LOGFILE']
end

.log_file=(path) ⇒ Object

Public.



62
63
64
65
# File 'lib/resqued/logging.rb', line 62

def log_file=(path)
  ENV['RESQUED_LOGFILE'] = File.expand_path(path)
  close_log
end

.loggerObject

Public: Get a ‘Logger`.



9
10
11
# File 'lib/resqued/logging.rb', line 9

def logger
  @logger ||= build_logger
end

.logging_ioObject

Private: Get an IO to write log messages to.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/resqued/logging.rb', line 37

def logging_io
  @logging_io = nil if @logging_io && @logging_io.closed?
  @logging_io ||=
    if path = Resqued::Logging.log_file
      File.open(path, 'a').tap do |f|
        f.sync = true
        f.close_on_exec = true
        # Make sure we're not holding onto a stale filehandle.
        $stdout.reopen(f)
        $stderr.reopen(f)
      end
    else
      $stdout
    end
end

Instance Method Details

#log(level, message = nil) ⇒ Object

Private (in classes that include this module)



84
85
86
87
# File 'lib/resqued/logging.rb', line 84

def log(level, message = nil)
  level, message = :info, level if message.nil?
  Resqued::Logging.logger.send(level, self.class.name) { message }
end

#log_to_stdout?Boolean

Public.

Returns:

  • (Boolean)


74
75
76
# File 'lib/resqued/logging.rb', line 74

def log_to_stdout?
  Resqued::Logging.log_file.nil?
end

#reopen_logsObject

Public: Re-open all log files.



79
80
81
# File 'lib/resqued/logging.rb', line 79

def reopen_logs
  Resqued::Logging.close_log # it gets opened the next time it's needed.
end