Sha256: abfac9544e91db16498e59f25ff83466d19a9a07f357dd42975b91bb93d46f2b

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require "logging"

module Bixby

  # A simple logging mixin
  module Log

    # Get a log instance for this class
    #
    # @return [Logger]
    def log
      @log ||= Logging.logger[self]
    end

    # Create a method for each log level. Allows receiver to simply call
    #
    #   warn "foo"
    %w{debug warn info error fatal}.each do |level|
      code = <<-EOF
      def #{level}(data=nil, &block)
        log.send(:#{level}, data, &block)
      end
      EOF
      eval(code)
    end

    # Setup logging
    #
    # @param [Symbol] level       Log level to use (default = :warn)
    # @param [String] pattern     Log pattern
    def self.setup_logger(level=nil, pattern=nil)
      # set level: ENV flag overrides; default to warn
      level = :debug if ENV["BIXBY_DEBUG"]
      level ||= :warn

      pattern ||= '%.1l, [%d] %5l -- %c: %m\n'

      # TODO always use stdout for now
      Logging.appenders.stdout(
        :level  => level,
        :layout => Logging.layouts.pattern(:pattern => pattern)
        )
      Logging::Logger.root.add_appenders(Logging.appenders.stdout)
      Logging::Logger.root.level = level
    end

  end # Log

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bixby-common-0.3.8 lib/bixby_common/util/log.rb