Class: Logtail::Config

Inherits:
Object
  • Object
show all
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.

Examples:

Rails example

config.logtail. = false

Everything else

config = Logtail::Config.instance
config.append_metdata = false

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

Instance Method Summary collapse

Instance Attribute Details

#http_body_limit=(value) ⇒ Object (writeonly)

Sets the attribute http_body_limit

Parameters:

  • value

    the value to set the attribute http_body_limit to.



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.



38
39
40
41
42
43
44
45
# File 'lib/logtail/config.rb', line 38

def debug(&block)
  debug_logger = Config.instance.debug_logger
  if debug_logger
    message = yield
    debug_logger.debug(message)
  end
  true
end

#debug_loggerObject

Accessor method for #debug_logger=.



62
63
64
# File 'lib/logtail/config.rb', line 62

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.

Examples:

Rails

config.logtail.debug_logger = ::Logger.new(STDOUT)

Everything else

Logtail::Config.instance.debug_logger = ::Logger.new(STDOUT)


57
58
59
# File 'lib/logtail/config.rb', line 57

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.

Examples:

Rails

config.Logtail.debug_to_file!("#{Rails.root}/log/logtail.log")

Everything else

Logtail::Config.instance.debug_to_file!("log/logtail.log")


72
73
74
75
76
77
78
# File 'lib/logtail/config.rb', line 72

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.

Examples:

Rails

config.logtail.debug_to_stdout!

Everything else

Logtail::Config.instance.debug_to_stdout!


86
87
88
89
90
# File 'lib/logtail/config.rb', line 86

def debug_to_stdout!
  stdout_logger = ::Logger.new(STDOUT)
  stdout_logger.formatter = SimpleLogFormatter.new
  self.debug_logger = stdout_logger
end

#development?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/logtail/config.rb', line 135

def development?
  environment == DEVELOPMENT_NAME
end

#environmentObject

Accessor method for #environment=



103
104
105
# File 'lib/logtail/config.rb', line 103

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.

Examples:

If you do not set ‘RACK_ENV` or `RAILS_ENV`

Logtail::Config.instance.environment = "staging"


98
99
100
# File 'lib/logtail/config.rb', line 98

def environment=(value)
  @environment = value
end

#integrationsObject

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.



110
111
112
# File 'lib/logtail/config.rb', line 110

def integrations
  Integrations
end

#loggerObject

Accessor method for #logger=.



126
127
128
129
130
131
132
# File 'lib/logtail/config.rb', line 126

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.

Examples:

Non-rails frameworks

my_global_logger = Logtail::Logger.new(STDOUT)
Logtail::Config.instance.logger = my_global_logger


121
122
123
# File 'lib/logtail/config.rb', line 121

def logger=(value)
  @logger = value
end

#production?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/logtail/config.rb', line 145

def production?
  environment == PRODUCTION_NAME
end

#staging?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/logtail/config.rb', line 150

def staging?
  environment == STAGING_NAME
end

#test?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/logtail/config.rb', line 140

def test?
  environment == TEST_NAME
end