From 7a6afa00acf4424d1f901147d41381a92f7cdafc Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Thu, 17 Aug 2023 13:50:50 +0200 Subject: [PATCH] Add ability to filter logs send to Better Stack --- example-project/main.rb | 6 ++++++ lib/logtail/config.rb | 22 ++++++++++++++++++++++ lib/logtail/log_devices/http.rb | 2 ++ 3 files changed, 30 insertions(+) diff --git a/example-project/main.rb b/example-project/main.rb index 6af69ec..b062ddd 100644 --- a/example-project/main.rb +++ b/example-project/main.rb @@ -15,6 +15,9 @@ http_device = Logtail::LogDevices::HTTP.new(ARGV[0]) logger = Logtail::Logger.new(http_device) +# Filter logs that shouldn't be sent to Better Stack, see {Logtail::LogEntry} for available attributes +Logtail.config.filter_sent_to_better_stack { |log_entry| log_entry.message.include?("DO_NOT_SEND") } + # LOGGING # Send debug logs messages using the debug() method @@ -33,6 +36,9 @@ } ) +# Some messages can be filtered, see {Logtail::Config#filter_sent_to_better_stack} call above +logger.info("This message will not be sent to Better Stack because it contains 'DO_NOT_SEND'") + # Send error messages using the error() method logger.error("Oops! An error occurred!") diff --git a/lib/logtail/config.rb b/lib/logtail/config.rb index 662162b..6ea6729 100755 --- a/lib/logtail/config.rb +++ b/lib/logtail/config.rb @@ -32,6 +32,28 @@ def call(severity, timestamp, progname, msg) attr_writer :http_body_limit + # Whether a particular {Logtail::LogEntry} should be sent to Better Stack + def send_to_better_stack?(log_entry) + !@better_stack_filters&.any? { |blocker| blocker.call(log_entry) } + rescue => e + debug { "Could not determine whether to send LogEntry to Better Stack (assumed yes): #{e}" } + true + end + + # This allows filtering logs that are sent to Better Stack. Can be called multiple times, all filters will + # be applied. If the passed block RETURNS TRUE for a particular LogEntry, it WILL NOT BE SENT to Better Stack. + # + # See {Logtail::LogEntry} for available attributes of the block parameter. + # + # @example Rails + # config.logtail.filter_sent_to_better_stack { |log_entry| log_entry.context_snapshot[:http][:path].start_with?('_') } + # @example Everything else + # Logtail.config.filter_sent_to_better_stack { |log_entry| log_entry.message.include?('IGNORE') } + def filter_sent_to_better_stack(&block) + @better_stack_filters ||= [] + @better_stack_filters << -> (log_entry) { yield(log_entry) } + end + # Convenience method for logging debug statements to the debug logger # set in this class. # @private diff --git a/lib/logtail/log_devices/http.rb b/lib/logtail/log_devices/http.rb index 863102e..c3243d4 100755 --- a/lib/logtail/log_devices/http.rb +++ b/lib/logtail/log_devices/http.rb @@ -89,6 +89,8 @@ def initialize(source_token, options = {}) # size is constricted by the Logtail API. The actual application limit is a multiple # of this. Hence the `@request_queue`. def write(msg) + return unless Logtail.config.send_to_better_stack?(msg) + @msg_queue.enq(msg) # Lazily start flush threads to ensure threads are alive after forking processes.