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

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=



147
148
149
# File 'lib/timber/config.rb', line 147

def http_body_limit
  @http_body_limit
end

Instance Method Details

#debug_loggerObject

Accessor method for #debug_logger=.



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

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)


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

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")


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

def debug_to_file!(file_path)
  unless File.exist? File.dirname path
    FileUtils.mkdir_p File.dirname path
  end
  file = File.open file_path, "a"
  file.binmode
  file.sync = config.autoflush_log
  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!


97
98
99
100
101
# File 'lib/timber/config.rb', line 97

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=



114
115
116
# File 'lib/timber/config.rb', line 114

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"


109
110
111
# File 'lib/timber/config.rb', line 109

def environment=(value)
  @environment = value
end

#http_header_filtersObject

Accessor method for #http_header_filters=



129
130
131
# File 'lib/timber/config.rb', line 129

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']


124
125
126
# File 'lib/timber/config.rb', line 124

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.



154
155
156
# File 'lib/timber/config.rb', line 154

def integrations
  Integrations
end

#loggerObject

Accessor method for #logger=.



211
212
213
214
215
216
217
# File 'lib/timber/config.rb', line 211

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


206
207
208
# File 'lib/timber/config.rb', line 206

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.



192
193
194
195
196
197
# File 'lib/timber/config.rb', line 192

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