Skip to content

Commit

Permalink
Add ability to filter logs send to Better Stack
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz committed Aug 17, 2023
1 parent 81f7d1d commit 7a6afa0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions example-project/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!")

Expand Down
22 changes: 22 additions & 0 deletions lib/logtail/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/logtail/log_devices/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7a6afa0

Please sign in to comment.