Context is additional data shared across log lines. Think of it like log join data. Custom contexts allow you to extend beyond contexts already defined in the Timber::Contexts namespace.

Implementation

Timber::CurrentContext.with({build: {version: "1.0.0"}}) do
  logger.info("My log message")
end

# => My log message @metadata {"level": "info", "context": {"build": {"version": "1.0.0"}}}
  • Notice the :build root key. Timber will classify this context as such.

Rails ActionController Example

The following adds context to logs written within the request cycle:

class ApplicationController < ActionController::Base
  around_filter :add_build_context

  private
    def add_build_context
      Timber::CurrentContext.with({build: {version: "1.0.0"}}) do
        yield
      end
    end
end

# => Processing MyController @metadata {... "context": {"build": {"version": "1.0.0"}} ...}

What you can do

  1. Use this data for querying your logs. Ex: build.version:1.0.0.
  2. View this custom context when inspecting log lines:

    Context panels

    Or view it in the raw JSON payload:

    Context raw

Limits

  • There are no limits to the depth of fields you can nest. As long as the resulting field path is less than 128 characters.
  • There are no limits to the number of custom fields you can send. As long as the result payload size is less than 2mb.

Questions? Need help?

Chat with us, just click the talk button in the bottom right of this page.

Chat with us

Start a trial - 15 days free

Log better and solve problems faster, today. Less than 1 minutes to install.

Start a trial