Sha256: a11699d5ad97b8e516c8a06c005a94f4fdd162d86f6b364074fc9782ed99f83a

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

# vim:fileencoding=utf-8

require 'mono_logger'

module ResqueAdmin
  module Scheduler
    # Just builds a logger, with specified verbosity and destination.
    # The simplest example:
    #
    #   ResqueAdmin::Scheduler::LoggerBuilder.new.build
    class LoggerBuilder
      # Initializes new instance of the builder
      #
      # Pass :opts Hash with
      #   - :quiet if logger needs to be silent for all levels. Default - false
      #   - :verbose if there is a need in debug messages. Default - false
      #   - :log_dev to output logs into a desired file. Default - STDOUT
      #   - :format log format, either 'text' or 'json'. Default - 'text'
      #
      # Example:
      #
      #   LoggerBuilder.new(
      #     :quiet => false, :verbose => true, :log_dev => 'log/scheduler_admin.log'
      #   )
      def initialize(opts = {})
        @quiet = !!opts[:quiet]
        @verbose = !!opts[:verbose]
        @log_dev = opts[:log_dev] || $stdout
        @format = opts[:format] || 'text'
      end

      # Returns an instance of MonoLogger
      def build
        logger = MonoLogger.new(@log_dev)
        logger.level = level
        logger.formatter = send(:"#{@format}_formatter")
        logger
      end

      private

      def level
        if @verbose && !@quiet
          MonoLogger::DEBUG
        elsif !@quiet
          MonoLogger::INFO
        else
          MonoLogger::FATAL
        end
      end

      def text_formatter
        proc do |severity, datetime, _progname, msg|
          "resque_admin-scheduler_admin: [#{severity}] #{datetime.iso8601}: #{msg}\n"
        end
      end

      def json_formatter
        proc do |severity, datetime, progname, msg|
          require 'json'
          JSON.dump(
            name: 'resque_admin-scheduler_admin',
            progname: progname,
            level: severity,
            timestamp: datetime.iso8601,
            msg: msg
          ) + "\n"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
resque-admin-scheduler-1.3.0 lib/resque/scheduler_admin/logger_builder.rb
resque-admin-scheduler-4.3.0 lib/resque/scheduler_admin/logger_builder.rb