lib/lumberjack/logger.rb in lumberjack-1.2.5 vs lib/lumberjack/logger.rb in lumberjack-1.2.6

- old
+ new

@@ -154,14 +154,14 @@ # logger.add_entry(Logger::ERROR, exception) # logger.add_entry(Logger::INFO, "Request completed") # logger.add_entry(:warn, "Request took a long time") # logger.add_entry(Logger::DEBUG){"Start processing with options #{options.inspect}"} def add_entry(severity, message, progname = nil, tags = nil) - begin + begin severity = Severity.label_to_level(severity) unless severity.is_a?(Integer) return true unless device && severity && severity >= level - + return true if Thread.current[:lumberjack_logging] Thread.current[:lumberjack_logging] = true time = Time.now message = message.call if message.is_a?(Proc) @@ -235,11 +235,11 @@ # Return +true+ if +FATAL+ messages are being logged. def fatal? level <= FATAL end - + # Set the log level to fatal. def fatal! self.level = FATAL end @@ -250,11 +250,11 @@ # Return +true+ if +ERROR+ messages are being logged. def error? level <= ERROR end - + # Set the log level to error. def error! self.level = ERROR end @@ -265,11 +265,11 @@ # Return +true+ if +WARN+ messages are being logged. def warn? level <= WARN end - + # Set the log level to warn. def warn! self.level = WARN end @@ -280,11 +280,11 @@ # Return +true+ if +INFO+ messages are being logged. def info? level <= INFO end - + # Set the log level to info. def info! self.level = INFO end @@ -295,11 +295,11 @@ # Return +true+ if +DEBUG+ messages are being logged. def debug? level <= DEBUG end - + # Set the log level to debug. def debug! self.level = DEBUG end @@ -347,22 +347,40 @@ def progname thread_local_value(:lumberjack_logger_progname) || @progname end # Set a hash of tags on logger. If a block is given, the tags will only be set - # for the duration of the block. + # for the duration of the block. If this method is called inside such a block, + # the tags will only be defined on the tags in that block. When the parent block + # exits, all the tags will be reverted. If there is no block, then the tags will + # be defined as global and apply to all log statements. def tag(tags, &block) tags = Tags.stringify_keys(tags) + thread_tags = thread_local_value(:lumberjack_logger_tags) if block - thread_tags = thread_local_value(:lumberjack_logger_tags) - value = (thread_tags ? thread_tags.merge(tags) : tags) - push_thread_local_value(:lumberjack_logger_tags, value, &block) + merged_tags = (thread_tags ? thread_tags.merge(tags) : tags) + push_thread_local_value(:lumberjack_logger_tags, merged_tags, &block) + elsif thread_tags + thread_tags.merge!(tags) else @tags.merge!(tags) end + nil end + # Remove a tag from the current tag context. If this is called inside a block to a + # call to `tag`, the tags will only be removed for the duration of that block. Otherwise + # they will be removed from the global tags. + def remove_tag(*tag_names) + thread_tags = thread_local_value(:lumberjack_logger_tags) + if thread_tags + tag_names.each { |name| thread_tags.delete(name.to_s) } + else + tag_names.each { |name| @tags.delete(name.to_s) } + end + end + # Return all tags in scope on the logger including global tags set on the Lumberjack # context, tags set on the logger, and tags set on the current block for the logger def tags tags = {} context_tags = Lumberjack.context_tags @@ -472,10 +490,10 @@ while !closed? begin sleep(flush_seconds) logger.flush if Time.now - logger.last_flushed_at >= flush_seconds rescue => e - STDERR.puts("Error flushing log: #{e.inspect}") + $stderr.puts("Error flushing log: #{e.inspect}") end end end end end