lib/lumberjack/logger.rb in lumberjack-1.2.7 vs lib/lumberjack/logger.rb in lumberjack-1.2.8
- old
+ new
@@ -61,11 +61,11 @@
# * :flush_seconds - The maximum number of seconds between flush calls.
# * :roll - If the log device is a file path, it will be a Device::DateRollingLogFile if this is set.
# * :max_size - If the log device is a file path, it will be a Device::SizeRollingLogFile if this is set.
#
# All other options are passed to the device constuctor.
- def initialize(device = STDOUT, options = {})
+ def initialize(device = $stdout, options = {})
options = options.dup
self.level = options.delete(:level) || INFO
self.progname = options.delete(:progname)
max_flush_seconds = options.delete(:flush_seconds).to_f
@@ -98,23 +98,23 @@
# severity level will be ignored.
def level
thread_local_value(:lumberjack_logger_level) || @level
end
- alias_method :sev_threshold, :level
+ alias sev_threshold level
# Set the log level using either an integer level like Logger::INFO or a label like
# :info or "info"
def level=(value)
- if value.is_a?(Integer)
- @level = value
+ @level = if value.is_a?(Integer)
+ value
else
- @level = Severity::label_to_level(value)
+ Severity.label_to_level(value)
end
end
- alias_method :sev_threshold=, :level=
+ alias sev_threshold= level=
# Set the Lumberjack::Formatter used to format objects for logging as messages.
def formatter=(value)
@_formatter = (value.is_a?(TaggedLoggerSupport::Formatter) ? value.__formatter : value)
end
@@ -135,11 +135,11 @@
#
# The tags added with this method are just strings so they are stored in the logger tags
# in an array under the "tagged" tag. So calling `logger.tagged("foo", "bar")` will result
# in tags `{"tagged" => ["foo", "bar"]}`.
def tagged_logger!
- self.extend(TaggedLoggerSupport)
+ extend(TaggedLoggerSupport)
self
end
# Add a message to the log with a given severity. The message can be either
# passed in the +message+ argument or supplied with a block. This method
@@ -171,14 +171,14 @@
current_tags = self.tags
tags = nil unless tags.is_a?(Hash)
if current_tags.empty?
tags = Tags.stringify_keys(tags) unless tags.nil?
else
- if tags.nil?
- tags = current_tags.dup
+ tags = if tags.nil?
+ current_tags.dup
else
- tags = current_tags.merge(Tags.stringify_keys(tags))
+ current_tags.merge(Tags.stringify_keys(tags))
end
end
tags = Tags.expand_runtime_values(tags)
tags = tag_formatter.format(tags) if tag_formatter
@@ -201,11 +201,11 @@
end
end
add_entry(severity, message, progname)
end
- alias_method :log, :add
+ alias log add
# Flush the logging device. Messages are not guaranteed to be written until this method is called.
def flush
device.flush
@last_flushed_at = Time.now
@@ -323,11 +323,11 @@
# do_something # Log level inside the block is +ERROR+
# end
def silence(temporary_level = ERROR, &block)
if silencer
unless temporary_level.is_a?(Integer)
- temporary_level = Severity::label_to_level(temporary_level)
+ temporary_level = Severity.label_to_level(temporary_level)
end
push_thread_local_value(:lumberjack_logger_level, temporary_level, &block)
else
yield
end
@@ -359,14 +359,15 @@
if block
merged_tags = (thread_tags ? thread_tags.merge(tags) : tags.dup)
push_thread_local_value(:lumberjack_logger_tags, merged_tags, &block)
elsif thread_tags
thread_tags.merge!(tags)
+ nil
else
@tags.merge!(tags)
+ nil
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.
@@ -378,21 +379,39 @@
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
+ # context, tags set on the logger, and tags set on the current block for the logger.
def tags
tags = {}
context_tags = Lumberjack.context_tags
tags.merge!(context_tags) if context_tags && !context_tags.empty?
- tags.merge!(@tags) if !@tags.empty?
+ tags.merge!(@tags) if !@tags.empty? && !thread_local_value(:lumberjack_logger_untagged)
scope_tags = thread_local_value(:lumberjack_logger_tags)
tags.merge!(scope_tags) if scope_tags && !scope_tags.empty?
tags
end
+ # Remove all tags on the current logger and logging context within a block.
+ # You can still set new block scoped tags within theuntagged block and provide
+ # tags on individual log methods.
+ def untagged(&block)
+ Lumberjack.use_context(nil) do
+ scope_tags = thread_local_value(:lumberjack_logger_tags)
+ untagged = thread_local_value(:lumberjack_logger_untagged)
+ begin
+ set_thread_local_value(:lumberjack_logger_untagged, true)
+ set_thread_local_value(:lumberjack_logger_tags, nil)
+ tag({}, &block)
+ ensure
+ set_thread_local_value(:lumberjack_logger_untagged, untagged)
+ set_thread_local_value(:lumberjack_logger_tags, scope_tags)
+ end
+ end
+ end
+
private
# Dereference arguments to log calls so we can have methods with compatibility with ::Logger
def call_add_entry(severity, message_or_progname_or_tags, progname_or_tags, &block) #:nodoc:
message = nil
@@ -471,29 +490,29 @@
end
end
end
def write_to_device(entry) #:nodoc:
- begin
- device.write(entry)
- rescue => e
- $stderr.puts("#{e.class.name}: #{e.message}#{' at ' + e.backtrace.first if e.backtrace}")
- $stderr.puts(entry.to_s)
- end
+ device.write(entry)
+ rescue => e
+ # rubocop:disable Style/StderrPuts
+ $stderr.puts("#{e.class.name}: #{e.message}#{" at " + e.backtrace.first if e.backtrace}")
+ $stderr.puts(entry.to_s)
+ # rubocop:enable Style/StderrPuts
end
# Create a thread that will periodically call flush.
def create_flusher_thread(flush_seconds) #:nodoc:
if flush_seconds > 0
begin
logger = self
Thread.new do
- while !closed?
+ until 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}")
+ warn("Error flushing log: #{e.inspect}")
end
end
end
end
end