lib/tee_logger.rb in tee_logger-2.5.0 vs lib/tee_logger.rb in tee_logger-2.6.0
- old
+ new
@@ -16,43 +16,50 @@
# main
# @see http://www.rubydoc.info/stdlib/logger/Logger Logger
class TeeLogger
class << self
+ # @!macro [attach] logging_methods
+ # @!method $1(progname = nil, disabling_target = nil, &block)
+ # logging $1 level message.
+ # @param progname see also Logger
+ # @param disabling_target (Symbol) valid values => [:console, :logfile]
+ # @param &block see also Logger
+ # @return true
+ # @see Logger
+ def define_logging_methods(name)
+ define_method(name) do |progname = nil, disabling_target = nil, &block|
+ if disabling_target
+ disable(disabling_target) { logging(name, progname, &block) }
+ else
+ logging(name, progname, &block)
+ end
+ end
+ end
+ private :define_logging_methods
+
# @!macro [attach] loglevel_check_methods
# @!method $1(name)
# @return [Boolean]
def define_loglevel_check_methods(name)
define_method(name) do
- @logfile.send(name)
@console.send(name)
+ @logfile.send(name)
end
end
private :define_loglevel_check_methods
-
- # @!macro [attach] logging_methods
- # @!method $1(progname = nil, &block)
- # logging $1 level message.
- # @return true
- def define_logging_methods(name)
- define_method(name) do |progname = nil, &block|
- @logfile.send(name, progname, &block)
- @console.send(name, progname, &block)
- end
- end
- private :define_logging_methods
end
attr_reader :level, :progname, :formatter
# @param logdev [String]
# @param shift_age [Integer]
# @param shift_size [Integer]
# @see Logger#initialize
def initialize(logdev = DEFAULT_FILE, shift_age = 0, shift_size = 1_048_576)
- @logfile = Logger.new(logdev, shift_age, shift_size)
@console = Logger.new($stdout)
+ @logfile = Logger.new(logdev, shift_age, shift_size)
end
define_logging_methods :debug
define_logging_methods :info
define_logging_methods :warn
@@ -65,50 +72,49 @@
define_loglevel_check_methods :error?
define_loglevel_check_methods :fatal?
# @param level [Integer]
def level=(level)
- @logfile.level = level
- @console.level = level
- @level = level
+ @console.level = @logfile.level = @level = level
end
alias_method :sev_threshold, :level
alias_method :sev_threshold=, :level=
# @param name [String, Symbol]
def progname=(name = nil)
- @logfile.progname = name
- @console.progname = name
- @progname = name
+ @console.progname = @logfile.progname = @progname = name
end
# @param formatter
def formatter=(formatter)
- @logfile.formatter = formatter
- @console.formatter = formatter
- @formatter = formatter
+ @console.formatter = @logfile.formatter = @formatter = formatter
end
- # @param target [String, Symbol]
- # @yield before target disable, after target enable.
- def disable(target)
+ # @param logdev_name [String, Symbol]
+ # @yield before logdev_name disable, after logdev_name enable.
+ def disable(logdev_name)
if block_given?
- disable(target)
+ disable(logdev_name)
yield
- enable(target)
+ enable(logdev_name)
else
- target_instance(target).formatter = proc { |_, _, _, _| '' }
+ logdev_instance(logdev_name).formatter = proc { |_, _, _, _| '' }
end
end
- # @param target [String, Symbol]
- def enable(target)
- target_instance(target).formatter = @formatter
+ # @param logdev_name [String, Symbol]
+ def enable(logdev_name)
+ logdev_instance(logdev_name).formatter = @formatter
end
private
- def target_instance(target)
- instance_variable_get("@#{target}")
+ def logging(name, progname, &block)
+ @console.send(name, progname, &block)
+ @logfile.send(name, progname, &block)
+ end
+
+ def logdev_instance(logdev_name)
+ instance_variable_get("@#{logdev_name}")
end
end
end