4.7 KiB · text History bbb2a48
defmodule VigilWeb.LogComponents do
  @moduledoc """
  Pieces of the log pages (`VigilWeb.LogLive.Index`, the trace page's
  "Logs in this trace"): a level badge, a log line's time (shown in the
  viewer's local time once connected, the full ISO time on hover), and
  the search box's syntax help.
  """
  use Phoenix.Component

  import VigilWeb.CoreComponents, only: [icon: 1]

  @doc """
  A log level, always labelled: trace and debug muted, info blue, warn
  amber, error and fatal in the error color (fatal solid, with an icon).
  """
  attr :level, :string, required: true
  attr :class, :any, default: "badge-sm w-14"

  def level_badge(assigns) do
    ~H"""
    <span
      data-level={@level}
      class={[
        "log-level badge shrink-0 justify-center gap-0.5 font-mono text-[0.65rem] font-semibold uppercase",
        level_class(@level),
        @class
      ]}
    >
      <.icon :if={@level == "fatal"} name="hero-fire-micro" class="size-3" />{@level || "—"}
    </span>
    """
  end

  defp level_class("fatal"), do: "badge-error"
  defp level_class("error"), do: "badge-error badge-soft"
  defp level_class("warn"), do: "badge-warning badge-soft"
  defp level_class("info"), do: "badge-info badge-soft"
  defp level_class("debug"), do: "badge-ghost"
  defp level_class(_), do: "badge-ghost text-base-content/60"

  @doc """
  When a line was logged: `HH:MM:SS.mmm` (with the date when it isn't
  today), UTC until the browser rewrites it in local time; the full ISO
  time on hover.
  """
  attr :at, DateTime, required: true
  attr :id, :string, required: true
  attr :class, :any, default: nil

  def log_time(assigns) do
    ~H"""
    <time
      id={@id}
      datetime={DateTime.to_iso8601(@at)}
      title={DateTime.to_iso8601(@at)}
      phx-hook=".LocalTime"
      phx-update="ignore"
      class={["log-time shrink-0 font-mono text-xs tabular-nums text-base-content/60", @class]}
    >
      {short_time(@at)}
    </time>
    <script :type={Phoenix.LiveView.ColocatedHook} name=".LocalTime">
      export default {
        mounted() {
          const d = new Date(this.el.dateTime)
          if (isNaN(d)) return
          const opts = {hour: "2-digit", minute: "2-digit", second: "2-digit", hourCycle: "h23"}
          if (d.toDateString() !== new Date().toDateString()) {
            opts.month = "short"
            opts.day = "numeric"
          }
          const ms = String(d.getMilliseconds()).padStart(3, "0")
          this.el.textContent = `${d.toLocaleString(undefined, opts)}.${ms}`
        }
      }
    </script>
    """
  end

  @doc "A time as the server shows it before the browser localizes it."
  def short_time(%DateTime{} = at, today \\ Date.utc_today()) do
    ms =
      at.microsecond |> elem(0) |> div(1000) |> Integer.to_string() |> String.pad_leading(3, "0")

    if DateTime.to_date(at) == today,
      do: Calendar.strftime(at, "%H:%M:%S.") <> ms,
      else: Calendar.strftime(at, "%b %-d %H:%M:%S.") <> ms
  end

  @doc "The search syntax, briefly, in a disclosure."
  attr :id, :string, required: true

  def syntax_help(assigns) do
    ~H"""
    <details id={@id} class="group relative">
      <summary class="btn btn-ghost btn-sm list-none gap-1 text-base-content/70">
        <.icon name="hero-question-mark-circle-micro" class="size-4" /> Syntax
      </summary>
      <div class="absolute right-0 z-20 mt-1 w-80 max-w-[calc(100vw-2rem)] rounded-box border border-base-300 bg-base-100 p-4 text-xs shadow-lg">
        <p class="mb-2 text-base-content/70">
          Words match the message, all of them, ignoring case. Narrow with <code class="font-mono">key:value</code>:
        </p>
        <dl class="grid grid-cols-[auto_minmax(0,1fr)] gap-x-3 gap-y-1">
          <dt><code class="font-mono">level:warn,error</code></dt>
          <dd class="text-base-content/70">either level</dd>
          <dt><code class="font-mono">env:prod</code></dt>
          <dd class="text-base-content/70">environment</dd>
          <dt><code class="font-mono">service:billing</code></dt>
          <dd class="text-base-content/70">
            also <code class="font-mono">host:</code>, <code class="font-mono">release:</code>
          </dd>
          <dt><code class="font-mono">trace:0af7…</code></dt>
          <dd class="text-base-content/70">one trace's lines</dd>
          <dt><code class="font-mono">user.id:42</code></dt>
          <dd class="text-base-content/70">any attribute</dd>
          <dt><code class="font-mono">service:"billing api"</code></dt>
          <dd class="text-base-content/70">quote spaces</dd>
          <dt><code class="font-mono">-env:dev -timeout</code></dt>
          <dd class="text-base-content/70">a leading <code class="font-mono">-</code> negates</dd>
        </dl>
      </div>
    </details>
    """
  end
end