# frozen_string_literal: truemoduleSidekiqUniqueJobs# Utility module for reducing the number of uses of logger.## @author Mikael Henriksson <mikael@mhenrixon.com>moduleLoggingdefself.included(base)base.send(:extend,self)end## A convenience method for using the configured gem logger## @see SidekiqUniqueJobs#.logger## @return [Logger]#defloggerSidekiqUniqueJobs.loggerend## Logs a message at debug level## @param [String, Exception] message_or_exception the message or exception to log## @return [void]## @yield [String, Exception] the message or exception to use for log message#deflog_debug(message_or_exception=nil,item=nil,&block)message=build_message(message_or_exception,item)logger.debug(message,&block)nilend## Logs a message at info level## @param [String, Exception] message_or_exception the message or exception to log## @return [void]## @yield [String, Exception] the message or exception to use for log message#deflog_info(message_or_exception=nil,item=nil,&block)message=build_message(message_or_exception,item)logger.info(message,&block)nilend## Logs a message at warn level## @param [String, Exception] message_or_exception the message or exception to log## @return [void]## @yield [String, Exception] the message or exception to use for log message#deflog_warn(message_or_exception=nil,item=nil,&block)message=build_message(message_or_exception,item)logger.warn(message,&block)nilend## Logs a message at error level## @param [String, Exception] message_or_exception the message or exception to log## @return [void]## @yield [String, Exception] the message or exception to use for log message#deflog_error(message_or_exception=nil,item=nil,&block)message=build_message(message_or_exception,item)logger.error(message,&block)nilend## Logs a message at fatal level## @param [String, Exception] message_or_exception the message or exception to log## @return [void]## @yield [String, Exception] the message or exception to use for log message#deflog_fatal(message_or_exception=nil,item=nil,&block)message=build_message(message_or_exception,item)logger.fatal(message,&block)nilenddefbuild_message(message_or_exception,item=nil)returnnilifmessage_or_exception.nil?returnmessage_or_exceptionifitem.nil?message=message_or_exception.dupdetails=item.slice(LOCK,QUEUE,CLASS,JID,LOCK_DIGEST).each_with_object([])do|(key,value),memo|memo<<"#{key}=#{value}"endmessage<<" ("message<<details.join(" ")message<<")"messageend## Wraps the middleware logic with context aware logging### @return [void]## @yieldreturn [void] yield to the middleware instance#defwith_logging_contextwith_configured_loggers_contextdoreturnyieldendnil# Need to make sure we don't return anything hereend## Attempt to setup context aware logging for the given logger### @return [void]## @yield#defwith_configured_loggers_context(&block)logger_method.call(logging_context,&block)end## Setup some variables to add to each log line### @return [Hash] the context to use for each log line#deflogging_contextraiseNotImplementedError,"#{__method__} needs to be implemented in #{self.class}"endprivate## A memoized method to use for setting up a logging context### @return [proc] the method to call#deflogger_method@logger_method||=sidekiq_context_method@logger_method||=sidekiq_logger_context_method@logger_method||=sidekiq_logging_context_method@logger_method||=no_sidekiq_context_methodend## Checks if the logger respond to `with_context`.## @note only used to remove the need for explicitly ignoring manual dispatch in other places.### @return [true,false]#deflogger_respond_to_with_context?logger.respond_to?(:with_context)end## Checks if the logger context takes a hash argument## @note only used to remove the need for explicitly ignoring manual dispatch in other places.### @return [true,false]#deflogger_context_hash?defined?(Sidekiq::Context)||logger_respond_to_with_context?enddefsidekiq_context_methodSidekiq::Context.method(:with)ifdefined?(Sidekiq::Context)enddefsidekiq_logger_context_methodlogger.method(:with_context)iflogger_respond_to_with_context?enddefsidekiq_logging_context_methodSidekiq::Logging.method(:with_context)ifdefined?(Sidekiq::Logging)enddefno_sidekiq_context_methodmethod(:fake_logger_context)enddeffake_logger_context(_context)logger.warn"Don't know how to setup the logging context. Please open a feature request:"\" https://github.com/mhenrixon/sidekiq-unique-jobs/issues/new?template=feature_request.md"yieldendendend