🌲 Timber - Log Better. Solve Problems Faster.
Overview
Timber for Elixir is an optional upgrade you can install for Elixir apps on the Timber.io logging platform. Instead of completely replacing your log messages, Timber augments your logs with critical metadata. Turning them into rich events with context. This preserves the readability of your logs while still improving the quality of your log data.
How it works
For example, Timber turns this raw text log:
Sent 200 in 45.ms
Into a rich http_server_response event.
Sent 200 in 45.2ms @metadata {"dt": "2017-02-02T01:33:21.154345Z", "level": "info", "context": {"user": {"id": 1}, "http": {"method": "GET", "host": "timber.io", "path": "/path", "request_id": "abcd1234"}}, "event": {"http_response": {"status": 200, "time_ms": 45.2}}}
In the Timber console simply click the line to view this metdata. Moreover, this data allows you to run powerful queries like:
context.request_id:abcd1234- View all logs generated for a specific request.context.user.id:1- View logs generated by a specific user.type:http_response- View specific events (exceptions, sql queries, etc)http_server_response.time_ms:>=1000- View slow responses with the ability to zoom out and view them in context (request, user, etc).level:error- Levels in your logs!
For a complete overview, see the Timber for Elixir docs.
Installation
Add
timberas a dependency inmix.exs:# Mix.exsdef application do[applications: [:timber]]enddef deps do[{:timber, "~> 2.1"}]endIn your
shell, runmix deps.get.In your
shell, runmix timber.install.
Usage
Basic logging
No special API, Timber works directly with Logger:
Logger.info("My log message")
# => My log message @metadata {"level": "info", "context": {...}}
Custom events
Custom events allow you to extend beyond events already defined in
the Timber.Events namespace.
event_data = %{customer_id: "xiaus1934", amount: 1900, currency: "USD"}
Logger.info("Payment rejected", event: %{payment_rejected: event_data})
# => Payment rejected @metadata {"level": "warn", "event": {"payment_rejected": {"customer_id": "xiaus1934", "amount": 100, "reason": "Card expired"}}, "context": {...}}
- Notice the
:payment_rejectedroot key. Timber will classify this event as such. - In the Timber console use the query:
type:payment_rejectedorpayment_rejected.amount:>100.
Custom contexts
Context is additional data shared across log lines. Think of it like log join data.
It's stored in the local process dictionary and is incldued in every log written
within that process. Custom contexts allow you to extend beyond contexts already defined in the Timber.Contexts namespace.
Timber.add_context(%{build: %{version: "1.0.0"}})
Logger.info("My log message")
# => My log message @metadata {"level": "info", "context": {"build": {"version": "1.0.0"}}}
- Notice the
:buildroot key. Timber will classify this context as such. - In the Timber console use the query:
build.version:1.0.0
Metrics
Logging metrics is accomplished by logging custom events. Please see our metrics docs page for a more detailed explanation with examples.
Jibber-Jabber
Which log events does Timber structure for me?
Out of the box you get everything in the Timber.Events namespace.
We also add context to every log, everything in the Timber.Contexts
namespace. Context is structured data representing the current environment when the log line
was written. It is included in every log line. Think of it like join data for your logs.
What about my current log statements?
They'll continue to work as expected. Timber adheres strictly to the default Logger interface
and will never deviate in any way.
In fact, traditional log statements for non-meaningful events, debug statements, etc, are encouraged. In cases where the data is meaningful, consider logging a custom event.
How is Timber different?
- It's just better logging. Nothing beats well structured raw data. And that's exactly what Timber aims to provide. There are no agents, special APIs, or proprietary data sets that you can't access.
- Improved log data quality. Instead of relying on parsing alone, Timber ships libraries that structure and augment your logs from within your application. Improving your log data at the source.
- Human readability. Timber augments your logs without sacrificing human readability. For
example:
log message @metadata {...}. And when you view your logs in the Timber console, you'll see the human friendly messages with the ability to view the associated metadata. - Long retention. Logging is notoriously expensive with low retention. Timber offers 6 months of retention by default with sane prices.
- Normalized schema. Have multiple apps? All of Timber's libraries adhere to our JSON schema. This means queries, alerts, and graphs for your ruby app can also be applied to your elixir app (for example).