Timber v3.1.2 Timber View Source

This is the root module for interacting with the :timber library.

It defines the primary public interface. Users should favor the methods defined in this module over their lower level counterparts. For example, instead of Timber.LocalContact.add/1 use Timber.add_context/1.

Link to this section Summary

Types

The target context to perform the operation

Functions

Adds context which will be included on log entries

Captures the duration in fractional milliseconds since the timer was started. See start_timer/0

Gets the current context

Used to time runtime execution

Link to this section Types

Link to this type

context_location() View Source
context_location() :: :local | :global

The target context to perform the operation.

  • :global - This stores the context at a global level, meaning it will be present on every log line, regardless of which process generates the log line.
  • :local - This stores the context in the Logger Metadata which is local to the process

Link to this section Functions

Link to this function

add_context(data, location \\ :local) View Source
add_context(map(), context_location()) :: :ok

Adds context which will be included on log entries

The second parameter indicates where you want the context to be stored. See context_location for more details.

Link to this function

delete_context(key, location \\ :local) View Source
delete_context(atom(), context_location()) :: :ok

Deletes a context key.

The second parameter indicates which context you want the key to be removed from.

Captures the duration in fractional milliseconds since the timer was started. See start_timer/0.

Link to this function

get_context(type \\ :all) View Source

Gets the current context

This is a merged representation of the Timber.LocalContext and Timber.GlobalContext. If you would like local or global context specifically you can pass :global or :local as the argument to this function.

Used to time runtime execution.

We highly recommend using this method as it uses the system monotonic time for accuracy.

Example

timer = Timber.start_timer()
# .... do something
duration_ms = Timber.duration_ms(timer)
event = %{job_completed: %{duration_ms: duration_ms}}
message = "Job completed in #{duration_ms}ms"
Logger.info(message, event: event)