Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Assets/EditorTests/Logger/LoggerServiceConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public void Init_WhenJsonExists_LoadsBasicProperties()
{
LoggerServiceConfig config = new LoggerServiceConfig();
config.Init();
Assert.IsTrue(config.ServiceEnabled);
Assert.IsTrue(config.EnableConsole);
Assert.AreEqual("Logger/log4net.xml", config.Log4NetConfigXml);
Assert.AreEqual("DEBUG", config.MinLogLevel);
Assert.IsTrue(config.Settings.ServiceEnabled);
Assert.IsTrue(config.Settings.ConsoleEnabled);
Assert.AreEqual("Logger/log4net.xml", config.Settings.Log4NetConfigXml);
Assert.AreEqual("DEBUG", config.Settings.MinConsoleLogLevel);
}
}
}
4 changes: 2 additions & 2 deletions Assets/EditorTests/Logger/LoggerServiceIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public void DebugLog_WritesToInfoLogFile()
LoggerServiceConfig config = new LoggerServiceConfig();
config.Init();

Assert.IsTrue(config.ServiceEnabled, "ServiceEnabled must be true for this integration test.");
Assert.IsFalse(string.IsNullOrEmpty(config.Log4NetConfigXml), "Log4NetConfigXml must point to a valid XML file.");
Assert.IsTrue(config.Settings.ServiceEnabled, "ServiceEnabled must be true for this integration test.");
Assert.IsFalse(string.IsNullOrEmpty(config.Settings.Log4NetConfigXml), "Log4NetConfigXml must point to a valid XML file.");

