Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Global logger #18

Closed
wants to merge 2 commits into from

4 participants

tpaktop Coveralls Rudolf Schmidt Solomon White
tpaktop

Added ability to register a global logger.
So that shared logger can be used across multiple classes which include Loggeable
Convention based - to register global logger use the same interface as named one but specify :global as name

Coveralls

Coverage Status

Coverage remained the same when pulling e73a68f on zombees:global-logger into 9212b5e on rudionrails:master.

Coveralls

Coverage Status

Coverage remained the same when pulling e73a68f on zombees:global-logger into 9212b5e on rudionrails:master.

Rudolf Schmidt
Owner

You can already do this without having to implement a :global logger.

# Define logger
Yell.new :stdout, name: Object

# Define new class with loggable module
class Foo
  include Yell::Loggable
end

# Access logger
Foo.logger.info "Hello World"

Since the above only defines Foo to have an accessable logger, you may even do something like this (though I wouldnot recommend it)

Yell.new :stdout, name: Object
Object.send :include, Yell::Loggable

logger.info "Hello from anywhere"
tpaktop

Ah right, maybe it's worth to add a comment to documentation. It's really not obvious.

Rudolf Schmidt
Owner

Yes I agree. I have some wiki pages about that (me think). We should definitely update those and add some lines to the README.md.

tpaktop
Rudolf Schmidt
Owner

Great thank you!

Rudolf Schmidt
Owner

I've added some documentation in the README.md about defining a global logger. Would that be an explanation fitting your needs? It would be great to receive feedback since I am looking at the problem from a different angle.

tpaktop
Rudolf Schmidt
Owner

Hey no sweat :-) If you could provide me with an example on how a DSL could look for your case, that would be great.

tpaktop

Ok so I mean replace

# Enable logging for the class that (almost) every Ruby class inherits from
Object.send :include, Yell::Loggable in README

With

class Foo
  Include Yell::Loggable
end
class Bar
  include Yell::Loggable
end
Foo.logger.info
Bar.new.logger.info

Sorry for not making a proper pull request it's a bit hard from mobile

Rudolf Schmidt rudionrails closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 30, 2013
  1. Solomon White

    add a global logger

    rubysolo authored
Commits on May 31, 2013
  1. tpaktop
This page is out of date. Refresh to see the latest.
3  README.md
View
@@ -117,6 +117,9 @@ you are good to go.
# Before you can use it, you will need to define a logger and
# provide it with the `:name` of your class.
Yell.new :stdout, :name => 'Foo'
+# As alternative you can register a global logger
+# which is gonna be used if no named logger for a class found
+Yell.new :stdout, :name => :global
class Foo
include Yell::Loggable
2  lib/yell/repository.rb
View
@@ -38,7 +38,7 @@ def []=( name, logger )
# @raise [Yell::LoggerNotFound] Raised when repository does not have that key
# @return [Yell::Logger] The logger instance
def []( name )
- synchronize { instance.fetch(name) or raise Yell::LoggerNotFound.new(name) }
+ synchronize { instance.fetch(name) or instance.fetch(:global) or raise Yell::LoggerNotFound.new(name) }
end
# Get the list of all loggers in the repository
14 spec/yell/repository_spec.rb
View
@@ -18,6 +18,20 @@
subject.should == logger
end
+ context "with a global logger" do
+ before do
+ @logger = Yell.new :stdout, :name => :global
+ end
+
+ it "should not raise with any name supplied" do
+ lambda { Yell::Repository[ String ] }.should_not raise_error
+ end
+
+ it "should return the global logger" do
+ Yell::Repository[ Numeric ].should == @logger
+ end
+ end
+
context "given a Class" do
before do
@logger = Yell.new :stdout, :name => "Numeric"
Something went wrong with that request. Please try again.