Sha256: 509b05752c9a01580eacdeb92f356fa996114a16d9c02c6619c3e0b173c4bb59

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

require 'binding_of_caller'
require 'fileutils'
require 'logger'

module Fasten
  class << self
    attr_accessor :logger
  end

  module Support
    module Logger
      attr_accessor :log_file, :logger

      %w[debug info error].each do |method|
        define_method "log_#{method}" do |msg|
          dest_logger = logger || Fasten.logger
          return unless dest_logger.respond_to?(method)

          caller_name = name if respond_to? :name
          caller_name ||= Kernel.binding.of_caller(1).eval('self').class
          dest_logger.send(method, caller_name) { msg }
        end
      end

      def initialize_logger(log_file: nil)
        if log_file
          self.log_file = log_file
        else
          self.log_file ||= "#{fasten_dir}/log/#{kind}/#{name}.log"
          FileUtils.mkdir_p File.dirname(self.log_file)
        end

        close_logger
        self.logger = ::Logger.new self.log_file, level: Fasten.logger.level, progname: Fasten.logger.progname
      end

      def close_logger
        logger.close if logger.is_a? ::Logger
      end

      def log_ini(object, message = nil)
        object.ini ||= Time.new
        log_info "Ini #{object.state} #{object.class} #{object} #{message}"
      end

      def log_fin(object, message = nil)
        object.fin ||= Time.new
        object.dif = object.fin - object.ini

        log_info "Fin #{object.state} #{object.class} #{object} #{message} in #{object.dif}"
      end
    end
  end
end

Fasten.logger ||=
  begin
    Logger.new $stdout, level: Logger::DEBUG, progname: $PROGRAM_NAME
  rescue StandardError
    nil
  end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fasten-0.18.0 lib/fasten/support/logger.rb
fasten-0.16.0 lib/fasten/support/logger.rb
fasten-0.14.4 lib/fasten/support/logger.rb