Skip to content

Commit

Permalink
Merge pull request #560 from geekingfrog/logging-improvements
Browse files Browse the repository at this point in the history
Logging improvements
  • Loading branch information
L-e-x-o-n authored Jan 28, 2025
2 parents 1710fa5 + 0853080 commit 28b37d8
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 69 deletions.
26 changes: 23 additions & 3 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,29 @@ config :teiserver, Teiserver,
# config :grpc,
# start_server: true

config :logger, :console,
format: "$date $time $metadata[$level] $message\n",
metadata: [:request_id, :user_id]
config :logger, :default_handler, false

metadata = [:request_id, :user_id, :queue_id]

config :logger, LoggerBackends.Console,
format: "$date $time [$level] $metadata $message\n",
metadata: metadata,
level: :debug

config :logger, :error_log,
format: "$date $time [$level] $metadata $message\n",
metadata: metadata,
level: :error

config :logger, :notice_log,
format: "$date $time [$level] $metadata $message\n",
metadata: metadata,
level: :notice

config :logger, :info_log,
format: "$date $time [$level] $metadata $message\n",
metadata: metadata,
level: :info

# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
Expand Down
22 changes: 0 additions & 22 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,6 @@ config :teiserver, Oban,
queues: false,
crontab: false

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

config :logger,
backends: [
{LoggerFileBackend, :error_log},
{LoggerFileBackend, :info_log},
:console
]

config :logger, :error_log,
path: "/tmp/teiserver_error.log",
format: "$time [$level] $metadata $message\n",
metadata: [:request_id, :user_id],
level: :error

config :logger, :info_log,
path: "/tmp/teiserver_info.log",
format: "$time [$level] $metadata $message\n",
metadata: [:request_id, :user_id],
level: :info

try do
import_config "dev.secret.exs"
rescue
Expand Down
5 changes: 5 additions & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,8 @@ config :teiserver, TeiserverWeb.Endpoint,
cache_static_manifest: "priv/static/cache_manifest.json",
server: true,
version: Mix.Project.config()[:version]

config :logger, :default_handler, level: :info

# disable stdout logging in production, rely on log files
config :logger, LoggerBackends.Console, false
39 changes: 8 additions & 31 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -134,37 +134,6 @@ if config_env() == :prod do
auth: :always
end

log_root_path = Teiserver.ConfigHelpers.get_env("TEI_LOG_ROOT_PATH", "/var/log/teiserver/")

config :logger,
backends: [
{LoggerFileBackend, :error_log},
{LoggerFileBackend, :notice_log},
{LoggerFileBackend, :info_log},
:console
]

# Do not print debug messages in production
config :logger, :default_handler, false

config :logger, :error_log,
path: "#{log_root_path}error.log",
format: "$date $time [$level] $metadata $message\n",
metadata: [:request_id, :user_id],
level: :error

config :logger, :notice_log,
path: "#{log_root_path}notice.log",
format: "$date $time [$level] $metadata $message\n",
metadata: [:request_id, :user_id],
level: :notice

config :logger, :info_log,
path: "#{log_root_path}info.log",
format: "$date $time [$level] $metadata $message\n",
metadata: [:request_id, :user_id],
level: :info

