Class: Timber::Config

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

Overview

Interface for setting and reading 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

Classes: NoLoggerError

Constant Summary collapse

PRODUCTION_NAME =
"production".freeze
STAGING_NAME =
"staging".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#append_metadata=(value) ⇒ Object (writeonly)

Sets the attribute append_metadata

Parameters:

  • value

    the value to set the attribute append_metadata to.



21
22
23
# File 'lib/timber/config.rb', line 21

def append_metadata=(value)
   = value
end

#debug_loggerObject

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)


39
40
41
# File 'lib/timber/config.rb', line 39

def debug_logger
  @debug_logger
end

#header_filtersObject

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.filter_headers += ['api-key']


89
90
91
# File 'lib/timber/config.rb', line 89

def header_filters
  @header_filters
end

#http_body_limitObject

Truncates captured HTTP bodies to this specified limit. The default is ‘2000`. If you want to capture more data, you can raise this to a maximum of `5000`, or lower this to be more efficient with data. `2000` characters should give you a good idea of the body content. If you need to raise to `5000` you’re only constraint is network throughput.

Examples:

Rails

config.timber.http_body_limit = 500

Everything else

Timber::Config.instance.http_body_limit = 500


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

def http_body_limit
  @http_body_limit
end

#loggerObject

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:

Rails

Rails.logger = Timber::Logger.new(STDOUT)
config.timber.logger = Rails.logger

Everything else

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


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

def logger
  @logger || Logger.new(STDOUT)
end

Instance Method Details

#append_metadata?Boolean

This determines if the log messages should have metadata appended. Ex:

log message  {...}

By default, this is turned on for production and staging environments only. Other environment should set this setting explicitly.

Examples:

Rails

config.timber. = false

Everything else

Timber::Config.instance. = false

Returns:

  • (Boolean)


118
119
120
121
122
123
124
# File 'lib/timber/config.rb', line 118

def append_metadata?
  if defined?()
    return  == true
  end

  production? || staging?
end

#debug_to_file(file_path) ⇒ Object

A convenience method for writing 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")


49
50
51
52
53
54
55
56
57
58
# File 'lib/timber/config.rb', line 49

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)
  self.debug_logger = file_logger
end

#debug_to_stdoutObject

A convenience method for writing 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")


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

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

#environmentObject

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:

Rails

config.timber.environment = "staging"

Everything else

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


79
80
81
# File 'lib/timber/config.rb', line 79

def environment
  @environment ||= ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development"
end