Skip to content

Commit f12c439

Browse files
authored
Add ability to filter logs send to Better Stack (#23)
1 parent da2925f commit f12c439

File tree

8 files changed

+35
-6
lines changed

8 files changed

+35
-6
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ jobs:
2323
- 2.5
2424
- 2.4
2525
- 2.3
26-
- 2.2
2726
- jruby-9.4.3.0
2827
- jruby-9.2.14.0
2928
- truffleruby-23.0.0

example-project/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
source 'https://rubygems.org'
2-
gem "logtail"
2+
gem "logtail", "~> 0.1.12"

example-project/Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
logtail (0.1.9)
4+
logtail (0.1.12)
55
msgpack (~> 1.0)
6-
msgpack (1.7.1)
6+
msgpack (1.7.2)
77

88
PLATFORMS
99
ruby

example-project/main.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
http_device = Logtail::LogDevices::HTTP.new(ARGV[0])
1616
logger = Logtail::Logger.new(http_device)
1717

18+
# Filter logs that shouldn't be sent to Better Stack, see {Logtail::LogEntry} for available attributes
19+
Logtail.config.filter_sent_to_better_stack { |log_entry| log_entry.message.include?("DO_NOT_SEND") }
20+
1821
# LOGGING
1922

2023
# Send debug logs messages using the debug() method
@@ -33,6 +36,9 @@
3336
}
3437
)
3538

39+
# Some messages can be filtered, see {Logtail::Config#filter_sent_to_better_stack} call above
40+
logger.info("This message will not be sent to Better Stack because it contains 'DO_NOT_SEND'")
41+
3642
# Send error messages using the error() method
3743
logger.error("Oops! An error occurred!")
3844

lib/logtail/config.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,28 @@ def call(severity, timestamp, progname, msg)
3232

3333
attr_writer :http_body_limit
3434

35+
# Whether a particular {Logtail::LogEntry} should be sent to Better Stack
36+
def send_to_better_stack?(log_entry)
37+
!@better_stack_filters&.any? { |blocker| blocker.call(log_entry) }
38+
rescue => e
39+
debug { "Could not determine whether to send LogEntry to Better Stack (assumed yes): #{e}" }
40+
true
41+
end
42+
43+
# This allows filtering logs that are sent to Better Stack. Can be called multiple times, all filters will
44+
# be applied. If the passed block RETURNS TRUE for a particular LogEntry, it WILL NOT BE SENT to Better Stack.
45+
#
46+
# See {Logtail::LogEntry} for available attributes of the block parameter.
47+
#
48+
# @example Rails
49+
# config.logtail.filter_sent_to_better_stack { |log_entry| log_entry.context_snapshot[:http][:path].start_with?('_') }
50+
# @example Everything else
51+
# Logtail.config.filter_sent_to_better_stack { |log_entry| log_entry.message.include?('IGNORE') }
52+
def filter_sent_to_better_stack(&block)
53+
@better_stack_filters ||= []
54+
@better_stack_filters << -> (log_entry) { yield(log_entry) }
55+
end
56+
3557
# Convenience method for logging debug statements to the debug logger
3658
# set in this class.
3759
# @private

lib/logtail/log_devices/http.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def initialize(source_token, options = {})
8989
# size is constricted by the Logtail API. The actual application limit is a multiple
9090
# of this. Hence the `@request_queue`.
9191
def write(msg)
92+
return unless Logtail.config.send_to_better_stack?(msg)
93+
9294
@msg_queue.enq(msg)
9395

9496
# Lazily start flush threads to ensure threads are alive after forking processes.

lib/logtail/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Logtail
2-
VERSION = "0.1.11"
2+
VERSION = "0.1.12"
33
end

logtail.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
1818
spec.metadata["homepage_uri"] = spec.homepage
1919
spec.metadata["source_code_uri"] = spec.homepage
2020

21-
spec.required_ruby_version = Gem::Requirement.new(">= 2.2.0")
21+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3")
2222

2323
spec.files = `git ls-files`.split("\n")
2424
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")

0 commit comments

Comments
 (0)