Sha256: 749de94c34ac56daace174dfb66d6d30df9ca0b06677a5df3372e7d0304b09f5

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

require 'colorize'

module Locomotive
  module Common

    class Logger

      attr_accessor :logger

      def initialize
        self.logger = nil
      end

      # Setup the single instance of the ruby logger.
      #
      # @param[ optional ] [ String ] path The path to the log file, full path with log file name
      # Sample /home/locomotivecms/log/server.log (default: nil => Stdout)
      #
      def setup(log_file_full_path = nil)
        require 'logger'

        output = begin
          if log_file_full_path
            log_file_path log_file_full_path
          else
            STDOUT
          end
        end

        self.logger = ::Logger.new(output).tap do |log|
          log.level = ::Logger::DEBUG
          log.formatter = proc do |severity, datetime, progname, msg|
            "#{msg}\n"
          end
        end
      end

      def self.instance
        @@instance ||= self.new
      end

      def self.setup(*args)
        if args.size > 1
          puts "[DEPRECATION] Logger.setup(path, stdout=false) is deprecated. " \
            "Please use Logger.setup(log_file_full_path) instead, " \
            "like: /home/locomotivecms/log/server.log"
        end
        self.instance.setup args.first
      end

      def self.close
        self.instance.logger.close
      end

      class << self
        %w(debug info warn error fatal unknown).each do |name|
          define_method(name) do |message|
            self.instance.logger.send(name.to_sym, message)
          end
        end
      end

      private

      def log_file_path(log_file_full_path)
        if File.directory? log_file_full_path
          puts "[DEPRECATION] Please use fully log file path like: /home/locomotivecms/log/server.log"
          File.expand_path(File.join(log_file_full_path, 'log', 'locomotivecms.log'))
        else
          log_file_full_path
        end.tap { |path| FileUtils.mkdir_p(File.dirname(path)) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
locomotivecms_common-0.0.3 lib/locomotive/common/logger.rb