lib/sawmill/logger.rb in sawmill-0.0.8 vs lib/sawmill/logger.rb in sawmill-0.0.9

- old
+ new

@@ -52,11 +52,11 @@ # Create a new logger. # # Supported options include: # - # <tt>:levels</tt>:: + # <tt>:level_group</tt>:: # Use a custom Sawmill::LevelGroup. Normally, you should leave this # set to the default, which is Sawmill::STANDARD_LEVELS. # <tt>:level</tt>:: # Default level to use for log messages when no level is explicitly # provided. By default, this is set to the level group's default, @@ -79,16 +79,16 @@ # <tt>:processor</tt>:: # A processor for log entries generated by this logger. # If not specified, log entries are written out to STDOUT. def initialize(opts_={}) - @levels = opts_[:levels] || STANDARD_LEVELS - @level = @levels.get(opts_[:level]) + @level_group = opts_[:level_group] || opts_[:levels] || STANDARD_LEVELS + @level = @level_group.get(opts_[:level]) if opts_.include?(:attribute_level) - @attribute_level = @levels.get(opts_[:attribute_level]) + @attribute_level = @level_group.get(opts_[:attribute_level]) else - @attribute_level = @levels.highest + @attribute_level = @level_group.highest end @progname = opts_[:progname] || 'sawmill' @record_progname = opts_[:record_progname] @record_id_generator = opts_[:record_id_generator] || Logger._get_default_record_id_generator @processor = opts_[:processor] || Formatter.new(::STDOUT) @@ -98,11 +98,11 @@ # Emit a log message. This method has the same behavior as the # corresponding method in ruby's logger class. def add(level_, message_=nil, progname_=nil, &block_) - level_obj_ = @levels.get(level_) + level_obj_ = @level_group.get(level_) if level_obj_.nil? raise Errors::UnknownLevelError, level_ end return true if level_obj_ < @level progname_ ||= @progname @@ -142,11 +142,11 @@ # method in ruby's logger class, which dumps the given string to the log # device without any formatting. Normally, you would not use this method # because it bypasses the log formatting and parsing capability. def <<(message_) - add(@levels.default, message_) + add(@level_group.default, message_) end # Emits a begin_record log entry. This begins a new log record. # @@ -160,11 +160,11 @@ # end_record is implicitly called in this case. def begin_record(id_=nil) end_record if @current_record_id @current_record_id = (id_ || @record_id_generator.call).to_s - @processor.begin_record(Entry::BeginRecord.new(@levels.highest, ::Time.now, @record_progname || @progname, @current_record_id)) + @processor.begin_record(Entry::BeginRecord.new(@level_group.highest, ::Time.now, @record_progname || @progname, @current_record_id)) @current_record_id end # Returns the record ID for the currently open log record, or nil if @@ -179,11 +179,11 @@ # a record is currently open. Returns the record ID of the ended log # record if one was open, or nil if no log record was open. def end_record if @current_record_id - @processor.end_record(Entry::EndRecord.new(@levels.highest, ::Time.now, @record_progname || @progname, @current_record_id)) + @processor.end_record(Entry::EndRecord.new(@level_group.highest, ::Time.now, @record_progname || @progname, @current_record_id)) id_ = @current_record_id @current_record_id = nil id_ else nil @@ -202,11 +202,11 @@ def attribute(key_, value_, operation_=nil, level_=true, progname_=nil) if level_ == true level_obj_ = @attribute_level else - level_obj_ = @levels.get(level_) + level_obj_ = @level_group.get(level_) if level_obj_.nil? raise Errors::UnknownLevelError, level_ end end return true if level_obj_ < @level @@ -268,11 +268,11 @@ def level=(value_) if value_.kind_of?(Level) @level = value_ else - level_obj_ = @levels.get(value_) + level_obj_ = @level_group.get(value_) if level_obj_.nil? raise Errors::UnknownLevelError, value_ end @level = level_obj_ end @@ -280,10 +280,18 @@ alias_method :sev_threshold=, :level= alias_method :sev_threshold, :level + # Get the LevelGroup in use by this Logger. This setting cannot be + # changed once the logger is constructed. + + def level_group + @level_group + end + + # Provide a block that generates and returns a unique record ID string. # This block will be called when begin_record is called without an # explicit ID provided. If you do not provide a block, Sawmill will use # a default generator which uses the variant 4 (random) UUID standard. @@ -327,11 +335,11 @@ def method_missing(method_, *args_, &block_) method_name_ = method_.to_s question_ = method_name_[-1..-1] == '?' method_name_ = method_name_[0..-2] if question_ - level_ = @levels.lookup_method(method_name_) + level_ = @level_group.lookup_method(method_name_) return super(method_, *args_, &block_) unless level_ if question_ level_ >= @level else add(level_, nil, args_[0], &block_) @@ -340,23 +348,23 @@ def self._get_default_record_id_generator # :nodoc: unless @_default_generator if defined?(::SecureRandom) - def self._random_hex32 + _random_hex32 = ::Proc.new do ::SecureRandom.hex(16) end elsif defined?(::ActiveSupport::SecureRandom) - def self._random_hex32 + _random_hex32 = ::Proc.new do ::ActiveSupport::SecureRandom.hex(16) end else - def self._random_hex32 + _random_hex32 = ::Proc.new do ::Kernel.rand(0x100000000000000000000000000000000).to_s(16).rjust(32, '0') end end @_default_generator = ::Proc.new do - uuid_ = _random_hex32 + uuid_ = _random_hex32.call uuid_[12] = '4' uuid_[16] = (uuid_[16,1].to_i(16)&3|8).to_s(16) uuid_.insert(8, '-') uuid_.insert(13, '-') uuid_.insert(18, '-')