lib/lager.rb in lager-0.2.0.3 vs lib/lager.rb in lager-0.2.0.4
- old
+ new
@@ -6,20 +6,23 @@
# extend Lager
# log_to '/tmp/foo.log'
# ...
# end
#
-# It provides the class instance variable @lager, which may be used in class methods
+# It provides the class instance variable @lager, which may be used in class
+# methods, e.g.
#
-# e.g. @lager.debug { "example log message" }
+# def self.foo
+# @lager.debug { "example log message" }
+# end
#
# Note that using the block invocation means that the block contents are
# not evaluated if the log level is above the message level.
#
# Make sure to call log_to within the class definition, so that class methods
-# will already have @lager defined.
-
+# will already have @lager defined at requiretime.
+#
# For instance methods, you need to set @lager directly, within initialize
# Note: the instance layer and class layer each have their own independent
# @lager
# Here we will make the instance @lager reference the class @lager
#
@@ -38,14 +41,19 @@
vpath = File.join(File.dirname(__FILE__), '..', 'VERSION')
File.read(vpath).chomp
end
# create @lager
+ # if passed a Logger instance, set @lager to that instance and return
# supports IO and String (filename, presumably) for log destination
# (passed straight through to Logger.new)
# supports symbols, strings, and integers for log level
#
def log_to(dest = $stderr, level = nil)
+ if dest.is_a?(Logger)
+ @lager = dest
+ return
+ end
# use the old @lager's level by default, as appropriate
level ||= (defined?(@lager) ? @lager.level : :warn)
@lager = Logger.new dest
@lager.formatter = proc { |sev, time, progname, msg|
line = "[#{time.strftime('%Y-%m-%d %H:%M:%S')}] #{sev.to_s.upcase}: "