[Release] Simple server side Logs

Hi there, mates.

Sorry, i was in alcohol trip for last two weeks :slight_smile: and i had no good releases.

I hope this one be useful for server owners, who wants to see what happened on server while all admins not in game.

Code

AddEventHandler('serverlog', function(text)
	local gt = os.date('*t')
	local f,err = io.open("serverlog.log","a")
	if not f then return print(err) end
	local h = gt['hour'] if h < 10 then h = "0"..h end
	local m = gt['min'] if m < 10 then m = "0"..m end
	local s = gt['sec'] if s < 10 then s = "0"..s end
	local formattedlog = string.format("[%s:%s:%s] %s \n",h,m,s,text)
	f:write(formattedlog)
	f:close()
	-- uncomment line below, if you need (to show all logs in console also)
	--print(formattedlog)
end)

Copy-paste this code in your serverside script.

How to use

Put this template where you need to log something.

TriggerEvent("serverlog", "Log something")

like that

AddEventHandler('onGameTypeStart', function(resource)
	TriggerEvent("serverlog", "Server has been started")
end)

or like that:

AddEventHandler('playerDropped', function(reason)
	local pname = GetPlayerName(source)
	TriggerEvent("serverlog", pname.. " left the game with reason ("..reason..")")
end)

Also you can use it from client side scripts.
Just
TriggerServerEvent("serverlog", "text_to_log")

Where i can find my logs?

In your root server directory.

File will be created automaticaly. Inside it is looks like this:

Also

No credits required, use it however you want.
I love you all. Thanks.

12 Likes

Thank you again for your work ! Can you include something to set an auto-restart every [x] time ?

Hey,

There’s a ■■■■■■■ for exactly this purpose :slight_smile:
It puts everything on /log (ex http://51.255.47.51:30120/log)
You can read more about it here https://wiki.fivem.net/wiki/■■■■■■■

This is really good for saving the logs tho, Maybe you could do a local HTTP request to save the /log endpoint aswell?

3 Likes

Ok. Nice. But everyone can see this logs via http request like serverip:port/logs, unfortunately.

1 Like

How to change log path ? Because everyone can see server IP in client log file and can view all players IP in XX.XX.XX.XX/log :unamused:
thanks

wtf ip adresse for all client in log with ip server and port in the navigator good security --’

2 Likes

Thanks for this, appreciate it! I modified it a little bit so that console doesn’t get the newline.

AddEventHandler('VxServer:serverLog', function(text)
	local currentDatetime = os.date("%Y-%m-%d %H:%M:%S")
	local logFile,errorReason = io.open("serverLog.log","a")
	if not logFile then return print(errorReason) end
	local formattedLog = string.format("[%s] [VxServer] %s",currentDatetime,text)
	logFile:write(formattedLog.."\n")
	logFile:close()
	-- Print to console
	print(formattedLog)
end)


@potassium How to make this log access PRIVATE please ?

@kyai @Havanna @potassium @MarkViolla Did you guys manage to hide this from public ?
Thanks

Yeah just edit resources/[system]/■■■■■■■ or basevents you have file who sens log with IP adresse just edit that file

Thanks @HavannaPC but this is not exacly what I’m looking for.

I know that I can avoid any call to ■■■■■■■ within those sources.
However I’m looking for a global way to write logs not to console but to a target log file.

In fact, the same way you can do with Nlog.

Any clue ?

(Well perhaps @MarkViolla solution will do.)

is this live logs? because we are looking for a live log

I have seen a different system with a ingame ui but not sure what the name is hehe.

This?

PM ME

Nice way to see some logs - hope Cheleber can point me in the right direction to get a hold on it quite like it.

Hey :slight_smile:
I have one Question, how do i log Hitch Warnings :slight_smile:
i know CitizenFX.log do log it…

But just a question how i do it :slight_smile:

how do i change the folder where logs are saved? i tried io.open(“log\serverLog.log”,“a”) but i got errors

I want to see which player give money to another player.
I use this in es_extended:

TriggerClientEvent(‘esx:showNotification’, _source, _U(‘yougave’) … ’ ~g~$’ … itemCount … _U(‘to’) … targetXPlayer.name)
TriggerClientEvent(‘esx:showNotification’, target, _U(‘youreceived’) … ’ ~g~$’ … itemCount … _U(‘by’) … sourceXPlayer.name)
TriggerEvent(“serverlog”, _source, _U(‘yougave’) … ’ ~g~$’ … itemCount … _U(‘to’) … targetXPlayer.name)
TriggerEvent(“serverlog”, target, _U(‘youreceived’) … ’ ~g~$’ … itemCount … _U(‘by’) … sourceXPlayer.name)
But in log just a date and time, not other information. Could you help me?

this code, i add where?

Where does this come only to the console??
Maybe you should add it for like discord to