JakeWharton / JakeWharton/timber

Adding optional meta data to Timber logs

Open
#184 12 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
10.8k
Forks
994
Avg merge
17h 16m
Merged PRs (30d)
17

Description

We are currently using Timber for all our logging purposes and want to send them up to our backend for monitoring from our production app. However in order to do so we need the ability to pass in optional metadata to each log statement. The extra metadata can be of any type, such as a Map.

Currently using Timber, we don’t have a way to pass in extra information besides Object[] args and overriding the formatMessage() to handle them. The issue with this is that we miss out on type safety and can’t enforce the required schema or make it explicit that the arguments passed in is metadata vs formatting args.

Our current implementation is to add a thin layer on top of Timber which enforces various schema requirements for the consumers ensuring that the data that gets funneled down is compatible for our transport system to send it up to our servers. We’d like to propose upstreaming this to enhance Timber’s API. We’re also interested in getting others thoughts on if it can already be solved by Timber’s current API or why it should remain separate.

Using our current use case:
1. Errors and Warnings should have a unique identifier
* solved by using Timber.tag() + Lint
2. Errors and Warnings must have a stacktrace
* can use linting to enforce that they provide an Exception
3. Optional meta data of type Map can be passed in with each log
* This is lacking as we don’t have a clear way of enforcing type safety on these optional args.
* Furthermore, by overriding formattingMessage() we will be unable to distinguish between arguments that should be formatted into the string vs. arguments that are to be sent up to our servers.

### Suggested implementation:

Timber can support generics when logging in order to define a specific log format that the downstream trees can listen and consume.

i.e.
e(T logMeta); -- where the generic T is a instance of LogMeta (can implement a common interface defined in Timber i.e. LogMeta)
```java
public interface LogMeta { }
```

Using this interface the consumer can create a log meta-data class

```java
public class MyLogMeta implements LogMeta {
public MyLogMeta(String msg, Throwable tr, Map extra);
}
```

then on the client the following call can be made

```java
Timber.e(new MyLogMeta("message",new Exception(),new HashMap()));
```

we can create a tree with the following method signature to intercept the log statement
```java
public class MyLogMetaTree implements Timber.Tree {
...
protected void log(MyLogMeta meta) {
// do something
}
}
```
This way the consumer can define the metadata it expects for one or more of its trees downstream.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing Timber's existing log methods, Tree API, and formatMessage() behavior to determine how metadata is currently passed and consumed. Compare that API with the proposed LogMeta and generic Tree signatures. Done would be an agreed, type-safe metadata design with clear handling for message formatting, exceptions, and downstream trees.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java, kotlin
Domain
api, developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.