Module: Faktory::Logging

Defined in:
lib/faktory/logging.rb

Defined Under Namespace

Classes: Pretty, WithoutTimestamp

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initialize_logger(log_target = STDOUT) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/faktory/logging.rb', line 48

def self.initialize_logger(log_target = STDOUT)
  oldlogger = defined?(@logger) ? @logger : nil
  @logger = Logger.new(log_target)
  @logger.level = Logger::INFO
  # We assume that any TTY is logging directly to a terminal and needs timestamps.
  # We assume that any non-TTY is logging to Upstart/Systemd/syslog/Heroku/etc with a decent
  # logging subsystem that provides a timestamp for each entry.
  @logger.formatter = log_target.tty? ? Pretty.new : WithoutTimestamp.new
  oldlogger.close if oldlogger && !$TESTING # don't want to close testing's STDOUT logging
  @logger
end

.job_hash_context(job_hash) ⇒ Object



29
30
31
32
33
34
# File 'lib/faktory/logging.rb', line 29

def self.job_hash_context(job_hash)
  # If we're using a wrapper class, like ActiveJob, use the "wrapped"
  # attribute to expose the underlying thing.
  klass = job_hash.dig('custom', 'wrapped') || job_hash["jobtype"]
  "#{klass} JID-#{job_hash['jid']}"
end

.loggerObject



60
61
62
# File 'lib/faktory/logging.rb', line 60

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

.logger=(log) ⇒ Object



64
65
66
# File 'lib/faktory/logging.rb', line 64

def self.logger=(log)
  @logger = (log ? log : Logger.new(File::NULL))
end

.with_context(msg) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/faktory/logging.rb', line 40

def self.with_context(msg)
  Thread.current[:faktory_context] ||= []
  Thread.current[:faktory_context] << msg
  yield
ensure
  Thread.current[:faktory_context].pop
end

.with_job_hash_context(job_hash, &block) ⇒ Object



36
37
38
# File 'lib/faktory/logging.rb', line 36

def self.with_job_hash_context(job_hash, &block)
  with_context(job_hash_context(job_hash), &block)
end

Instance Method Details

#loggerObject



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

def logger
  Faktory::Logging.logger
end