Sha256: f70761f936ed17c1179f827d1b0809e5b3c8d85244239cec1492499a8f31a895

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'log4r'

# Open up Log4r and add a simple log accessor for 'logfile'
#
# @example allows simple setup
#
#   include Autogui::Logging
#
#   logger.filename = 'log/autogui.log'
#   logger.warn "warning message goes to log file"
#
module Log4r
  class Logger

    # @return [String] filename of the logfile
    def logfile
      @filename
    end

    def logfile=(fn)
      FileOutputter.new(:logfile, :filename => fn, :trunc => true)
      Outputter[:logfile].formatter = Log4r::PatternFormatter.new(:pattern => "[%5l %d] %M [%t]")
      add(:logfile)
    end

  end
end

module Autogui

  STANDARD_LOGGER = 'standard'

  # wrapper for Log4r gem
  module Logging

    # Logging mixin allows simple logging setup
    # to STDOUT and optionally, to one filename.  Logger is a wrapper
    # for Log4r::Logger it accepts any methods that 
    # Log4r::Logger accepts in addition to the "logfile" filename.
    #
    # @example  simple logging to file setup
    #
    #   include Autogui::Logging
    #
    #   logger.filename = 'log/autogui.log'
    #   logger.warn "warning message goes to 'log/autogui.log'" 
    #
    #   logger.level = Log4r::DEBUG
    #   logger.debug "this message goes to 'log/autogui.log'"
    #
    def logger
      init_logger if Log4r::Logger[STANDARD_LOGGER].nil?
      Log4r::Logger[STANDARD_LOGGER]
    end


    protected
    
    # Initialize the logger, defaults to log4r::Warn
    def init_logger
      log = Log4r::Logger.new(STANDARD_LOGGER)
      Log4r::Logger[STANDARD_LOGGER].level = Log4r::WARN
      Log4r::Logger[STANDARD_LOGGER].trace = true

      Log4r::StderrOutputter.new :console
      Log4r::Outputter[:console].formatter = Log4r::PatternFormatter.new(:pattern => "[%l] %m")
      log.add(:console)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
win32-autogui-0.3.0 lib/win32/autogui/logging.rb