Packages
timber
2.0.0-rc4
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/timber/install/timber_config_file.ex
defmodule Mix.Tasks.Timber.Install.TimberConfigFile do
@moduledoc false
alias Mix.Tasks.Timber.Install.{FileHelper, IOHelper}
@file_name "timber.exs"
@file_path Path.join(["config", @file_name])
# Adds the config/timber.exs file to be linked in config/config.exs
def create!(application, project, api) do
contents =
"""
use Mix.Config
#{endpoint_portion(project)}#{repo_portion(project)}
# Use Timber as the logger backend
# Feel free to add additional backends if you want to send you logs to multiple devices.
#{timber_portion(application, api)}
# For dev / test environments, always log to STDOUT and format the logs properly
if Mix.env() == :dev || Mix.env() == :test do
# Fall back to the default `:console` backend with the Timber custom formatter
config :logger,
backends: [:console],
utc_log: true
config :logger, :console,
format: {Timber.Formatter, :format},
metadata: [:timber_context, :event]
config :timber, Timber.Formatter,
colorize: true,
format: :logfmt,
print_timestamps: true,
print_log_level: true,
print_metadata: false # turn this on to view the additiional metadata
end
# Need help?
# Email us: support@timber.io
# File an issue: https://github.com/timberio/timber-elixir/issues
"""
FileHelper.write!(@file_path, contents, api)
end
defp endpoint_portion(%{endpoint_module_name: nil}), do: ""
defp endpoint_portion(%{mix_name: mix_name, endpoint_module_name: endpoint_module_name}) do
"""
# Update the instrumenters so that we can structure Phoenix logs
config :#{mix_name}, #{endpoint_module_name},
instrumenters: [Timber.Integrations.PhoenixInstrumenter]
"""
end
defp repo_portion(%{repo_module_name: nil}), do: ""
defp repo_portion(%{mix_name: mix_name, repo_module_name: repo_module_name}) do
"""
# Structure Ecto logs
config :#{mix_name}, #{repo_module_name},
loggers: [{Timber.Integrations.EctoLogger, :log, [:info]}]
"""
end
defp timber_portion(%{platform_type: "heroku"}, _api) do
"""
# For Heroku, use the `:console` backend provided with Logger but customize
# it to use Timber's internal formatting system
config :logger,
backends: [:console],
utc_log: true
config :logger, :console,
format: {Timber.Formatter, :format},
metadata: [:timber_context, :event]
"""
end
defp timber_portion(_application, api) do
"""
# Deliver logs via HTTP to the Timber API by using the Timber HTTP backend.
config :logger,
backends: [Timber.LoggerBackends.HTTP],
utc_log: true
config :timber,
api_key: #{api_key_portion(api)},
http_client: Timber.HTTPClients.Hackney
"""
end
defp api_key_portion(%{api_key: api_key} = api) do
"""
How would you prefer to store your Timber API key?
1) In the TIMBER_LOGS_KEY environment variable
2) Inline within the #{@file_path} file
"""
|> IOHelper.puts()
case IOHelper.ask("Enter your choice (1/2)", api) do
"1" -> "{:system, \"TIMBER_LOGS_KEY\"}"
"2" -> "\"#{api_key}\""
other ->
"Sorry #{inspect(other)} is not a valid input. Please try again."
|> IOHelper.puts(:red)
api_key_portion(api)
end
end
def file_path, do: @file_path
def link!(config_file_path, api) do
contents =
"""
# Import Timber, structured logging
import_config \"#{@file_name}\"
"""
check = "import_config \"#{@file_name}\""
FileHelper.append_once!(config_file_path, contents, check, api)
end
end