Module: SidekiqUniqueJobs::Logging

Included in:
BatchDelete, SidekiqUniqueJobs::Lock::BaseLock, LockArgs, LockDigest, Locksmith, Middleware, OnConflict::Log, OnConflict::Reschedule, OnConflict::Strategy, Orphans::Observer, Orphans::Reaper, Redis::Entity, UpgradeLocks
Defined in:
lib/sidekiq_unique_jobs/logging.rb,
lib/sidekiq_unique_jobs/logging/middleware_context.rb

Overview

Provides the sidekiq middleware that makes the gem work

Author:

Defined Under Namespace

Modules: Middleware

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
# File 'lib/sidekiq_unique_jobs/logging.rb', line 8

def self.included(base)
  base.send(:extend, self)
end

Instance Method Details

#build_message(message_or_exception, item = nil) ⇒ String

Build a log message

Parameters:

  • message_or_exception (String, Exception)

    an entry to log

  • item (Hash) (defaults to: nil)

    the sidekiq job hash

Returns:

  • (String)

    a complete log entry



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sidekiq_unique_jobs/logging.rb', line 117

def build_message(message_or_exception, item = nil)
  return nil if message_or_exception.nil?
  return message_or_exception if item.nil?

  message = message_or_exception.dup
  details = item.slice(LOCK, QUEUE, CLASS, JID, LOCK_DIGEST).each_with_object([]) do |(key, value), memo|
    memo << "#{key}=#{value}"
  end
  message << " ("
  message << details.join(" ")
  message << ")"

  message
end

#log_debug(message_or_exception = nil, item = nil) {|String, Exception| ... } ⇒ void

This method returns an undefined value.

Logs a message at debug level

Parameters:

  • message_or_exception (String, Exception) (defaults to: nil)

    the message or exception to log

Yields:

  • (String, Exception)

    the message or exception to use for log message



32
33
34
35
36
37
38
# File 'lib/sidekiq_unique_jobs/logging.rb', line 32

def log_debug(message_or_exception = nil, item = nil, &block)
  return unless logging?

  message = build_message(message_or_exception, item)
  logger.debug(message, &block)
  nil
end

#log_error(message_or_exception = nil, item = nil) {|String, Exception| ... } ⇒ void

This method returns an undefined value.

Logs a message at error level

Parameters:

  • message_or_exception (String, Exception) (defaults to: nil)

    the message or exception to log

Yields:

  • (String, Exception)

    the message or exception to use for log message



83
84
85
86
87
88
89
# File 'lib/sidekiq_unique_jobs/logging.rb', line 83

def log_error(message_or_exception = nil, item = nil, &block)
  return unless logging?

  message = build_message(message_or_exception, item)
  logger.error(message, &block)
  nil
end

#log_fatal(message_or_exception = nil, item = nil) {|String, Exception| ... } ⇒ void

This method returns an undefined value.

Logs a message at fatal level

Parameters:

  • message_or_exception (String, Exception) (defaults to: nil)

    the message or exception to log

Yields:

  • (String, Exception)

    the message or exception to use for log message



100
101
102
103
104
105
106
107
# File 'lib/sidekiq_unique_jobs/logging.rb', line 100

def log_fatal(message_or_exception = nil, item = nil, &block)
  return unless logging?

  message = build_message(message_or_exception, item)
  logger.fatal(message, &block)

  nil
end

#log_info(message_or_exception = nil, item = nil) {|String, Exception| ... } ⇒ void

This method returns an undefined value.

Logs a message at info level

Parameters:

  • message_or_exception (String, Exception) (defaults to: nil)

    the message or exception to log

Yields:

  • (String, Exception)

    the message or exception to use for log message



49
50
51
52
53
54
55
# File 'lib/sidekiq_unique_jobs/logging.rb', line 49

def log_info(message_or_exception = nil, item = nil, &block)
  return unless logging?

  message = build_message(message_or_exception, item)
  logger.info(message, &block)
  nil
end

#log_warn(message_or_exception = nil, item = nil) {|String, Exception| ... } ⇒ void

This method returns an undefined value.

Logs a message at warn level

Parameters:

  • message_or_exception (String, Exception) (defaults to: nil)

    the message or exception to log

Yields:

  • (String, Exception)

    the message or exception to use for log message



66
67
68
69
70
71
72
# File 'lib/sidekiq_unique_jobs/logging.rb', line 66

def log_warn(message_or_exception = nil, item = nil, &block)
  return unless logging?

  message = build_message(message_or_exception, item)
  logger.warn(message, &block)
  nil
end

#loggerLogger

A convenience method for using the configured gem logger

Returns:

  • (Logger)

See Also:

  • SidekiqUniqueJobs#.logger


19
20
21
# File 'lib/sidekiq_unique_jobs/logging.rb', line 19

def logger
  SidekiqUniqueJobs.logger
end

#logging_contextHash

Setup some variables to add to each log line

Returns:

  • (Hash)

    the context to use for each log line

Raises:

  • (NotImplementedError)


166
167
168
# File 'lib/sidekiq_unique_jobs/logging.rb', line 166

def logging_context
  raise NotImplementedError, "#{__method__} needs to be implemented in #{self.class}"
end

#with_configured_loggers_context { ... } ⇒ void

This method returns an undefined value.

Attempt to setup context aware logging for the given logger

Yields:



156
157
158
# File 'lib/sidekiq_unique_jobs/logging.rb', line 156

def with_configured_loggers_context(&block)
  logger_method.call(logging_context, &block)
end

#with_logging_contextvoid

This method returns an undefined value.

Wraps the middleware logic with context aware logging

Yield Returns:

  • (void)

    yield to the middleware instance



140
141
142
143
144
145
146
# File 'lib/sidekiq_unique_jobs/logging.rb', line 140

def with_logging_context
  with_configured_loggers_context do
    return yield
  end

  nil # Need to make sure we don't return anything here
end