1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Added logger.ap

This commit is contained in:
Tobias Crawley 2010-04-22 00:57:21 -04:00
parent 9166bab5d0
commit 6dc67a2cff
4 changed files with 78 additions and 0 deletions

View file

@ -144,6 +144,21 @@ Supported color names:
}
rails>
### Logger Convenience Method ###
awesome_print adds an ap method to the Logger and ActiveSupport::BufferedLogger classes,
allowing you to call:
logger.ap object
By default, this logs at the :debug level. You can override that globally with
:log_level => :info
in the custom defaults (see below), or you can override on a per call basis with
logger.ap object, :warn
### Setting Custom Defaults ###
You can set your own default options by creating ``.aprc`` file in your home
directory. Within that file assign your defaults to ``AwesomePrint.defaults``.

View file

@ -7,6 +7,8 @@ require File.dirname(__FILE__) + "/ap/core_ext/string"
require File.dirname(__FILE__) + "/ap/core_ext/kernel"
require File.dirname(__FILE__) + "/ap/awesome_print"
require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(::Logger) or defined?(::ActiveSupport::BufferedLogger)
require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(::ActiveRecord)
require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(::ActiveSupport)

18
lib/ap/core_ext/logger.rb Normal file
View file

@ -0,0 +1,18 @@
# Copyright (c) 2010 Michael Dvorkin
#
# Awesome Print is freely distributable under the terms of MIT license.
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
module AwesomePrintLogger
# Add ap method to logger
#------------------------------------------------------------------------------
def ap(object, level = nil)
level ||= AwesomePrint.defaults[:log_level] || :debug
send level, object.ai
end
end
Logger.send(:include, AwesomePrintLogger) if defined?(Logger)
ActiveSupport::BufferedLogger.send(:include, AwesomePrintLogger) if defined?(::ActiveSupport::BufferedLogger)

43
spec/logger_spec.rb Normal file
View file

@ -0,0 +1,43 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'logger'
require 'ap/core_ext/logger'
describe "AwesomePrint logging extensions" do
before(:all) do
@logger = Logger.new('/dev/null')
end
describe "ap method" do
it "should awesome_inspect the given object" do
object = mock
object.should_receive(:ai)
@logger.ap object
end
describe "the log level" do
before(:each) do
AwesomePrint.defaults = { }
end
it "should fallback to the default :debug log level" do
@logger.should_receive(:debug)
@logger.ap(nil)
end
it "should use the global user default if no level passed" do
AwesomePrint.defaults = { :log_level => :info }
@logger.should_receive(:info)
@logger.ap(nil)
end
it "should use the passed in level" do
@logger.should_receive(:warn)
@logger.ap(nil, :warn)
end
end
end
end