Sha256: ac46ae05d0d391f094e801be83608a78c1473c809c4f6be63d16f9b832ea224b

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

#!/usr/bin/ruby1.8 -w
#
# Copyright:: Copyright 2009 Google Inc.
# Original Author:: Ryan Brown (mailto:ribrdb@google.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Replacement for the standard logger.rb which uses the Google App Engine
# logging API.

require 'appengine-apis/apiproxy'
require 'logger'

module AppEngine
  class Logger < ::Logger
    SEVERITIES = {
      DEBUG => ApiProxy::LogRecord::Level::debug,
      INFO => ApiProxy::LogRecord::Level::info,
      WARN => ApiProxy::LogRecord::Level::warn,
      ERROR => ApiProxy::LogRecord::Level::error,
      FATAL => ApiProxy::LogRecord::Level::fatal,
    }
    SEVERITIES.default = ApiProxy::LogRecord::Level::info
    
    def initialize(*args)
      super(STDERR)
    end
    
    def <<(msg)
      write_log(INFO, msg.to_s, "")
    end
    
    def add(severity, msg=nil, progname=nil, &block)
      severity ||= UNKNOWN
      return if severity < @level
      progname ||= @progname
      if msg.nil?
        if block_given?
          msg = yield
        else
          msg = progname
          progname = @progname
        end
      end
      write_log(severity, msg, progname)
    end
    alias log add

    private
    def write_log(severity, msg, progname)
      level = SEVERITIES[severity]
      if progname && !progname.empty? && progname != msg
        msg = "#{progname}: #{msg}"
      end
      ApiProxy.log(level, msg)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
appengine-apis-0.0.1 lib/appengine-apis/logger.rb