Skip to content

Commit

Permalink
feat: Creating method to replace all log handlers (#1880)
Browse files Browse the repository at this point in the history
* adding ReplaceLogHandler to LogFactory

* property to get all loggers
  • Loading branch information
James-Frowen committed May 11, 2020
1 parent 30a610d commit d8aaf76
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions Assets/Mirror/Runtime/Logging/LogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ public static class LogFactory
{
internal static readonly Dictionary<string, ILogger> loggers = new Dictionary<string, ILogger>();

public static Dictionary<string, ILogger>.ValueCollection AllLoggers => loggers.Values;

/// <summary>
/// logHandler used for new loggers
/// </summary>
static ILogHandler defaultLogHandler = Debug.unityLogger;

/// <summary>
/// if true sets all log level to LogType.Log
/// </summary>
static bool debugMode = false;

public static ILogger GetLogger<T>(LogType defaultLogLevel = LogType.Warning)
{
return GetLogger(typeof(T).Name, defaultLogLevel);
Expand All @@ -24,7 +36,7 @@ public static ILogger GetLogger(string loggerName, LogType defaultLogLevel = Log
return logger;
}

logger = new Logger(Debug.unityLogger)
logger = new Logger(defaultLogHandler)
{
// by default, log warnings and up
filterLogType = debugMode ? LogType.Log : defaultLogLevel
Expand All @@ -34,18 +46,30 @@ public static ILogger GetLogger(string loggerName, LogType defaultLogLevel = Log
return logger;
}


static bool debugMode = false;
/// <summary>
/// Makes all log levels LogType.Log, this is so that NetworkManger.showDebugMessages can still be used
/// </summary>
internal static void EnableDebugMode()
{
debugMode = true;

foreach (KeyValuePair<string, ILogger> kvp in loggers)
foreach (ILogger logger in loggers.Values)
{
logger.filterLogType = LogType.Log;
}
}

/// <summary>
/// Replacing log handler for all existing loggers and sets defaultLogHandler for new loggers
/// </summary>
/// <param name="logHandler"></param>
public static void ReplaceLogHandler(ILogHandler logHandler)
{
defaultLogHandler = logHandler;

foreach (ILogger logger in loggers.Values)
{
kvp.Value.filterLogType = LogType.Log;
logger.logHandler = logHandler;
}
}
}
Expand Down

0 comments on commit d8aaf76

Please sign in to comment.