Sha256: 5d958bd569823db78deec957bf52d9d24a75a6eb66c0704561e254c88b1e11c5

Contents?: true

Size: 1019 Bytes

Versions: 5

Compression:

Stored size: 1019 Bytes

Contents

require "logger"

module S3log
  module Log
    extend self

    def set_logger(logfile, level="info")
      if logfile.is_a?(String) && !Dir.exists?(File.dirname(logfile))
        FileUtils.mkdir_p(File.dirname(logfile))
      end
      @logger = ::Logger.new(logfile)
      @logger.level = get_level_constant(level)
      @logger.formatter = proc do |severity, datetime, progname, msg|
        "[#{datetime.utc}] #{severity}: #{msg}\n"
      end
    end

    def logger
      @logger ||= ::Logger.new(STDOUT)
    end

    def debug(msg)
      logger.debug(msg)
    end

    def info(msg)
      logger.info(msg)
    end

    def warn(msg)
      logger.warn(msg)
    end

    def error(msg)
      logger.error(msg)
    end

    def fatal(msg)
      logger.fatal(msg)
    end

    def get_level_constant(level)
      if level
        begin
          ::Logger.const_get(level.to_s.upcase)
        rescue NameError
          return ::Logger::INFO
        end
      else
        ::Logger::INFO
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
s3log-0.0.5 lib/s3log/logger.rb
s3log-0.0.4 lib/s3log/logger.rb
s3log-0.0.3 lib/s3log/logger.rb
s3log-0.0.2 lib/s3log/logger.rb
s3log-0.0.1 lib/s3log/logger.rb