Pretty v1.0.4 Pretty View Source

Inspect values with syntax colors despite your remote console.

This module addresses two surprises you'll encounter trying to dump data to the remote console like you did during development with iex -S mix:

  • IO.inspect/1 et al work fine at the iex> prompt but their output goes to the server's logs, not your console when you call them from inside a :telemetry handler function or other troubleshooting mechanism

  • The syntax colors aren't working

  • The inspection width is 80… just like iex, now that you think of it

Why? See the explanation.

In case of emergency, BREAK GLASS to get what you need with some copying and pasting.

Usage

To get the right syntax colors and inspection width, replace your calls to IO.inspect/1 with calls to Pretty.inspect/1:

Pretty.inspect(<<0, 1, 2>>, width: 40)

... and the same for IO.inspect/2:

[1, 2, 3]
|> IO.inspect(label: "before")
|> Enum.map(&(&1 * 2))
|> IO.inspect(label: "after")
|> Enum.sum()

To get the right colors, width, and output device, use Pretty.bind/1 to get an inspector/1 function, and use it instead of IO.inspect/1 or Pretty.inspect/1:

dump = Pretty.bind(label: "Ecto")
handler = fn name, m10s, m6a, nil -> dump.({name, m10s, m6a}) end
:telemetry.attach(self(), [:my_app, :repo, :query], handler, nil)

Link to this section Summary

Types

Keyword options supported by IO.inspect/2.

A 1-ary inspection function returning its argument unchanged e.g. IO.inspect/1.

Functions

Bind an inspection function to the current standard output.

Bind an inspection function to a particular device.

Inspect an item, writing the report to the standard output. Return the item unchanged.

Inspect an item, writing the report to a device. Return the item unchanged.

Link to this section Types

Link to this type

inspect_opts()

View Source
inspect_opts() :: keyword()

Keyword options supported by IO.inspect/2.

Link to this type

inspector(item)

View Source
inspector(item) :: (item -> item)

A 1-ary inspection function returning its argument unchanged e.g. IO.inspect/1.

Link to this section Functions

Bind an inspection function to the current standard output.

See bind/2 for more details.

Bind an inspection function to a particular device.

The inspector's device and options are resolved when bind/2 is called, not when the inspector is called. If you bind at the remote console prompt, the device and options will remain appropriate for your console no matter which process calls the inspector.

See inspect/3 for details on option handling.

Link to this function

inspect(item, opts \\ [])

View Source
inspect(item, inspect_opts()) :: item when item: var

Inspect an item, writing the report to the standard output. Return the item unchanged.

Like IO.inspect/2, but with pretty defaults appropriate to the device.

See inspect/3 for details on option handling.

Link to this function

inspect(device, item, opts)

View Source
inspect(IO.device(), item, inspect_opts()) :: item when item: var

Inspect an item, writing the report to a device. Return the item unchanged.

Like IO.inspect/3, but with pretty defaults appropriate to the device: