SQL Logs Crashing

Hello, well I have an addon made by someone here on Facepunch but I lost the thread, anyway it gets logs from the server and stores them on my SQL database, but that keeps stopping all randomly, I know its not the SQL database because the reports mod he made for me aswell works flawlessly. Can someone please have alook at the code and tell me whats wrong with it, I will still try to get intouch with the creator.

[lua]/*--------------------------------------------------------------------------
Start Config
*/--------------------------------------------------------------------------

local Host = “STOPLOOKING” – Host
local User = “YOURNOTSEEINGTHIS” – Username
local Password = “ORTHIS” – Password
local Database = “ORTHIS” – Database

/*--------------------------------------------------------------------------
End Config - Do not edit below this unless you know what you’re doing!
*/------------------------------------------------ -------------------------

require(“mysql”)

local log_connect = CreateClientConVar(“log_connect”, 1, true, false)
local log_spawn = CreateClientConVar(“log_spawn”, 1, true, false)
local log_death = CreateClientConVar(“log_death”, 1, true, false)
local log_disconnect = CreateClientConVar(“log_disconnect”, 1, true, false)
local log_propspawn = CreateClientConVar(“log_propspawn”, 1, true, false)
local log_weapon = CreateClientConVar(“log_weapon”, 1, true, false)
local log_team = CreateClientConVar(“log_team”, 1, true, false)
local log_spray = CreateClientConVar(“log_spray”, 1, true, false)
local log_plyuse = CreateClientConVar(“log_plyuse”, 1, true, false)
local log_noclip = CreateClientConVar(“log_noclip”, 1, true, false)
local log_chat = CreateClientConVar(“log_chat”, 1, true, false)

local db, error = mysql.connect(Host, User, Password, Database);
if (db == 0) then Msg(error … "
") end – MySql error logging

/*--------------------------------------------------------------------------
Log: Player Connected
*/------------------------------------------------ -------------------------
local function LogSqlConnect( name, ip )
if log_connect:GetInt() == 1 then
local PlyNickConnect = name
local ConnectInfo = mysql.escape(db, “[LOG] " … PlyNickConnect … " joined the server ( " … ip … " )” )
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … ConnectInfo … “’ , NOW())”);
end
end
hook.Add(“PlayerConnect”, “LogSqlConnect”, LogSqlConnect)

/*--------------------------------------------------------------------------
Log: Player Spawned
*/------------------------------------------------ -------------------------
local function LogSqlInitialSpawn( ply )
if log_spawn:GetInt() == 1 then
local PlyNickInitialSpawn = ply:Nick()
local PlySteamId = ply:SteamID()
local InitialSpawnInfo = mysql.escape(db, “[LOG] " … PlyNickInitialSpawn … " [” … PlySteamId … “] has spawned in the server.” )
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … InitialSpawnInfo … “’ , NOW())”);
end
end
hook.Add(“PlayerInitialSpawn”, “LogSqlInitialSpawn”, LogSqlInitialSpawn)

/*--------------------------------------------------------------------------
Log: Player Died
*/------------------------------------------------ -------------------------
local function LogSqlDeath( ply, attacker, dmginfo )
if log_death:GetInt() == 1 then
if attacker:IsValid() and attacker:IsPlayer() then
local PlyNick = ply:Nick()
local AttackPly = attacker:Nick()
local PlySteamId = ply:SteamID()
local AttackerSteamId = attacker:SteamID()
local PlyDeath = mysql.escape(db, “[LOG] " … PlyNick … " [” … PlySteamId … “] has died. He was killed by " … AttackPly … " [” … AttackerSteamId … “]”)
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … PlyDeath … “’ , NOW())”);
end
end
end
hook.Add(“DoPlayerDeath”, “LogSqlDeath”, LogSqlDeath)

/*--------------------------------------------------------------------------
Log: Player Disconnected
*/------------------------------------------------ -------------------------
local function LogSqlLeave( ply )
if log_disconnect:GetInt() == 1 then
local PlyNick = ply:Nick()
local PlySteamId = ply:SteamID()
local PlyLeave = mysql.escape(db, “[LOG] " … PlyNick … " [” … PlySteamId … “] has left the server.”)
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … PlyLeave … “’ , NOW())” );
end
end
hook.Add(“PlayerDisconnected”, “LogSqlLeave”, LogSqlLeave)

/*--------------------------------------------------------------------------
Log: Player Spawned A Prop
*/------------------------------------------------ -------------------------
local function LogSqlSpawnProp( ply, model, ent )
if log_propspawn:GetInt() == 1 then
local PlyNick = ply:Nick()
local PlySteamId = ply:SteamID()
local PlyPropSpawn = mysql.escape(db, “[LOG] " … PlyNick … " [” … PlySteamId … "] has spawned " … model )
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … PlyPropSpawn … “’ , NOW())” );
end
end
hook.Add(“PlayerSpawnedProp”, “LogSqlSpawnProp”, LogSqlSpawnProp)

