otland / forgottenserver

A free and open-source MMORPG server emulator written in C++
https://otland.net
GNU General Public License v2.0
1.56k stars 1.04k forks source link

[Feature] Logger #4688

Closed ArturKnopik closed 2 months ago

ArturKnopik commented 2 months ago

Pull Request Prelude

Changes Proposed

Logger that can handle LUA and C++ calls.

logger.conf - logger config file, not required

#log level (debug, info, warning, error, fatal), default warning
log_level=warning
#max log files, default 2
max_log_files=2
#max size for single log file in bytes, default 1048576(1MB)
log_file_size=1048576
#log file name without ".log", default "tfs"
log_file_name=tfs
#the maximum length of the file address that will be printed in log message, default 35 characters
file_adress_size=35
local logger = GlobalEvent("loggerTest")
function logger.onStartup()
    logD("debug msg")
    logI("info msg")
    logW("warning msg")
    logE("error msg")
    logF("fatal msg")
end
logger:register()

image

Issues addressed: Lack of true logger.

ArturKnopik commented 2 months ago

todo: missing month

Shawak commented 2 months ago

I really dislike the fact that it doesn't even prints error messages on stderr. Also the output looks kinda ugly, especially with these _E_, _F_, etc.

I would probably also replace fatal with trace.

ranisalt commented 2 months ago

What's the purpose of making it a separate thread? I would rather keep it simple and just wrap around Boost.Log, which is already thread-safe.

ArturKnopik commented 2 months ago

clarification: 1) why no boost.log/spdlog - I didn't want to add another dependency 2) message format - I use the format that is used in my work (the project is over 25 years old), it works quite well, this can be changed if someone defines what it should look like 3) why separated thread: the file open + write operation is expensive, in many cases the file will be opened every time we log, I don't want to block the main thread by writing logs etc. 4) why a separate configuration file for logger: config.lua is loaded relatively late and I want to have a logger from the very beginning 5) why logF instead Log.fatal("fatal") etc: I want it to be short and quick to write logs

ranisalt commented 2 months ago

why separated thread: the file open + write operation is expensive, in many cases the file will be opened every time we log, I don't want to block the main thread by writing logs etc.

It would be better than to write to stdout or stderr and pipe to a file if necessary. So it stays open, managed by the OS

why a separate configuration file for logger: config.lua is loaded relatively late and I want to have a logger from the very beginning

How about using environment variables for that? So you don't need to read any file

ArturKnopik commented 2 months ago

It would be better than to write to stdout or stderr and pipe to a file if necessary. So it stays open, managed by the OS

In my opinion, the log size limit is quite significant feature