lib/semantic_logger/appenders.rb in semantic_logger-4.7.4 vs lib/semantic_logger/appenders.rb in semantic_logger-4.8.0
- old
+ new
@@ -8,60 +8,68 @@
@logger.name = self.class.name
end
def add(**args, &block)
appender = SemanticLogger::Appender.factory(**args, &block)
+
+ if appender.is_a?(Appender::File) && console_output?
+ logger.warn "Ignoring attempt to add a second console appender since it would result in duplicate console output."
+ return
+ end
+
self << appender
appender
end
+ # Whether any of the existing appenders already output to the console?
+ # I.e. Writes to stdout or stderr.
+ def console_output?
+ any? do |appender|
+ next unless appender.is_a?(Appender::File)
+
+ [$stderr, $stdout].include?(appender.instance_variable_get(:@log))
+ end
+ end
+
def log(log)
each do |appender|
- begin
- appender.log(log) if appender.should_log?(log)
- rescue Exception => e
- logger.error "Failed to log to appender: #{appender.name}", e
- end
+ appender.log(log) if appender.should_log?(log)
+ rescue Exception => e
+ logger.error "Failed to log to appender: #{appender.name}", e
end
end
def flush
each do |appender|
- begin
- logger.trace "Flushing appender: #{appender.name}"
- appender.flush
- rescue Exception => e
- logger.error "Failed to flush appender: #{appender.name}", e
- end
+ logger.trace "Flushing appender: #{appender.name}"
+ appender.flush
+ rescue Exception => e
+ logger.error "Failed to flush appender: #{appender.name}", e
end
logger.trace "All appenders flushed"
end
def close
- each do |appender|
- begin
- logger.trace "Closing appender: #{appender.name}"
- appender.flush
- appender.close
- delete(appender)
- rescue Exception => e
- logger.error "Failed to close appender: #{appender.name}", e
- end
+ to_a.each do |appender|
+ logger.trace "Closing appender: #{appender.name}"
+ delete(appender)
+ appender.flush
+ appender.close
+ rescue Exception => e
+ logger.error "Failed to close appender: #{appender.name}", e
end
logger.trace "All appenders closed and removed from appender list"
end
# After a fork the appender thread is not running, start it if it is not running.
def reopen
each do |appender|
- begin
- next unless appender.respond_to?(:reopen)
+ next unless appender.respond_to?(:reopen)
- logger.trace "Reopening appender: #{appender.name}"
- appender.reopen
- rescue Exception => e
- logger.error "Failed to re-open appender: #{appender.name}", e
- end
+ logger.trace "Reopening appender: #{appender.name}"
+ appender.reopen
+ rescue Exception => e
+ logger.error "Failed to re-open appender: #{appender.name}", e
end
logger.trace "All appenders re-opened"
end
end
end