defmodule DaProductApp.ConsoleLogger do @moduledoc """ A custom Logger backend that writes log messages to the console with Apache-style formatting. Uses the shared LogFormatter to ensure console output matches the WeeklyLogger file format exactly. Features: - Logs are formatted in Apache style: [Day Mon DD HH:MM:SS.microseconds YYYY] [level] [pid PID] [client IP:PORT] message - Output is sent to standard output (IO.write) - Format is identical to WeeklyLogger for consistency across all logging destinations Configuration example: config :logger, backends: [:console, DaProductApp.ConsoleLogger] """ @behaviour :gen_event defstruct [:pid] # --------------------------------------------------------------------------- # :gen_event callbacks # --------------------------------------------------------------------------- def init(__MODULE__) do {:ok, %__MODULE__{pid: System.pid()}} end def init({__MODULE__, _opts}) do {:ok, %__MODULE__{pid: System.pid()}} end def handle_event({level, _gl, {Logger, msg, timestamp, metadata}}, state) do try do formatted = DaProductApp.LogFormatter.format_entry(level, msg, timestamp, metadata, state.pid) IO.write(formatted) {:ok, state} rescue e -> IO.warn("ConsoleLogger handle_event failed: #{inspect(e)}") {:ok, state} end end def handle_event(:flush, state), do: {:ok, state} def handle_event(_event, state), do: {:ok, state} def handle_call({:configure, _opts}, state), do: {:ok, :ok, state} def handle_info(_msg, state), do: {:ok, state} def code_change(_old_vsn, state, _extra), do: {:ok, state} def terminate(_reason, _state), do: :ok end