Files
SPT-Server-Build/Libraries/SPTarkov.Server.Core/Utils/Logger/SptLogger.cs
T
Jesse 687b4f7a49 Add blazor & MVC Support (#602)
* Add initial code for Razor pages support

* Remove finalizer

* Try fully loading blazor

This is most likely entirely broken because of a rebase now

* UseSptBlazor after app.Use

* Fix up StaticWebAsset loading, add MudBlazor

* Implement page

* Update comment

* Replaced existing status page with razor

* Track background video in LFS

* Update attributes

* Improved status page theming

* Fix up wwwroot publish folder to SPT_Data/wwwroot

* Added name to page

* Remove unnecessary code

* Begin fixing up MVC & Blazor for modding

* Update TestMod

* Cleanup todo

* Further work out mod support

* Re-order initialization and use logger

* Rename library to SPTarkov.Server.Web

---------

Co-authored-by: Chomp <dev@dev.sp-tarkov.com>
Co-authored-by: Chomp <27521899+chompDev@users.noreply.github.com>
2025-10-02 19:03:27 +00:00

215 lines
5.9 KiB
C#

using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Models.Logging;
using SPTarkov.Server.Core.Models.Utils;
using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel;
namespace SPTarkov.Server.Core.Utils.Logger;
[Injectable(TypePriority = int.MinValue)]
public class SptLogger<T> : ISptLogger<T>, IDisposable
{
private string _category;
private readonly SptLoggerQueueManager _loggerQueueManager;
private const string ConfigurationPath = "./sptLogger.json";
private const string ConfigurationPathDev = "./sptLogger.Development.json";
private SptLoggerConfiguration _config;
public SptLogger(FileUtil fileUtil, JsonUtil jsonUtil, SptLoggerQueueManager loggerQueueManager)
{
_category = typeof(T).FullName;
_loggerQueueManager = loggerQueueManager;
bool IsReleaseType = ProgramStatics.ENTRY_TYPE() switch
{
Models.Enums.EntryType.BLEEDINGEDGEMODS => true,
Models.Enums.EntryType.RELEASE => true,
_ => false,
};
if (!ProgramStatics.DEBUG() || IsReleaseType)
{
LoadConfig(fileUtil, jsonUtil, ConfigurationPath);
}
else
{
LoadConfig(fileUtil, jsonUtil, ConfigurationPathDev);
}
if (_config == null)
{
throw new Exception("The configuration path was loaded but it contained invalid or incorrect configuration.");
}
_loggerQueueManager.Initialize(_config);
}
private void LoadConfig(FileUtil fileUtil, JsonUtil jsonUtil, string sptLoggerJson)
{
if (fileUtil.FileExists(sptLoggerJson))
{
_config = jsonUtil.DeserializeFromFile<SptLoggerConfiguration>(sptLoggerJson);
}
else
{
throw new Exception($"Unable to find SPTLogger file '{sptLoggerJson}'");
}
}
public void OverrideCategory(string category)
{
_category = category;
}
public void LogWithColor(string data, LogTextColor? textColor = null, LogBackgroundColor? backgroundColor = null, Exception? ex = null)
{
_loggerQueueManager.EnqueueMessage(
new SptLogMessage(
_category,
DateTime.UtcNow,
LogLevel.Info,
Environment.CurrentManagedThreadId,
Thread.CurrentThread.Name,
data,
ex,
textColor,
backgroundColor
)
);
}
public void Success(string data, Exception? ex = null)
{
_loggerQueueManager.EnqueueMessage(
new SptLogMessage(
_category,
DateTime.UtcNow,
LogLevel.Info,
Environment.CurrentManagedThreadId,
Thread.CurrentThread.Name,
data,
ex,
LogTextColor.Green
)
);
}
public void Error(string data, Exception? ex = null)
{
_loggerQueueManager.EnqueueMessage(
new SptLogMessage(
_category,
DateTime.UtcNow,
LogLevel.Error,
Environment.CurrentManagedThreadId,
Thread.CurrentThread.Name,
data,
ex,
LogTextColor.Red
)
);
}
public void Warning(string data, Exception? ex = null)
{
_loggerQueueManager.EnqueueMessage(
new SptLogMessage(
_category,
DateTime.UtcNow,
LogLevel.Warn,
Environment.CurrentManagedThreadId,
Thread.CurrentThread.Name,
data,
ex,
LogTextColor.Yellow
)
);
}
public void Info(string data, Exception? ex = null)
{
_loggerQueueManager.EnqueueMessage(
new SptLogMessage(
_category,
DateTime.UtcNow,
LogLevel.Info,
Environment.CurrentManagedThreadId,
Thread.CurrentThread.Name,
data,
ex
)
);
}
public void Debug(string data, Exception? ex = null)
{
_loggerQueueManager.EnqueueMessage(
new SptLogMessage(
_category,
DateTime.UtcNow,
LogLevel.Debug,
Environment.CurrentManagedThreadId,
Thread.CurrentThread.Name,
data,
ex,
LogTextColor.Gray
)
);
}
public void Critical(string data, Exception? ex = null)
{
_loggerQueueManager.EnqueueMessage(
new SptLogMessage(
_category,
DateTime.UtcNow,
LogLevel.Fatal,
Environment.CurrentManagedThreadId,
Thread.CurrentThread.Name,
data,
ex,
LogTextColor.Black,
LogBackgroundColor.Red
)
);
}
public void Log(
LogLevel level,
string data,
LogTextColor? textColor = null,
LogBackgroundColor? backgroundColor = null,
Exception? ex = null
)
{
_loggerQueueManager.EnqueueMessage(
new SptLogMessage(
_category,
DateTime.UtcNow,
level,
Environment.CurrentManagedThreadId,
Thread.CurrentThread.Name,
data,
ex,
textColor,
backgroundColor
)
);
}
public bool IsLogEnabled(LogLevel level)
{
return _config.Loggers.Any(l => l.LogLevel.CanLog(level));
}
public void DumpAndStop()
{
_loggerQueueManager.DumpAndStop();
}
public void Dispose()
{
_loggerQueueManager.DumpAndStop();
}
}