Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Loggers every which way #7

Closed
wants to merge 3 commits into from

2 participants

@rauchy

Just my take on Yell::Loggable.

@rudionrails
Owner

Hi,

I am not so very fond of implicit stuff that gets added to any arbitrary class, actually. Especially not overloading the Kernel class :-S

You can already achieve what you are suggesting by doing the following:

# include loggable into object
class Object
  include Yell::Loggable

end

# define the logger
Yell.new :stdout, :name => 'Object'

# define any class and log away :-)
class Foo; end

Foo.new.logger.info :info

I still prefer that the user makes an own choice when adding a logger method to any class. It is, of course, a bit more code, but people understand what happend when reading it - that's my guess ;-)

What do you think?

@rauchy

Yeah, I guess that makes sense :)

@rauchy rauchy closed this
@rudionrails
Owner

I did not mean to put your idea down. I've been experimenting to have something like this:

Yell.new :stdout, :name => :root

Only thing is: I rather not magically have the Object class get a logger method. Also, doing something like a root logger would, again that's my guess, have some people assuming that there always is one. Yell has many different adapters (stdout, stderr, file-based, graylog2, syslog) and which would the default be? Also, I always like to have a real use-case for this as I do not want features added that people may never use.

If people want things like a root logger, I am of course going to accept a pull :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 40 additions and 5 deletions.
  1. +12 −1 README.md
  2. +18 −4 lib/yell/logger.rb
  3. +10 −0 spec/yell/logger_spec.rb
View
13 README.md
@@ -109,7 +109,18 @@ you global namespace with Yell's subclasses.
### You want any class to have a logger?
-Yell comes with a simple module: +Yell::Loggable+. Simply include this in a class and
+Yell provides a general logger that can be accessed *everywhere*. Just
+use the :everywhere option when initializing it:
+
+```ruby
+Yell.new :stdout, :everywhere => true
+
+some_instance = SomeClass.new
+some_instance.logger.info "Hello World"
+```
+
+If you prefer to be more picky and have it only in some objects instead
+of everywhere, Yell comes with a simple module: +Yell::Loggable+. Simply include this in a class and
you are good to go.
```ruby
View
22 lib/yell/logger.rb
@@ -51,8 +51,13 @@ def initialize( *args, &block )
# set the log level when given
self.level = @options[:level]
- # set the loggeer's name
- self.name = @options[:name] if @options[:name]
+ # include this logger to any object if 'everywhere' is defined
+ if !!@options[:everywhere]
+ include_everywhere!
+ elsif @options[:name]
+ # set the logger's name
+ self.name = @options[:name]
+ end
# extract adapter
self.adapter args.pop if args.any?
@@ -82,9 +87,9 @@ def initialize( *args, &block )
# adapter( Yell::Adapter::File.new )
#
# @param [Symbol] type The type of the adapter, may be `:file` or `:datefile` (default `:file`)
- #
# @return [Yell::Adapter] The instance
#
+ #
# @raise [Yell::NoSuchAdapter] Will be thrown when the adapter is not defined
def adapter( type = :file, *args, &block )
options = [@options, *args].inject( Hash.new ) do |h, c|
@@ -111,8 +116,8 @@ def level( val = nil )
@level = Yell::Level.new( val )
end
- end
+ end
# Convenience method for resetting all adapters of the Logger.
def close
@adapters.each(&:close)
@@ -178,6 +183,15 @@ def write( event )
@adapters.each { |a| a.write(event) }
end
+ def include_everywhere!
+ self.name = 'General'
+
+ Kernel.module_eval do
+ def logger
+ Yell['General']
+ end
+ end
+ end
end
end
View
10 spec/yell/logger_spec.rb
@@ -57,6 +57,16 @@ def bar
end
end
+ context "initialize with :everywhere" do
+ let(:object) { Object.new }
+ it "should be available from any object" do
+ logger = Yell.new :everywhere => true
+
+ object.should respond_to(:logger)
+ object.logger.should == logger
+ end
+ end
+
context "initialize with a :filename" do
it "should call adapter with :file" do
mock.proxy( Yell::Adapters::File ).new( :filename => 'test.log' )
Something went wrong with that request. Please try again.