string logsDir = config.GetSystemLogsDirectory();
Directory.CreateDirectory(logsDir);
Expand Down
6 changes: 6 additions & 0 deletions Assets/StreamingAssets/DataManagement/app_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"offlineConfigurationFile": "DataManagement/offline_config.json",
"workspacesDirectory": "DataManagement/workspaces",
"remoteConfigurationUrl": "https://example.com/config/getConfig?v=10",
"loggerSettings": {
"serviceEnabled": true,
"minFileLogLevel": "WARN",
"minHttpLogLevel": "WARN",
"forceMinLogLevel": false
},
"translationSettings": {
"locale": "he-IL",
"localFilePath": "Translations/Translations.json",
Expand Down
7 changes: 5 additions & 2 deletions Assets/StreamingAssets/Logger/LoggerConfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"Enabled": true,
"ServiceEnabled": true,
"ConsoleEnabled": true,
"Log4NetConfigXml": "Logger/log4net.xml",
"MinLogLevel": "DEBUG",
"MinConsoleLogLevel": "DEBUG",
"MinFileLogLevel": "DEBUG",
"MinHttpLogLevel": "DEBUG",
"ForceMinLogLevel": false,
"StackTraceRowLimit": 0,
"HttpEndpointUrl": "https://TODO/logs",
"HttpPersistenceDirectory": "C:/Yahalom/logs/offline"
Expand Down
14 changes: 0 additions & 14 deletions Assets/com.mapcolonies.core/Services/LoggerService/Config.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ namespace com.mapcolonies.core.Services.LoggerService
public static class LoggerInitializer
{
private const string AdditionalRevisionSeparator = "f";
private static LoggerService Logger;

public static LoggerService Logger
{
get;
private set;
}

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
public static void Init()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System.IO;
using com.mapcolonies.core.Services.LoggerService.CustomAppenders;
using Cysharp.Threading.Tasks;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using UnityEngine;
using ConsoleAppender = com.mapcolonies.core.Services.LoggerService.CustomAppenders.ConsoleAppender;
using Debug = UnityEngine.Debug;

namespace com.mapcolonies.core.Services.LoggerService
Expand All @@ -17,6 +20,10 @@ public class LoggerService : IDisposable
private const string HttpEndpointUrl = "HttpEndpointUrl";
private const string HttpPersistenceDirectory = "HttpPersistenceDirectory";
private const string Pattern = "%date %-5level %logger - %message%newline";
private const string ConsoleAppenderName = "Console";
private const string FileAppenderName = "File";
private const string HttpAppenderName = "Http";

private readonly LoggerServiceConfig _config;

private ILogHandler _originalUnityLogHandler;
Expand All @@ -26,7 +33,7 @@ public LoggerService(LoggerServiceConfig config)
{
_config = config;

if (!config.ServiceEnabled) return;
if (!_config.Settings.ServiceEnabled) return;

bool success = InitializeLog4Net();

Expand All @@ -47,7 +54,7 @@ private bool InitializeLog4Net()
try
{
_originalUnityLogHandler = Debug.unityLogger.logHandler;
string logConfigFilePath = Path.Combine(Application.streamingAssetsPath, _config.Log4NetConfigXml);
string logConfigFilePath = Path.Combine(Application.streamingAssetsPath, _config.Settings.Log4NetConfigXml);

if (!File.Exists(logConfigFilePath))
{
Expand All @@ -62,13 +69,36 @@ private bool InitializeLog4Net()
}

GlobalContext.Properties[LogFilePath] = logDirectory;
GlobalContext.Properties[HttpEndpointUrl] = _config.HttpEndpointUrl;
GlobalContext.Properties[HttpEndpointUrl] = _config.Settings.HttpEndpointUrl;
GlobalContext.Properties[HttpPersistenceDirectory] = _config.GetHttpPersistenceDirectory();

FileInfo logConfigFile = new FileInfo(logConfigFilePath);
XmlConfigurator.Configure(logConfigFile);

Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
bool isDev = Application.isEditor || Debug.isDebugBuild;

if (!isDev)
{
foreach (IAppender appender in hierarchy.GetAppenders())
{
if (appender is AppenderSkeleton sk)
{
if (appender.Name.Contains(ConsoleAppenderName, StringComparison.OrdinalIgnoreCase))
{
sk.Threshold = hierarchy.LevelMap[_config.Settings.MinConsoleLogLevel] ?? Level.Debug;
}
else
{
sk.Threshold = Level.Debug;
}

sk.ActivateOptions();
}
}

ApplyFileAndHttpThresholds(hierarchy, _config.Settings);
}

ConsoleAppender consoleAppender = new ConsoleAppender(_originalUnityLogHandler);

Expand All @@ -77,13 +107,12 @@ private bool InitializeLog4Net()
layout.ActivateOptions();
consoleAppender.Layout = layout;

Level consoleThreshold = hierarchy.LevelMap[_config.MinLogLevel] ?? Level.Debug;
consoleAppender.Threshold = consoleThreshold;
consoleAppender.Threshold = hierarchy.LevelMap[_config.Settings.MinConsoleLogLevel] ?? Level.Debug;
consoleAppender.ActivateOptions();

hierarchy.Root.AddAppender(consoleAppender);

if (!_config.EnableConsole)
if (!_config.Settings.ConsoleEnabled)
{
hierarchy.Root.RemoveAppender(consoleAppender);
}
Expand Down Expand Up @@ -128,7 +157,7 @@ private bool TryInitializeLogDirectory(out string logDirectory)
success = false;
}

string logConfigFilePath = Path.Combine(Application.streamingAssetsPath, _config.Log4NetConfigXml);
string logConfigFilePath = Path.Combine(Application.streamingAssetsPath, _config.Settings.Log4NetConfigXml);

if (!success || !File.Exists(logConfigFilePath))
{
Expand Down Expand Up @@ -160,5 +189,40 @@ public void Dispose()
_originalUnityLogHandler = null;
}
}

private void ApplyFileAndHttpThresholds(Hierarchy hierarchy, LoggerSettings settings)
{
foreach (IAppender appender in hierarchy.GetAppenders())
{
if (appender is AppenderSkeleton sk)
{
if (appender.Name.Contains(FileAppenderName, StringComparison.OrdinalIgnoreCase) ||
appender is RollingFileAppender)
{
sk.Threshold = hierarchy.LevelMap[settings.MinFileLogLevel] ?? Level.Debug;
}
else if (appender.Name.Contains(HttpAppenderName, StringComparison.OrdinalIgnoreCase) ||
appender is HttpAppender)
{
sk.Threshold = hierarchy.LevelMap[settings.MinHttpLogLevel] ?? Level.Debug;
}

sk.ActivateOptions();
}
}
}

public async UniTask<bool> UpdateLoggerSettings(LoggerSettings settings)
{
if (!settings.ForceMinLogLevel) return false;

Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
bool isDev = Application.isEditor || Debug.isDebugBuild;

if (isDev) return false;

ApplyFileAndHttpThresholds(hierarchy, settings);
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.IO;
using com.mapcolonies.core.Utilities;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;

namespace com.mapcolonies.core.Services.LoggerService
Expand All @@ -7,78 +10,30 @@ public class LoggerServiceConfig
{
private const string JsonFileName = "Logger/LoggerConfig.json";

private Config _config;

public string Log4NetConfigXml
{
get;
private set;
}

public bool ServiceEnabled
{
get;
private set;
}

public bool EnableConsole
{
get;
private set;
}

public string MinLogLevel
{
get;
private set;
}

public string HttpEndpointUrl
{
get;
private set;
}

public string HttpPersistenceDirectory
public LoggerSettings Settings
{
get;
private set;
}

public void Init()
{
string filePath = Path.Combine(Application.streamingAssetsPath, JsonFileName);

if (!File.Exists(filePath))
{
Debug.LogError($"File {JsonFileName} not found at: " + filePath);
return;
}

try
{
string jsonContent = File.ReadAllText(filePath);
_config = JsonUtility.FromJson<Config>(jsonContent);
Settings = JsonUtilityEx.LoadJson<LoggerSettings>(JsonFileName);

if (_config == null)
if (Settings == null)
{
Debug.LogError($"Failed to deserialize {JsonFileName} JSON content.");
return;
}

Log4NetConfigXml = _config.Log4NetConfigXml;
ServiceEnabled = _config.Enabled;
EnableConsole = _config.ConsoleEnabled;
MinLogLevel = _config.MinLogLevel;
HttpEndpointUrl = _config.HttpEndpointUrl;
HttpPersistenceDirectory = _config.HttpPersistenceDirectory;
}
catch (System.Exception ex)
{
Debug.LogError($"Error parsing file: {ex.Message}");
Debug.LogError($"Error loading {JsonFileName}: {ex.Message}");
}
}


public string GetSystemLogsDirectory()
{
if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;

namespace com.mapcolonies.core.Services.LoggerService
{
[Serializable]
public record LoggerSettings
{
public string Log4NetConfigXml
{
get;
set;
}

public int StackTraceRowLimit
{
get;
set;
}

public bool ServiceEnabled
{
get;
set;
}

public bool ConsoleEnabled
{
get;
set;
}

public string MinConsoleLogLevel
{
get;
set;
}

public string MinFileLogLevel
{
get;
set;
}

public string MinHttpLogLevel
{
get;
set;
}

public bool ForceMinLogLevel
{
get;
set;
}

public string HttpEndpointUrl
{
get;
set;
}

public string HttpPersistenceDirectory
{
get;
set;
}
}
}
Loading
Loading