diff --git a/ModTek/Features/Logging/LoggingFeature.cs b/ModTek/Features/Logging/LoggingFeature.cs index afa533e..b2330f6 100644 --- a/ModTek/Features/Logging/LoggingFeature.cs +++ b/ModTek/Features/Logging/LoggingFeature.cs @@ -13,7 +13,7 @@ internal static class LoggingFeature private static AppenderUnityConsole _consoleLog; private static AppenderFile _mainLog; - private static AppenderFile[] _logsAppenders = Array.Empty(); + private static AppenderFile[] _logsAppenders = []; private static MTLoggerAsyncQueue _queue; @@ -76,6 +76,16 @@ internal static void AddAppenders(string basePath, Dictionary Logs { get; set; } + + // simplified dedicated log file setup for mods + [JsonProperty] + internal const string Log_Description = "Allows to define a log relative to the mods directory, that will contain a copy of all log statements for the specified logger."; + [JsonProperty] + internal ModLogSettings Log { get; set; } + internal class ModLogSettings + { + [JsonProperty(Required = Required.Always)] + internal string FilePath { get; set; } + [JsonProperty(Required = Required.Always)] + internal string LoggerName { get; set; } + } [JsonIgnore] public bool LoadFail { get; set; } @@ -141,7 +156,7 @@ internal void SaveState() var modStatePath = Path.Combine(Directory, ModTek.MOD_STATE_JSON_NAME); var state = new ModState(); state.Enabled = Enabled; - Log.Main.Info?.Log("\t\twriting to FS:" + QuotedName + "->" + state.Enabled); + global::ModTek.Log.Main.Info?.Log("\t\twriting to FS:" + QuotedName + "->" + state.Enabled); state.SaveToPath(modStatePath); } diff --git a/doc/LOGGING.md b/doc/LOGGING.md index 8cdffda..69e15e4 100644 --- a/doc/LOGGING.md +++ b/doc/LOGGING.md @@ -6,7 +6,7 @@ ModTek logging has the following features: - Provides filter options to determine what does or does not get into the log. - Formats a log message so that it includes a logger name, nicely formatted time, thread id if not on main thread, log message and possibly an exception. - Uses a high precision timer, to allow 1/10 of microsecond precision (10^-7) for time stamps instead of milliseconds (10^-3). -- Supports adding additional log files. (TODO allow mods to define the log files directly in mod.json, right now only possible via `ModTek/config.json`) +- Supports adding additional log files through `ModTek/config.json`. - Added more log levels, use "200" for Trace logging and "250" for Fatal logging. In C# enums are ints, so casting a integer to HBS' LogLevel is valid, `(LogLevel)200`. - Log rotation per game start. Logs are rotated to survive at least one other start, logs that end with `.1` are from a previous application start. @@ -68,8 +68,26 @@ In the advanced merge json example from before, set 200 as the log level for you } ``` +### Mod Local Logs + +> **Note** +> It is not recommended to use this feature as it produces duplicate IO + +If a mod author wants to have a separate copy of all logs a logger produces, one can enable a separate log in `mod.json`: +```json +{ + "Log": { + "FilePath": "log.txt", + "LoggerName": "YourMod" + } +} +``` + ### Nullable loggers +> **Note** +> Nullable loggers is an advanced feature only necesary if one takes the time to use it + Another way of enhancing performance is using nullable loggers. This is only for advanced users requiring lots of performance while still wanting readable code. In C# one can use Null-conditional operators to check for null and skip code from executing. @@ -109,3 +127,4 @@ if (nullableLogger != null) There is a caveat, the game changes log levels after the mods have initialized, so in order to keep up there is a Harmony patch required and some boilerplate code. For the boilerplate code, see [NullableLogger.cs](../ModTek/NullableLogger.cs) and [Log.cs](../ModTek/Log.cs) as used by ModTek itself. +ModTek comments out some parts in `NullableLogger.cs` and replaces them with internal calls, these would need to be reverted back.