Permalink
Browse files

Adds logging. Closes #583

  • Loading branch information...
1 parent d0d751a commit 1a16151ab681d2ffccd8056101007cec035b70e0 Michael Bleigh committed Apr 12, 2012
Showing with 55 additions and 1 deletion.
  1. +10 −0 README.md
  2. +13 −1 lib/omniauth.rb
  3. +23 −0 lib/omniauth/strategy.rb
  4. +7 −0 spec/omniauth_spec.rb
  5. +2 −0 spec/spec_helper.rb
View
@@ -120,6 +120,16 @@ environment information on the callback request. It is entirely up to
you how you want to implement the particulars of your application's
authentication flow.
+## Logging
+
+OmniAuth supports a configurable logger. By default, OmniAuth will log
+to `STDOUT` but you can configure this using `OmniAuth.config.logger`:
+
+```ruby
+# Rails application example
+OmniAuth.config.logger = Rails.logger
+```
+
## <a name="resources"></a>Resources
The [OmniAuth Wiki](https://github.com/intridea/omniauth/wiki) has
View
@@ -1,5 +1,6 @@
require 'rack'
require 'singleton'
+require 'logger'
module OmniAuth
class Error < StandardError; end
@@ -22,12 +23,19 @@ def self.strategies
class Configuration
include Singleton
+ def self.default_logger
+ logger = Logger.new(STDOUT)
+ logger.progname = "omniauth"
+ logger
+ end
+
@@defaults = {
:camelizations => {},
:path_prefix => '/auth',
:on_failure => OmniAuth::FailureEndpoint,
:form_css => Form::DEFAULT_CSS,
:test_mode => false,
+ :logger => default_logger,
:allowed_request_methods => [:get, :post],
:mock_auth => {
:default => AuthHash.new(
@@ -88,7 +96,7 @@ def add_camelization(name, camelized)
end
attr_writer :on_failure
- attr_accessor :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations
+ attr_accessor :path_prefix, :allowed_request_methods, :form_css, :test_mode, :mock_auth, :full_host, :camelizations, :logger
end
def self.config
@@ -99,6 +107,10 @@ def self.configure
yield config
end
+ def self.logger
+ config.logger
+ end
+
def self.mock_auth_for(provider)
config.mock_auth[provider.to_sym] || config.mock_auth[:default]
end
@@ -142,6 +142,15 @@ def inspect
"#<#{self.class.to_s}>"
end
+ # Direct access to the OmniAuth logger, automatically prefixed
+ # with this strategy's name.
+ #
+ # @example
+ # log :warn, "This is a warning."
+ def log(level, message)
+ OmniAuth.logger.send(level, "(#{name}) #{message}")
+ end
+
# Duplicates this instance and runs #call! on it.
# @param [Hash] The Rack environment.
def call(env)
@@ -178,12 +187,16 @@ def options_call
def request_call
setup_phase
+ log :info, "Request phase initiated."
+
#store query params from the request url, extracted in the callback_phase
session['omniauth.params'] = request.params
if options.form.respond_to?(:call)
+ log :info, "Rendering form from supplied Rack endpoint."
options.form.call(env)
elsif options.form
+ log :info, "Rendering form from underlying application."
call_app!
else
if request.params['origin']
@@ -198,6 +211,8 @@ def request_call
# Performs the steps necessary to run the callback phase of a strategy.
def callback_call
setup_phase
+
+ log :info, "Callback phase initiated."
@env['omniauth.origin'] = session.delete('omniauth.origin')
@env['omniauth.origin'] = nil if env['omniauth.origin'] == ''
@env['omniauth.params'] = session.delete('omniauth.params') || {}
@@ -266,8 +281,10 @@ def mock_callback_call
# underlying application. This will default to `/auth/:provider/setup`.
def setup_phase
if options[:setup].respond_to?(:call)
+ log :info, "Setup endpoint detected, running now."
options[:setup].call(env)
elsif options.setup?
+ log :info, "Calling through to underlying application for setup."
setup_env = env.merge('PATH_INFO' => setup_path, 'REQUEST_METHOD' => 'GET')
call_app!(setup_env)
end
@@ -413,6 +430,12 @@ def fail!(message_key, exception = nil)
self.env['omniauth.error.type'] = message_key.to_sym
self.env['omniauth.error.strategy'] = self
+ if exception
+ log :error, "Authentication failure! #{message_key}: #{exception.class.to_s}, #{exception.message}"
+ else
+ log :error, "Authentication failure! #{message_key} encountered."
+ end
+
OmniAuth.config.on_failure.call(self.env)
end
@@ -76,6 +76,13 @@
end
end
+ describe '.logger' do
+ it 'should call through to the configured logger' do
+ OmniAuth.stub(:config => mock(:logger => "foo"))
+ OmniAuth.logger.should == "foo"
+ end
+ end
+
describe '::Utils' do
describe '.deep_merge' do
it 'should combine hashes' do
View
@@ -8,6 +8,8 @@
require 'omniauth'
require 'omniauth/test'
+OmniAuth.config.logger = Logger.new("/dev/null")
+
RSpec.configure do |config|
config.include Rack::Test::Methods
config.extend OmniAuth::Test::StrategyMacros, :type => :strategy

0 comments on commit 1a16151

Please sign in to comment.