Class: Logtail::Config
- Inherits:
-
Object
- Object
- Logtail::Config
- Includes:
- Singleton
- Defined in:
- lib/logtail/config.rb,
lib/logtail/config/integrations.rb
Overview
Singleton class for reading and setting Logtail configuration.
For Rails apps, this is installed into ‘config.logtail`. See examples below.
Defined Under Namespace
Modules: Integrations Classes: NoLoggerError, SimpleLogFormatter
Constant Summary collapse
- DEVELOPMENT_NAME =
"development".freeze
- PRODUCTION_NAME =
"production".freeze
- STAGING_NAME =
"staging".freeze
- TEST_NAME =
"test".freeze
Instance Attribute Summary collapse
-
#http_body_limit ⇒ Object
writeonly
Sets the attribute http_body_limit.
Instance Method Summary collapse
-
#debug(&block) ⇒ Object
Convenience method for logging debug statements to the debug logger set in this class.
-
#debug_logger ⇒ Object
Accessor method for #debug_logger=.
-
#debug_logger=(value) ⇒ Object
This is useful for debugging.
-
#debug_to_file!(file_path) ⇒ Object
A convenience method for writing internal Logtail debug messages to a file.
-
#debug_to_stdout! ⇒ Object
A convenience method for writing internal Logtail debug messages to STDOUT.
- #development? ⇒ Boolean
-
#environment ⇒ Object
Accessor method for #environment=.
-
#environment=(value) ⇒ Object
The environment your app is running in.
-
#filter_sent_to_better_stack(&block) ⇒ Object
This allows filtering logs that are sent to Better Stack.
-
#integrations ⇒ Object
Convenience method for accessing the various ‘Logtail::Integrations::*` class settings.
-
#logger ⇒ Object
Accessor method for #logger=.
-
#logger=(value) ⇒ Object
This is the main logger Logtail writes to.
- #production? ⇒ Boolean
-
#send_to_better_stack?(log_entry) ⇒ Boolean
Whether a particular LogEntry should be sent to Better Stack.
- #staging? ⇒ Boolean
- #test? ⇒ Boolean
Instance Attribute Details
#http_body_limit=(value) ⇒ Object (writeonly)
Sets the attribute http_body_limit
33 34 35 |
# File 'lib/logtail/config.rb', line 33 def http_body_limit=(value) @http_body_limit = value end |
Instance Method Details
#debug(&block) ⇒ Object
Convenience method for logging debug statements to the debug logger set in this class.
60 61 62 63 64 65 66 67 |
# File 'lib/logtail/config.rb', line 60 def debug(&block) debug_logger = Config.instance.debug_logger if debug_logger = yield debug_logger.debug() end true end |
#debug_logger ⇒ Object
Accessor method for #debug_logger=.
84 85 86 |
# File 'lib/logtail/config.rb', line 84 def debug_logger @debug_logger end |
#debug_logger=(value) ⇒ Object
This is useful for debugging. This Sets a debug_logger to view internal Logtail library log messages. The default is ‘nil`. Meaning log to nothing.
See #debug_to_file! and #debug_to_stdout! for convenience methods that handle creating and setting the logger.
79 80 81 |
# File 'lib/logtail/config.rb', line 79 def debug_logger=(value) @debug_logger = value end |
#debug_to_file!(file_path) ⇒ Object
A convenience method for writing internal Logtail debug messages to a file.
94 95 96 97 98 99 100 |
# File 'lib/logtail/config.rb', line 94 def debug_to_file!(file_path) FileUtils.mkdir_p( File.dirname(file_path) ) file = File.open(file_path, "ab") file_logger = ::Logger.new(file) file_logger.formatter = SimpleLogFormatter.new self.debug_logger = file_logger end |
#debug_to_stdout! ⇒ Object
A convenience method for writing internal Logtail debug messages to STDOUT.
108 109 110 111 112 |
# File 'lib/logtail/config.rb', line 108 def debug_to_stdout! stdout_logger = ::Logger.new(STDOUT) stdout_logger.formatter = SimpleLogFormatter.new self.debug_logger = stdout_logger end |
#development? ⇒ Boolean
157 158 159 |
# File 'lib/logtail/config.rb', line 157 def development? environment == DEVELOPMENT_NAME end |
#environment ⇒ Object
Accessor method for #environment=
125 126 127 |
# File 'lib/logtail/config.rb', line 125 def environment @environment ||= ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development" end |
#environment=(value) ⇒ Object
The environment your app is running in. Defaults to ‘RACK_ENV` and `RAILS_ENV`. It should be rare that you have to set this. If the aforementioned env vars are not set please do.
120 121 122 |
# File 'lib/logtail/config.rb', line 120 def environment=(value) @environment = value end |
#filter_sent_to_better_stack(&block) ⇒ Object
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 LogEntry for available attributes of the block parameter.
52 53 54 55 |
# File 'lib/logtail/config.rb', line 52 def filter_sent_to_better_stack(&block) @better_stack_filters ||= [] @better_stack_filters << -> (log_entry) { yield(log_entry) } end |
#integrations ⇒ Object
Convenience method for accessing the various ‘Logtail::Integrations::*` class settings. These provides settings for enabling, disabled, and silencing integrations. See Integrations for a full list of available methods.
132 133 134 |
# File 'lib/logtail/config.rb', line 132 def integrations Integrations end |
#logger ⇒ Object
Accessor method for #logger=.
148 149 150 151 152 153 154 |
# File 'lib/logtail/config.rb', line 148 def logger if @logger.is_a?(Proc) @logger.call() else @logger ||= Logger.new(STDOUT) end end |
#logger=(value) ⇒ Object
This is the main logger Logtail writes to. All of the Logtail integrations write to this logger instance. It should be set to your global logger. For Rails, this is set automatically to ‘Rails.logger`, you should not have to set this.
143 144 145 |
# File 'lib/logtail/config.rb', line 143 def logger=(value) @logger = value end |
#production? ⇒ Boolean
167 168 169 |
# File 'lib/logtail/config.rb', line 167 def production? environment == PRODUCTION_NAME end |
#send_to_better_stack?(log_entry) ⇒ Boolean
Whether a particular LogEntry should be sent to Better Stack
36 37 38 39 40 41 |
# File 'lib/logtail/config.rb', line 36 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 |
#staging? ⇒ Boolean
172 173 174 |
# File 'lib/logtail/config.rb', line 172 def staging? environment == STAGING_NAME end |
#test? ⇒ Boolean
162 163 164 |
# File 'lib/logtail/config.rb', line 162 def test? environment == TEST_NAME end |