Class: Timber::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/timber/config.rb,
lib/timber/config/integrations.rb,
lib/timber/config/integrations/rack.rb

Overview

Singleton class for reading and setting Timber configuration.

For Rails apps, this is installed into ‘config.timber`. See examples below.

Examples:

Rails example

config.timber. = false

Everything else

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

Defined Under Namespace

Modules: Integrations

Constant Summary collapse

DEFAULT_HTTP_BODY_LIMIT =
2048.freeze
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_limitObject

Accessor method for #http_body_limit=



144
145
146
# File 'lib/timber/config.rb', line 144

def http_body_limit
  @http_body_limit
end

Instance Method Details

#debug_loggerObject

Accessor method for #debug_logger=.



70
71
72
# File 'lib/timber/config.rb', line 70

def debug_logger
  @debug_logger
end

#debug_logger=(value) ⇒ Object

This is useful for debugging. This Sets a debug_logger to view internal Timber 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.timber.debug_logger = ::Logger.new(STDOUT)

Everything else

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


65
66
67
# File 'lib/timber/config.rb', line 65

def debug_logger=(value)
  @debug_logger = value
end

#debug_to_file!(file_path) ⇒ Object

A convenience method for writing internal Timber debug messages to a file.

Examples:

Rails

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

Everything else

Timber::Config.instance.debug_to_file!("log/timber.log")


80
81
82
83
84
85
86
# File 'lib/timber/config.rb', line 80

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 Timber debug messages to STDOUT.

Examples:

Rails

config.timber.debug_to_stdout!

Everything else

Timber::Config.instance.debug_to_stdout!


94
95
96
97
98
# File 'lib/timber/config.rb', line 94

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

#environmentObject

Accessor method for #environment=



111
112
113
# File 'lib/timber/config.rb', line 111

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`

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


106
107
108
# File 'lib/timber/config.rb', line 106

def environment=(value)
  @environment = value
end

#http_header_filtersObject

Accessor method for #http_header_filters=



126
127
128
# File 'lib/timber/config.rb', line 126

def http_header_filters
  @http_header_filters ||= []
end

#http_header_filters=(value) ⇒ Object

This is a list of header keys that should be filtered. Note, all headers are normalized to down-case. So please only pass down-cased headers.

Examples:

Rails

# config/environments/production.rb
config.timber.header_filter_headers += ['api-key']


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

def http_header_filters=(value)
  @http_header_filters = value
end

#integrationsObject

Convenience method for accessing the various ‘Timber::Integrations::*` class settings. These provides settings for enabling, disabled, and silencing integrations. See Integrations for a full list of available methods.



151
152
153
# File 'lib/timber/config.rb', line 151

def integrations
  Integrations
end

#loggerObject

Accessor method for #logger=.



208
209
210
211
212
213
214
# File 'lib/timber/config.rb', line 208

def logger
  if @logger.is_a?(Proc)
    @logger.call()
  else
    @logger ||= Logger.new(STDOUT)
  end
end

#logger=(value) ⇒ Object

This is the main logger Timber writes to. All of the Timber 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 = Timber::Logger.new(STDOUT)
Timber::Config.instance.logger = my_global_logger


203
204
205
# File 'lib/timber/config.rb', line 203

def logger=(value)
  @logger = value
end

#logrageify!Object

A convenience method that automatically sets Timber’s configuration to closely match the behavior of the ruby lograge library. This makes it easier when transitioning from lograge.

It turns this:

Started GET "/" for 127.0.0.1 at 2012-03-10 14:28:14 +0100
Processing by HomeController#index as HTML
  Rendered text template within layouts/application (0.0ms)
  Rendered layouts/_assets.html.erb (2.0ms)
  Rendered layouts/_top.html.erb (2.6ms)
  Rendered layouts/_about.html.erb (0.3ms)
  Rendered layouts/_google_analytics.html.erb (0.4ms)
Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)

Into this:

Get "/" sent 200 OK in 79ms @metadata {...}

In other words it:

  1. Silences ActiveRecord SQL query logs.

  2. Silences ActiveView template rendering logs.

  3. Silences ActionController controller call logs.

  4. Collapses HTTP request and response logs into a single event.

Notice also that is is not exactly like lograge. This is intentional. Lograge has a number of downsides:

  1. The attribute names (‘method`, `format`, `status`, `db`, etc) are too generalized and vague. This makes it very likely that it will clash with other structured data you’re logging.

  2. It doesn’t support context making it near impossible to view in-app logs generated for the same request.



189
190
191
192
193
194
# File 'lib/timber/config.rb', line 189

def logrageify!
  integrations.action_controller.silence = true
  integrations.action_view.silence = true
  integrations.active_record.silence = true
  integrations.rack.http_events.collapse_into_single_event = true
end