Packages
timber
3.1.2
3.1.2
3.1.1
3.1.0
3.0.0
3.0.0-alpha.3
3.0.0-alpha.2
3.0.0-alpha.1
2.8.4
2.8.3
2.8.2
2.8.1
2.8.0
2.7.0
2.6.1
2.6.0
2.5.6
2.5.5
2.5.4
2.5.3
2.5.2
2.5.1
2.5.0
2.4.5
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0
2.3.4
2.3.3
2.3.1
2.3.0
2.2.1
2.2.0
2.1.8
2.1.7
2.1.6
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.2
2.0.1
2.0.0
2.0.0-rc7
2.0.0-rc6
2.0.0-rc5
2.0.0-rc4
2.0.0-rc3
2.0.0-rc2
2.0.0-rc1
1.1.18
1.1.17
1.1.16
1.1.15
1.1.14
1.1.13
1.1.12
1.1.11
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.16
1.0.15
1.0.14
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
🌲 Great Elixir Logging Made Easy. Official Timber.io Integration.
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/test_the_pipes.ex
defmodule Mix.Tasks.Timber.TestThePipes do
@moduledoc """
Tests and verifies log delivery to the Timber.io service.
Run this whenever you have problems are want to verify setup. This is typically
run immediately after installation.
## Run
mix timber.test_the_pipes
If you installed Timber for production only:
MIX_ENV=prod mix timber.test_the_pipes
"""
use Mix.Task
alias Timber.API
alias Timber.Config
alias Timber.LogEntry
@support_email "support@timber.io"
@doc """
Runs the task
## Options
No options are supported at this time.
"""
@shortdoc "Tests and verifies log delivery to the Timber.io service"
def run(_args \\ []) do
{:ok, _} = Application.ensure_all_started(:timber)
header = ~S"""
^ ^ ^ ^ ^ ^ ___I_ ^ ^ ^ ^ ^ ^ ^
/|\/|\ /|\/|\/|\ /|\ /\-_--\ /|\/|\ /|\/|\/|\ /|\/|\
/|\/|\ /|\/|\/|\ /|\ / \_-__\ /|\/|\ /|\/|\/|\ /|\/|\
/|\/|\ /|\/|\/|\ /|\ |[]| [] | /|\/|\ /|\/|\/|\ /|\/|\
============================================================
TIMBER.IO - TESTING THE PIPES
============================================================
"""
IO.puts([IO.ANSI.green(), header])
with :ok <- verify_api_key_presence(),
:ok <- verify_delivery!() do
:ok
end
end
#
# Verfiication methods
#
defp verify_api_key_presence do
puts("Checking API key presence")
case Application.get_env(:timber, :api_key) do
{:system, env_var_name} ->
case System.get_env(env_var_name) do
nil ->
"""
The #{env_var_name} env var is not set!
This is required because it is specified as the environment variable for the
Timber API key. Timber cannot deliver messaegs with a valid API key. Try:
export TIMBER_API_KEY=my-api-key
Then re-run this command.
If you continue to have trouble please contact support:
#{@support_email}
"""
|> puts(:error)
:exit
_api_key ->
puts("API key found", :success)
:ok
end
nil ->
"""
Your Timber API key is not set!
Please make sure you set your API key properly within your `config/config.exs` file:
# config/config.exs
config :timber, :api_key, "my-api-key"
Then re-run this command.
If you continue to have trouble please contact support:
#{@support_email}
"""
|> puts(:error)
:exit
_else ->
puts("API key found", :success)
:ok
end
end
defp verify_delivery! do
puts("Verifying log delivery to the Timber API")
api_key = Config.api_key()
source_id = Config.source_id()
message = "Testing the pipes (click the inspect icon to view more details)"
log_entry = LogEntry.new(Timber.Utils.Timestamp.now(), :debug, message)
log_map = LogEntry.to_map!(log_entry)
body = Msgpax.pack!([log_map])
case API.send_logs(api_key, source_id, "application/msgpack", body) do
{:ok, status, _headers, _body} when status in 200..299 ->
puts("Logs successfully sent! View them at https://app.timber.io", :success)
:ok
{:ok, status, _headers, body} ->
"""
Unable to deliver logs.
We received a #{status} response from the Timber API:
#{inspect(body)}
If you continue to have trouble please contact support:
#{@support_email}
"""
|> puts(:error)
:exit
{:error, error} ->
"""
Unable to deliver logs.
Here's what we received from the Timber API:
#{inspect(error)}
If you continue to have trouble please contact support:
#{@support_email}
"""
|> puts(:error)
:exit
end
end
#
# Util
#
defp puts(message, level \\ :info)
defp puts(message, :info) do
[IO.ANSI.reset(), "---> #{message}"]
|> IO.puts()
end
defp puts(message, :error) do
[IO.ANSI.red(), ?\n, String.duplicate("!", 60), ?\n, ?\n, message]
|> IO.puts()
:exit
end
defp puts(message, :success) do
[IO.ANSI.green(), "---> ✔ ", message]
|> IO.puts()
end
end