Best Practice for `warn()` Messages and Error Context?

Here’s the relevant part of my code:

local success, err = self:AddRebirth(player, 1, replica)
if not success then
	warn(string.format(
		"[RebirthService | OnServerEvent] AddRebirth failed for %s (UserId: %d). Error: %s",
		player.Name,
		player.UserId,
		tostring(err)
	))
	return
end

function RebirthService:AddRebirth(player: Player, amount: number, replica: Replica?)
	local success, err = ValidationUtils:ValidateNumber(amount, "Amount")
	if not success then
		return false, err
	end

	success, err = ValidationUtils:ValidateMin(amount, 1, "Amount")
	if not success then
		return false, err
	end

	replica, err = DataManagerUtils:ResolveReplicaOrWarn(player, "RebirthService:AddRebirth", replica)
	if not replica then
		return false, err
	end

	local currentRebirth = replica.Data.Stats.Rebirth
	return self:SetRebirth(player, currentRebirth + amount, replica)
end

function Utils:ResolveReplicaOrWarn(player: Player, contextName: string, replica: Replica?): (Replica?, string?)
	local resolvedReplica, err = self:ResolveReplica(player, replica)

	if not resolvedReplica then
		warn(string.format(
			"[%s] Replica not found for %s (UserId: %d). Error: %s", 
			contextName, 
			player.Name, 
			player.UserId,
			tostring(err)
			))
		
		return nil, err
	end

	return resolvedReplica
end

Is this a good way to handle warn() messages?

My goal is to monitor player-related errors through Creator Hub after release, and I want the logs to be easy to trace. Since Creator Hub doesn’t clearly show where an error originated, I’m including a prefix like [RebirthService | OnServerEvent] so I can immediately identify the source.

I also want the warnings to be useful in the in-game Output, so if a player reports a bug in my discord server, they can simply send me their Output log.

The only thing I’m unsure about is including player.Name and player.UserId in the warning. In Creator Hub they don’t seem to display properly and instead show something like <Player>. Would it be better to keep that information anyway, or should I just make the message more generic, like "Failed to add rebirth" or "Failed to process rebirth request" so it doesn’t use player.Name at all?

I’m mainly looking for feedback on whether this logging approach scales well or if there’s a cleaner pattern people commonly use.

1 Like