Untangle (Bonfire v1.0.0-social)
View SourceLogging/inspecting data, and timing functions, with code location information.
Logging/inspecting
Untangle provides alternatives for IO.inspect and the macros in Elixir's Logger to output code location information. It also provides a polyfill for dbg which was introduce in Elixir 1.14

The first argument is inspected and the second argument (if provided) is used as a label:
> import Untangle
Untangle
> debug(:no, "the answer is") # log at debug
11:19:09.915 [debug] [iex:2] the answer is: :no
:no
> dump(%{a: :map}, "it") # inspect something on stdout
[iex:3] it: %{a: :map}
%{a: :map}When used in a code file, the location information becomes more useful, e.g.:
[lib/test_untangle.ex:15@Test.Untangle.example/2] Here's an empty list: []You may also notice from the iex output that it returns its first argument. This makes it ideal for inserting into a pipeline for debugging purposes:
do_something()
|> debug("output of do_something/0")When you are done debugging something, the location of the debug statement is already in the output so you know where to remove it, comment it out, or simply change warn or info for debug if you only need it during development :-)
Timing functions
You can decorate functions to measure and log their execution time:
use Untangle
@decorate time()
def fun(), do: :stuffwill output something like [info] Time to run MyModule.fun/0: 1 ms
Installation
If available in Hex, the package can be installed by adding untangle to your list of dependencies in mix.exs:
def deps do
[
{:untangle, "~> 0.3"}
]
endand adding some config:
config :untangle,
env: config_env()Configure as default dbg/2 handler
In config/config.exs
config :elixir, :dbg_callback, {Untangle, :custom_dbg, []}
Docs
The docs can be found at https://hexdocs.pm/untangle.
Copyright and License
Copyright (c) 2022 Bonfire contributors
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Summary
Functions
Custom backend for Kernel.dbg/2.
This function provides a backend for Kernel.dbg/2.
This function
Like dump, but for logging at debug level
IO.inspect but outputs to Logger with position information, an optional label and configured not to truncate output too much.
Logs or raises errors based on environment.
Similar to dump, but for logging at error level, and returns an error tuple
Like IO.inspect/2, but accepts a string as second argument and does not truncate the output
Formats the stacktrace.
Receives a stacktrace entry and formats it into a string.
Like dump, but for logging at info level
Like debug, but will do nothing unless the :debug option is truthy
Like maybe_dbg, but requires the :verbose option to be set. Intended for large outputs.
Tries to 'do what i mean'. Requires the debug option to be set regardless. If verbose is also
set, will inspect else will attempt to print some (hopefully smaller) type-dependent summary of
the data (list length, map keys).
Like dump, but for logging at warn level
Functions
Custom backend for Kernel.dbg/2.
This function provides a backend for Kernel.dbg/2.
This function:
- may log or print information about the given
env - may log or print information about
codeand its returned value (usingoptsto inspect terms) - returns the value returned by evaluating
code
Like dump, but for logging at debug level
IO.inspect but outputs to Logger with position information, an optional label and configured not to truncate output too much.
Logs or raises errors based on environment.
This function handles errors differently depending on the environment:
- In test: raises an exception
- In dev: prints a warning
- In production: logs a warning
Examples
# With just a message
iex> # When in dev/prod (not test), prints a warning and does not raise
iex> # Note: Only testing return value here, not side effects
iex> Process.put([:bonfire, :env], :dev)
iex> err("error message")
# Prints: [warning] error message
nil
# With just data
iex> Process.put([:bonfire, :env], :dev)
iex> err(%{key: "value"})
# Prints: [warning] An error occurred: %{key: "value"}
%{key: "value"}
# With both data and message
iex> Process.put([:bonfire, :env], :dev)
iex> err(%{key: "value"}, "Custom error message")
# Prints: [warning] Custom error message: %{key: "value"}
%{key: "value"}In test environment, it raises an exception:
iex> Process.put([:bonfire, :env], :test)
iex> err("test error")
** (RuntimeError) test error
Similar to dump, but for logging at error level, and returns an error tuple:
Like IO.inspect/2, but accepts a string as second argument and does not truncate the output
@spec format_stacktrace(Exception.stacktrace() | nil) :: String.t()
Formats the stacktrace.
A stacktrace must be given as an argument. If not, the stacktrace
is retrieved from Process.info/2.
@spec format_stacktrace_entry(Exception.stacktrace_entry()) :: String.t()
Receives a stacktrace entry and formats it into a string.
Like dump, but for logging at info level
Like debug, but will do nothing unless the :debug option is truthy
Like maybe_dbg, but requires the :verbose option to be set. Intended for large outputs.
Tries to 'do what i mean'. Requires the debug option to be set regardless. If verbose is also
set, will inspect else will attempt to print some (hopefully smaller) type-dependent summary of
the data (list length, map keys).
Like dump, but for logging at warn level