View Source Untangle (Bonfire v0.9.10-classic-beta.169)
Logging/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 inspect
ed 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: :stuff
will 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"}
]
end
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.
Similar to dump
, but for logging at error level, and returns an error tuple
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
code
and its returned value (usingopts
to 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.
Similar to dump
, but for logging at error level, and returns an error tuple:
- an error tuple with the label, if any
- an error tuple with the passed value otherwise
iex> error(:value)
### [error] :value
{:error, :value}
iex> error({:error, :value})
### [error] :value
{:error, :value}
iex> error(:value, "with label")
### [error] with label: :value
{:error, "with label"}
iex> error({:error, :value}, "with label")
### [error] with label: :value
{:error, "with label"}
@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