Sha256: 7cd70ad53683e646949d598e4de738f081346f9fafb321bca743c131ef60bc2a

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require "logger"
require "securerandom"
require "stringio"

require "memolog/version"
require "memolog/config"
require "memolog/formatter"
require "memolog/init"
require "memolog/logger_extension"
require "memolog/rails_middleware"
require "memolog/sentry_extension"
require "memolog/sidekiq_middleware"

module Memolog
  extend self

  attr_accessor :config

  @config = Memolog::Config.new

  def configure
    yield(config) if block_given?
  end

  def init_middlewares!
    Memolog::Init.init_middlewares!
  end

  def extend_logger(other_logger)
    other_logger.extend(Memolog::LoggerExtension)
    other_logger.formatter = config.formatter
  end

  def uuid
    Thread.current[:memolog_uuid]
  end

  def logger
    Thread.current[:memolog_logger] ||= Logger.new(nil, formatter: config.formatter)
  end

  def logdevs
    Thread.current[:memolog_logdevs] ||= []
  end

  def run
    Thread.current[:memolog_uuid] = config.uuid_callable.call

    logdevs.push(StringIO.new)
    logger.instance_variable_set(:@logdev, logdevs.last)

    yield
  ensure
    logdevs.pop unless config.debug
  end

  def dump
    return if logdevs.empty?

    beginning = logdevs.last.string.length - config.log_size_limit
    beginning = 0 if beginning.negative?

    logdevs.last.string.slice(beginning, config.log_size_limit).presence
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
memolog-0.3.5 lib/memolog.rb
memolog-0.3.4 lib/memolog.rb