Log4sp is a powerful SourceMod extension that provides SourceMod plugin developers with a high-performance, feature-rich, and easy-to-use logging API.
With log4sp, plugin developers no longer need to write complex logging code, so they can focus more on the core function development of the plugin.
-
Very fast, much faster than SourceMod API - Logging
-
spdlog benchmarks
-
log4sp benchmarks
-
SourceMod API - Logging benchmarks
-
-
Support custom log filtering.
-
For test, using lower log level (e.g.,
trace
,debug
) can increase log messages and help find problems. -
For release, using higher log level (e.g.,
warn
,error
) can reduce log messages and thus improve performance.
-
-
Support custom log message pattern.
-
Custom information can be appended to log messages. (such as time, log level, source code location, etc.)
-
Specify the style of the log message and append more information (such as time, log level, source code location, etc.)
-
Default log message pattern:
[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
[2024-08-01 12:34:56:789] [log4sp] [info] [example.sp:123] message
-
All log message pattern flags: spdlog wiki
-
-
Support custom log message flush level.
-
By default, the log4sp extension flush the log buffer when it sees fit in order to achieve good performance.
-
Logger.Flush()
can be manually flushed. -
Logger.FlushOn()
can set the minimum log level that will trigger automatic flush.
-
-
Support "backtrace" log messages.
- store
trace
anddebug
messages in a ring buffer and display them later on demand.
- store
-
Support server console commands.
- The server console command "sm log4sp" can dynamically modify the log level, flush level, log pattern, backtrace, etc.
-
Support for "unlimited size" logging messages.
-
For
Logger.LogAmxTpl()
method, the maximum length of the log message is 2048 characters, and the excess will be truncated. -
For
Logger.Log()
andLogger.LogEx()
methods, the length of the log message is not limited (theoretically subject to available memory)
-
-
Supports logging to multiple sinks at once.
-
Each Logger can have multiple Sinks.
-
Each Sink can customize different log level and log pattern.
For example: When the Logger has
ServerConsoleSink
andDailyFileSink
, it is equivalent to LogMessage whensv_logecho 1
.
-
-
Various log targets
-
BaseFileSink (Similar to LogToFile)
-
CallbackSink
-
ClientChatSink(Similar to PrintToChat)
-
ClientConsoleSink(Similar to PrintToConsole)
-
DailyFileSink(Similar to LogMessage, Rotate log files based on date.)
-
RotatingFileSink(Similar to LogMessage, Rotate log files based on file size.)
-
ServerConsoleSink(Similar to PrintToServer)
-
Documentation can be found in the wiki pages.
The log4sp
extension should work for all games on Linux and Windows.
Test platform: Windows 11 + VMware + Ubuntu 24.04 LTS + SourceMod 1.13.0.7178
Host configuration: AMD Ryzen 7 7840HS + 32 GB Memory
VM Ubuntu configuration: 1 CPU + 8 kernel + 8 GB Memory
Test case: benchmark-log4sp.sp
[benchmark] base-file | Iters 1000000 | Elapsed 0.465 secs 2150463/sec
[benchmark] daily-file | Iters 1000000 | Elapsed 0.471 secs 2118729/sec
[benchmark] rotating-file | Iters 1000000 | Elapsed 0.482 secs 2073553/sec
[benchmark] server-console | Iters 1000000 | Elapsed 4.847 secs 206288/sec
As a reference, sourcemod logging API was also tested
Test case: benchmark-sm-logging.sp
[benchmark] LogMessage | Iters 1000000 | Elapsed 9.657 secs 103548/sec
[benchmark] LogToFile | Iters 1000000 | Elapsed 8.070 secs 123903/sec
[benchmark] LogToFileEx | Iters 1000000 | Elapsed 7.959 secs 125637/sec
[benchmark] PrintToServer | Iters 1000000 | Elapsed 4.718 secs 211920/sec
-
gabime spdlog library implements most of the functionality, log4sp.ext wraps the spdlog API for SourcePawn to use
-
Fyren, nosoop, Deathreus provides solution for managing the Sink Handle
-
blueblur0730, Digby helped improve the traversal operation of all loggers
If I missed anyone, please contact me.