if enable_discord_bridge do
config :nostrum,
gateway_intents: [
Expand All @@ -185,3 +154,11 @@ if config_env() == :prod do
bot_name: Teiserver.ConfigHelpers.get_env("TEI_DISCORD_BOT_NAME")
end
end

log_root_path = Teiserver.ConfigHelpers.get_env("TEI_LOG_ROOT_PATH", "/tmp/teiserver")

config :logger, :error_log, path: "#{log_root_path}/error.log"

config :logger, :notice_log, path: "#{log_root_path}/notice.log"

config :logger, :info_log, path: "#{log_root_path}/info.log"
5 changes: 5 additions & 0 deletions lib/teiserver/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ defmodule Teiserver.Application do

@impl true
def start(_type, _args) do
LoggerBackends.add(LoggerBackends.Console)
LoggerBackends.add({LoggerFileBackend, :error_log})
LoggerBackends.add({LoggerFileBackend, :notice_log})
LoggerBackends.add({LoggerFileBackend, :info_log})

# List all child processes to be supervised
children =
[
Expand Down
2 changes: 2 additions & 0 deletions lib/teiserver/matchmaking/queue_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ defmodule Teiserver.Matchmaking.QueueServer do

@impl true
def init(state) do
Logger.metadata(queue_id: state.id)

if state.settings.tick_interval_ms != :manual do
:timer.send_interval(state.settings.tick_interval_ms, :tick)
end
Expand Down
17 changes: 8 additions & 9 deletions lib/teiserver/player/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ defmodule Teiserver.Player.Session do
@impl true
def init({conn_pid, user}) do
ref = Process.monitor(conn_pid)
Logger.metadata(user_id: user.id)

state = %{
user: user,
Expand All @@ -136,7 +137,7 @@ defmodule Teiserver.Player.Session do
matchmaking: initial_matchmaking_state()
}

Logger.debug("init session #{inspect(self())} for player #{user.id}")
Logger.debug("init session #{inspect(self())}")

{:ok, state}
end
Expand All @@ -150,7 +151,7 @@ defmodule Teiserver.Player.Session do
Process.demonitor(state.mon_ref, [:flush])

mon_ref = Process.monitor(new_conn_pid)
Logger.info("session reused for player #{state.user.id}")
Logger.info("session reused")

{:reply, :ok, %{state | conn_pid: new_conn_pid, mon_ref: mon_ref}}
end
Expand Down Expand Up @@ -277,9 +278,7 @@ defmodule Teiserver.Player.Session do
# we're not searching anything. This can happen as a race when two queues
# match the same player at the same time.
# Do log it since it should not happen too often unless something is wrong
Logger.info(
"Got a matchmaking found but in state #{inspect(state.matchmaking)} for user #{inspect(state.user.id)}"
)
Logger.info("Got a matchmaking found but in state #{inspect(state.matchmaking)}")

Matchmaking.cancel(room_pid, state.user.id)
{:noreply, state}
Expand Down Expand Up @@ -351,7 +350,7 @@ defmodule Teiserver.Player.Session do

_ ->
Logger.warning(
"User #{state.user.id} received a request to start a battle but is not in a state to do so #{inspect(state)}"
"User received a request to start a battle but is not in a state to do so #{inspect(state)}"
)

{:noreply, state}
Expand All @@ -362,8 +361,8 @@ defmodule Teiserver.Player.Session do
def handle_info({:DOWN, ref, :process, _, _reason}, state) when ref == state.mon_ref do
# we don't care about cancelling the timer if the player reconnects since reconnection
# should be fairly low (and rate limited) so too many messages isn't an issue
{:ok, _} = :timer.send_after(30_000, :player_timeout)
Logger.debug("Player #{state.user.id} disconnected abruptly")
{:ok, _} = :timer.send_after(2_000, :player_timeout)
Logger.info("Player disconnected abruptly")
{:noreply, %{state | conn_pid: nil}}
end

Expand All @@ -389,7 +388,7 @@ defmodule Teiserver.Player.Session do

def handle_info(:player_timeout, state) do
if is_nil(state.conn_pid) do
Logger.debug("Player #{state.user.id} timed out, stopping session")
Logger.debug("Player timed out, stopping session")
{:stop, :normal, state}
else
{:noreply, state}
Expand Down
5 changes: 3 additions & 2 deletions lib/teiserver/player/tachyon_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ defmodule Teiserver.Player.TachyonHandler do
initial_state |> Map.put(:sess_monitor, sess_monitor) |> Map.put(:pending_responses, %{})

user = initial_state.user
Logger.metadata(user_id: user.id)

event = %{
users: [
Expand All @@ -67,7 +68,7 @@ defmodule Teiserver.Player.TachyonHandler do
@impl Handler
def handle_info({:DOWN, _, :process, _, reason}, state) do
Logger.warning(
"Session for player #{state.user.id} went down because #{inspect(reason)}, terminating connection"
"Session for player went down because #{inspect(reason)}, terminating connection"
)

{:stop, :normal,
Expand Down Expand Up @@ -117,7 +118,7 @@ defmodule Teiserver.Player.TachyonHandler do

def handle_info({:timeout, message_id}, state)
when is_map_key(state.pending_responses, message_id) do
Logger.debug("User(#{state.user.id}) did not reply in time to request with id #{message_id}")
Logger.debug("User did not reply in time to request with id #{message_id}")
{:stop, :normal, state}
end

Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ defmodule Teiserver.MixProject do

# Extra deps
{:ecto_psql_extras, "~> 0.7"},
{:logger_file_backend, "~> 0.0.10"},
{:logger_file_backend, "~> 0.0.14"},
{:logger_backends, "~> 1.0"},
{:timex, "~> 3.7.5"},
{:breadcrumble, "~> 1.0.0"},
{:guardian, "~> 2.1"},
Expand Down
3 changes: 2 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"kcl": {:hex, :kcl, "1.4.2", "8b73a55a14899dc172fcb05a13a754ac171c8165c14f65043382d567922f44ab", [:mix], [{:curve25519, ">= 1.0.4", [hex: :curve25519, repo: "hexpm", optional: false]}, {:ed25519, "~> 1.3", [hex: :ed25519, repo: "hexpm", optional: false]}, {:poly1305, "~> 1.0", [hex: :poly1305, repo: "hexpm", optional: false]}, {:salsa20, "~> 1.0", [hex: :salsa20, repo: "hexpm", optional: false]}], "hexpm", "9f083dd3844d902df6834b258564a82b21a15eb9f6acdc98e8df0c10feeabf05"},
"libcluster": {:hex, :libcluster, "3.3.3", "a4f17721a19004cfc4467268e17cff8b1f951befe428975dd4f6f7b84d927fe0", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "7c0a2275a0bb83c07acd17dab3c3bfb4897b145106750eeccc62d302e3bdfee5"},
"libring": {:hex, :libring, "1.6.0", "d5dca4bcb1765f862ab59f175b403e356dec493f565670e0bacc4b35e109ce0d", [:mix], [], "hexpm", "5e91ece396af4bce99953d49ee0b02f698cd38326d93cd068361038167484319"},
"logger_file_backend": {:hex, :logger_file_backend, "0.0.13", "df07b14970e9ac1f57362985d76e6f24e3e1ab05c248055b7d223976881977c2", [:mix], [], "hexpm", "71a453a7e6e899ae4549fb147b1c6621f4233f8f48f58ca10a64ec67b6c50018"},
"logger_backends": {:hex, :logger_backends, "1.0.0", "09c4fad6202e08cb0fbd37f328282f16539aca380f512523ce9472b28edc6bdf", [:mix], [], "hexpm", "1faceb3e7ec3ef66a8f5746c5afd020e63996df6fd4eb8cdb789e5665ae6c9ce"},
"logger_file_backend": {:hex, :logger_file_backend, "0.0.14", "774bb661f1c3fed51b624d2859180c01e386eb1273dc22de4f4a155ef749a602", [:mix], [], "hexpm", "071354a18196468f3904ef09413af20971d55164267427f6257b52cfba03f9e6"},
"math": {:hex, :math, "0.7.0", "12af548c3892abf939a2e242216c3e7cbfb65b9b2fe0d872d05c6fb609f8127b", [:mix], [], "hexpm", "7987af97a0c6b58ad9db43eb5252a49fc1dfe1f6d98f17da9282e297f594ebc2"},
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},
"merkle_map": {:hex, :merkle_map, "0.2.1", "01a88c87a6b9fb594c67c17ebaf047ee55ffa34e74297aa583ed87148006c4c8", [:mix], [], "hexpm", "fed4d143a5c8166eee4fa2b49564f3c4eace9cb252f0a82c1613bba905b2d04d"},
Expand Down
4 changes: 4 additions & 0 deletions test/teiserver_web/live/battles/match/chat_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ defmodule TeiserverWeb.Battle.MatchLive.ChatLiveTest do

alias Central.Helpers.GeneralTestLib

# https://github.com/beyond-all-reason/teiserver/actions/runs/12946468298/job/36111060092?pr=560
# at a glance there's a problem with throttling in some cases?
@moduletag :needs_attention

setup do
{:ok, kw} = GeneralTestLib.conn_setup(["Overwatch"], [:no_login])

Expand Down

0 comments on commit 28b37d8

Please sign in to comment.