languages / elixir / usage / custom-context

Elixir Usage - Custom Context

Custom contexts allow you to extend beyond contexts already defined in the Timber.Contexts namespace. If you aren't sure what context is, please read the "Metdata, Context, and Events" doc. The How it works section below also expands on this concept.

How to use it

1
2
Timber.add_context(build: %{version: "1.0.0"})
Logger.info("My log message")
  1. Search it with queries like: build.version:1.0.0
  2. View this context when viewing a log's metadata

How it works

Context represents the current environment when the log was written. It's extremely powerful and introduces a missing gap with standard Elixir logging. In essence, it's join data for your logs, allowing you relate logs together without complicated regex searches. It's how Timber is able to provide features like tailing a user and tracing a request.

When your logs are received by the Timber service, they'll have the context included. The resulting JSON document for an example log will look like:

1
2
3
4
5
6
7
8
9
10
11
{
  "message": "My log message",
  "level": "info",
  "context": {
    "custom": {
      "build": {
        "version": "1.0.0"
      }
    }
  }
}
Questions?

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

Chat with us
Sign Up

Log better and solve problems faster, today. Install in under a minute.

Sign Up