module ContextualLogger::LoggerMixin

Public Instance Methods

add(arg_severity, arg1 = nil, arg2 = nil, **context) { || ... } click to toggle source

Note that this interface needs to stay compatible with the underlying ::Logger#add interface, which is: def add(severity, message = nil, progname = nil)

# File lib/contextual_logger.rb, line 105
def add(arg_severity, arg1 = nil, arg2 = nil, **context)   # Ruby will prefer to match hashes up to last ** argument
  severity = arg_severity || UNKNOWN
  if log_level_enabled?(severity)
    if arg1.nil?
      if block_given?
        message = yield
        progname = arg2 || context.delete(:progname) || @progname
      else
        message = arg2
        progname = @progname
      end
    else
      message = arg1
      progname = arg2 || @progname
    end
    write_entry_to_log(severity, Time.now, progname, message, context: current_context_for_thread.deep_merge(context))
  end

  true
end
current_context_for_thread() click to toggle source
# File lib/contextual_logger.rb, line 67
def current_context_for_thread
  Context::Handler.current_context
end
global_context=(context) click to toggle source
# File lib/contextual_logger.rb, line 48
def global_context=(context)
  Context::Handler.new(context).set!
end
log_level_enabled?(severity) click to toggle source
# File lib/contextual_logger.rb, line 99
def log_level_enabled?(severity)
  severity >= level
end
with_context(context) { || ... } click to toggle source
# File lib/contextual_logger.rb, line 52
def with_context(context)
  context_handler = Context::Handler.new(current_context_for_thread.deep_merge(context))
  context_handler.set!
  if block_given?
    begin
      yield
    ensure
      context_handler.reset!
    end
  else
    # If no block given, the context handler is returned to the caller so they can handle reset! themselves.
    context_handler
  end
end
write_entry_to_log(severity, timestamp, progname, message, context:) click to toggle source
# File lib/contextual_logger.rb, line 126
def write_entry_to_log(severity, timestamp, progname, message, context:)
  @logdev&.write(
    redactor.redact(
      format_message(format_severity(severity), timestamp, progname, message, context: context)
    )
  )
end

Private Instance Methods

basic_json_log_entry(severity, timestamp, normalized_progname, normalized_message, context:) click to toggle source
# File lib/contextual_logger.rb, line 150
def basic_json_log_entry(severity, timestamp, normalized_progname, normalized_message, context:)
  message_hash = {
    message: normalized_progname ? "#{normalized_progname}: #{normalized_message}" : normalized_message,
    severity:  severity,
    timestamp: timestamp
  }
  message_hash[:progname] = normalized_progname if normalized_progname

  # merge! is faster and OK here since message_hash is still local only to this method
  message_hash.merge!(context).to_json
end
format_message(severity, timestamp, progname, message, context: {}) click to toggle source
# File lib/contextual_logger.rb, line 140
def format_message(severity, timestamp, progname, message, context: {})
  normalized_message = ContextualLogger.normalize_message(message)
  normalized_progname = ContextualLogger.normalize_message(progname) unless progname.nil?
  if @formatter
    @formatter.call(severity, timestamp, normalized_progname, { message: normalized_message }.merge!(context))
  else
    "#{basic_json_log_entry(severity, timestamp, normalized_progname, normalized_message, context: context)}\n"
  end
end
redactor() click to toggle source
# File lib/contextual_logger.rb, line 136
def redactor
  @redactor ||= Redactor.new
end