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, SimpleFormatter

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.



29
30
31
# File 'lib/timber/config.rb', line 29

def append_metadata=(value)
  @append_metadata = 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)


47
48
49
# File 'lib/timber/config.rb', line 47

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


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

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


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

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)


145
146
147
148
149
150
151
# File 'lib/timber/config.rb', line 145

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

Instance Method Details

#append_metadata?Boolean

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

log message @metadata {...}

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)


128
129
130
131
132
133
134
# File 'lib/timber/config.rb', line 128

def append_metadata?
  if defined?(@append_metadata)
    return @append_metadata == 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")


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/timber/config.rb', line 57

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 = SimpleFormatter.new
  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")


75
76
77
78
79
# File 'lib/timber/config.rb', line 75

def debug_to_stdout
  stdout_logger = ::Logger.new(STDOUT)
  stdout_logger.formatter = SimpleFormatter.new
  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"


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

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