NLdoc.Test.Logs (NLdoc.Test v3.0.5)

View Source

This module helps with capturing log output in tests.

Summary

Functions

For use with &ExUnit.CaptureLog.capture_log/2.

For parsing multiple lines of log.

Parsing a single line of log.

Parsing metadata.

Types

log()

@type log() ::
  {time :: String.t(), metadata :: metadata(), level :: String.t(),
   message :: String.t()}

metadata()

@type metadata() :: %{required(String.t()) => String.t()}

Functions

opts(opts \\ [])

@spec opts(keyword()) :: keyword()

For use with &ExUnit.CaptureLog.capture_log/2.

Examples

iex> NLdoc.Test.Logs.opts()
[colors: [enabled: false], format: "$time $metadata[$level] $message\n", metadata: :all]
iex> NLdoc.Test.Logs.opts(format: "$message")
[colors: [enabled: false], metadata: :all, format: "$message"]
iex> NLdoc.Test.Logs.opts(level: :info)
[colors: [enabled: false], format: "$time $metadata[$level] $message\n", metadata: :all, level: :info]

parse!(log)

For parsing multiple lines of log.

Examples

iex> "23:09:04.123 line=114 file=lib/ex_unit/capture_log.ex [info] hello this is a test\n" <>
...> "22:08:03.193 line=124 file=lib/ex_unit/capture_log.ex [error] Lorem, ipsum, dolor sit.\n"
...> |> NLdoc.Test.Logs.parse!()
[
  {
    "23:09:04.123",
    %{"line" => "114", "file" => "lib/ex_unit/capture_log.ex"},
    "info",
    "hello this is a test"
  },
  {
    "22:08:03.193",
    %{"line" => "124", "file" => "lib/ex_unit/capture_log.ex"},
    "error",
    "Lorem, ipsum, dolor sit."
  }
]
iex> NLdoc.Test.Logs.parse!("Not following format.")
** (RuntimeError) Log does not follow format: "Not following format."

parse_line!(log)

@spec parse_line!(log :: String.t()) :: log()

Parsing a single line of log.

Examples

iex> "23:09:04.123 line=114 file=lib/ex_unit/capture_log.ex [info] hello this is a test"
...> |> NLdoc.Test.Logs.parse_line!()
{"23:09:04.123", %{"line" => "114", "file" => "lib/ex_unit/capture_log.ex"}, "info", "hello this is a test"}
iex> NLdoc.Test.Logs.parse_line!("Not following format.")
** (RuntimeError) Log does not follow format: "Not following format."

parse_metadata(metadata)

@spec parse_metadata(metadata :: String.t()) :: metadata()

Parsing metadata.

Examples

iex> "23:09:04.123 hello=world service.name=nldoc_foo"
...> |> NLdoc.Test.Logs.parse_metadata()
%{"hello" => "world", "service.name" => "nldoc_foo"}