/*--------------------------------------------------------------------------
Log: Player Picked Up Weapon
*/------------------------------------------------ -------------------------
local DontLog = {
“weapon_crowbar”,
“weapon_pistol”,
“weapon_smg1”,
“weapon_frag”,
“weapon_physcannon”,
“weapon_crossbow”,
“weapon_shotgun”,
“weapon_357”,
“weapon_rpg”,
“weapon_ar2”,
“gmod_tool”,
“gmod_camera”,
“weapon_physgun”,
}

local function LogSqlWeapon( ply, weapon )
if log_weapon:GetInt() == 1 then
if !table.HasValue(DontLog , weapon:GetClass()) then
local PlyName = ply:Nick()
local Weapon = weapon:GetClass()
local PlySteamId = ply:SteamID()
local PlyPickUpweapon = mysql.escape(db, “[LOG] " … PlyName … " [” … PlySteamId … "] picked up " … Weapon )
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … PlyPickUpweapon … “’ , NOW())” );
end
end
end
hook.Add( “PlayerCanPickupWeapon”, “LogSqlWeapon”, LogSqlWeapon )

/*--------------------------------------------------------------------------
Log: Player Joined Team
*/------------------------------------------------ -------------------------
local function LogSqlJoinTeam( ply, teamid )
if log_team:GetInt() == 1 then
local PlyNick = ply:Nick()
local PlyTeam = team.GetName(teamid)
local PlySteamId = ply:SteamID()
local PlyJoinTeam = mysql.escape(db, “[LOG] " … PlyNick … " [” … PlySteamId … "] joined team " … PlyTeam )
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … PlyJoinTeam … “’ , NOW())” );
end
end
hook.Add( “PlayerJoinTeam”, “LogSqlJoinTeam”, LogSqlJoinTeam )

/*--------------------------------------------------------------------------
Log: Player Sprayed Spray
*/------------------------------------------------ -------------------------
local function LogSqlSprayed( ply )
if log_spray:GetInt() == 1 then
local PlyNick = ply:Nick()
local PlySteamId = ply:SteamID()
local PlySprayed = mysql.escape(db, “[LOG] " … PlyNick … " [” … PlySteamId … “] sprayed a spray.”)
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … PlySprayed … “’ , NOW())” );
end
end
hook.Add( “PlayerSpray”, “LogSqlSprayed”, LogSqlSprayed )

/*--------------------------------------------------------------------------
Log: Player Used Entity
*/------------------------------------------------ -------------------------
local function LogSqlPlayerUse( ply, ent )
if log_plyuse:GetInt() == 1 then
local PlyNick = ply:Nick()
local PlySteamId = ply:SteamID()
local PlyUsed = mysql.escape(db, “[LOG] " … PlyNick … " [” … PlySteamId … "] used " … ent:GetClass() )
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … PlyUsed … “’ , NOW())” );
end
end
hook.Add( “PlayerUse”, “LogSqlPlayerUse”, LogSqlPlayerUse )

/*--------------------------------------------------------------------------
Log: Player Used Noclip
*/------------------------------------------------ -------------------------
local function LogSqlPlayerNocliped( ply )
if log_noclip:GetInt() == 1 then
local PlyNick = ply:Nick()
local PlySteamId = ply:SteamID()
local PlyNoclip = mysql.escape(db, “[LOG] " … PlyNick … " [” … PlySteamId … "] used noclip. ")
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … PlyNoclip … “’ , NOW())” );
end
end
hook.Add( “PlayerNoClip”, “LogSqlPlayerNocliped”, LogSqlPlayerNocliped )

/*--------------------------------------------------------------------------
Log: Player said
*/------------------------------------------------ -------------------------
local function LogSqlChat( ply, text )
if log_chat:GetInt() == 1 then
local PlyNick = ply:Nick()
local PlyChat = mysql.escape(db, PlyNick … ": " … text)
if ply:Alive() then
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … PlyChat … “’ , NOW())”);
else
mysql.query(db, “INSERT INTO logs (id, log, entered_time) VALUES(0, '” … "DEAD " … PlyChat … “’ , NOW())”);
end
end
end
hook.Add(“PlayerSay”, “LogSqlChat”, LogSqlChat )[/lua]

So it works at first then stops working after time?

Sounds like the connection is dying and I don’t see anywhere where it has any “keep-alive” method or a reconnection system.

Yes it just stops, so what do you think I should add?

@foszor I have never read of a function to keep the connection alive?
as far as I know it should stay connected. (Properly wrong)

I made this, so I’ll keep an eye out if there is a fix posted here.
And the thread is here: http://www.facepunch.com/showthread.php?t=911347

MySQL servers will disconnect any connection that is idle for too long, or even active connections that persist longer than a set interval. It really is 100% up to the server and how the server is configured.

You have a few options: you can query the server on a timer, say every 2 minutes, with a lightweight query that just lets the server know the connection is still active.

Alternatively, you can check if the connection is still alive prior to sending any queries, and if it isn’t, simply reconnect.

Ah, thanks for the info. Ill update it with the fix. Rated useful!