Sha256: b126c497bb6ea2ed5b86434c1a6653d3d7b15078df3f2da2842d7a2a8bb2d029

Contents?: true

Size: 1.8 KB

Versions: 46

Compression:

Stored size: 1.8 KB

Contents

module SiteHook
  module Loggers

    class Hook
      def initialize(base:, level:)
        @@levels  = {
            unknown: ::Logger::UNKNOWN,
            fatal:   ::Logger::FATAL,
            error:   ::Logger::ERROR,
            info:    ::Logger::INFO,
            debug:   ::Logger::DEBUG
        }
        @@loggers = {
            stdout: ::Logger.new(STDOUT, progname: base),
            stderr: ::Logger.new(STDERR, progname: base),
            file:   ::Logger.new(SiteHook::Paths.make_log_name(base, level), progname: base)
        }
        @@loggers.each do |_logger, obj|
          obj.datetime_format = '%Y-%m-%dT%H:%M:%S%Z'
          obj.formatter       = proc do |severity, datetime, progname, msg|
            "#{severity} [#{datetime}] #{progname} —— #{msg}\n"
          end
        end
      end

      def self.unknown(obj)
        @@loggers.each do |_key, value|
          value.unknown(obj)
        end
      end

      def self.error(obj)
        @@loggers.each do |_key, value|
          value.error(obj)
        end
      end

      def self.info(obj)
        @@loggers.each do |_key, value|
          next if key == :stderr
          value.info(obj)
        end
      end

      def self.fatal(obj)
        @@loggers.each do |key, value|
          next if key == :stderr
          value.fatal(obj)
        end
      end

      def self.warn(obj)
        @@loggers.each do |_key, value|
          value.warn(obj)
        end
      end

      # @param [Symbol] level log level to log at
      # @param [Object] obj some kind of object or msg to log
      def self.log(level, obj)
        @@loggers.each do |logger|
          logger.add(@levels[level], obj)
        end
      end

      def self.log_raw(msg)
        @@loggers.each do |logger|
          logger.<<(obj)
        end

      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
site_hook-1.0.30 lib/site_hook/loggers/hook.rb
site_hook-1.0.29 lib/site_hook/loggers/hook.rb
site_hook-1.0.28 lib/site_hook/loggers/hook.rb
site_hook-1.0.27 lib/site_hook/loggers/hook.rb
site_hook-1.0.26 lib/site_hook/loggers/hook.rb
site_hook-1.0.25 lib/site_hook/loggers/hook.rb
site_hook-1.0.24 lib/site_hook/loggers/hook.rb
site_hook-1.0.23 lib/site_hook/loggers/hook.rb
site_hook-1.0.22 lib/site_hook/loggers/hook.rb
site_hook-1.0.21 lib/site_hook/loggers/hook.rb
site_hook-1.0.20 lib/site_hook/loggers/hook.rb
site_hook-1.0.19 lib/site_hook/loggers/hook.rb
site_hook-1.0.18 lib/site_hook/loggers/hook.rb
site_hook-1.0.17 lib/site_hook/loggers/hook.rb
site_hook-1.0.16 lib/site_hook/loggers/hook.rb
site_hook-1.0.15 lib/site_hook/loggers/hook.rb
site_hook-1.0.14 lib/site_hook/loggers/hook.rb
site_hook-1.0.13 lib/site_hook/loggers/hook.rb
site_hook-1.0.12 lib/site_hook/loggers/hook.rb
site_hook-1.0.11 lib/site_hook/loggers/hook.rb