zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

translator.ex (2413B)


      1 defmodule Plug.Cowboy.Translator do
      2   @moduledoc false
      3 
      4   @doc """
      5   The `translate/4` function expected by custom Logger translators.
      6   """
      7   def translate(
      8         min_level,
      9         :error,
     10         :format,
     11         {~c"Ranch listener" ++ _, [ref, conn_pid, stream_id, stream_pid, reason, stack]}
     12       ) do
     13     extra = [" (connection ", inspect(conn_pid), ", stream id ", inspect(stream_id), ?)]
     14     translate_ranch(min_level, ref, extra, stream_pid, reason, stack)
     15   end
     16 
     17   def translate(_min_level, _level, _kind, _data) do
     18     :none
     19   end
     20 
     21   ## Ranch/Cowboy
     22 
     23   defp translate_ranch(
     24          min_level,
     25          _ref,
     26          extra,
     27          pid,
     28          {reason, {mod, :call, [%Plug.Conn{} = conn, _opts]}},
     29          _stack
     30        ) do
     31     if log_exception?(reason) do
     32       {:ok,
     33        [
     34          inspect(pid),
     35          " running ",
     36          inspect(mod),
     37          extra,
     38          " terminated\n",
     39          conn_info(min_level, conn)
     40          | Exception.format(:exit, reason, [])
     41        ], conn: conn, crash_reason: reason, domain: [:cowboy]}
     42     else
     43       :skip
     44     end
     45   end
     46 
     47   defp translate_ranch(_min_level, ref, extra, pid, reason, stack) do
     48     {:ok,
     49      [
     50        "Ranch protocol ",
     51        inspect(pid),
     52        " of listener ",
     53        inspect(ref),
     54        extra,
     55        " terminated\n"
     56        | Exception.format_exit({reason, stack})
     57      ], crash_reason: reason, domain: [:cowboy]}
     58   end
     59 
     60   defp log_exception?({%{__exception__: true} = exception, _}) do
     61     status_ranges =
     62       Application.get_env(:plug_cowboy, :log_exceptions_with_status_code, [500..599])
     63 
     64     status = Plug.Exception.status(exception)
     65 
     66     Enum.any?(status_ranges, &(status in &1))
     67   end
     68 
     69   defp log_exception?(_), do: true
     70 
     71   defp conn_info(_min_level, conn) do
     72     [server_info(conn), request_info(conn)]
     73   end
     74 
     75   defp server_info(%Plug.Conn{host: host, port: :undefined, scheme: scheme}) do
     76     ["Server: ", host, ?\s, ?(, Atom.to_string(scheme), ?), ?\n]
     77   end
     78 
     79   defp server_info(%Plug.Conn{host: host, port: port, scheme: scheme}) do
     80     ["Server: ", host, ":", Integer.to_string(port), ?\s, ?(, Atom.to_string(scheme), ?), ?\n]
     81   end
     82 
     83   defp request_info(%Plug.Conn{method: method, query_string: query_string} = conn) do
     84     ["Request: ", method, ?\s, path_to_iodata(conn.request_path, query_string), ?\n]
     85   end
     86 
     87   defp path_to_iodata(path, ""), do: path
     88   defp path_to_iodata(path, qs), do: [path, ??, qs]
     89 end