Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osmanzeki/148651628adb73dd7496cd63d02e6693 to your computer and use it in GitHub Desktop.
Save osmanzeki/148651628adb73dd7496cd63d02e6693 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using UnityEngine;
namespace NoSuchStudio.Common {
/// <summary>
/// Base class for MonoBehaviours that should have a separate logger. Useful for filtering
/// logs by class types.
/// </summary>
/// <remarks>
/// This class keeps track of all types that inherit it and creates a <see cref="UnityEngine.Logger"/>
/// for each.
/// Any messages logged through the helper methods will have the class name prepended to the message.
/// <code>
/// // will print "[MyClass] Hello World!"
/// MonoBehaviourWithLogger.LogLog&lt;MyClass&gt;("Hello World!");
/// </code>
/// Using sample code like below, you can filter your logs by class.
/// <code>
/// MonoBehaviourWithLogger.GetLoggerByType&lt;SubclassOfMonoBehaviourWithLogger&gt;().Item1.filterLogType = LogType.Error;
/// </code>
/// </remarks>
public abstract class MonoBehaviourWithLogger : MonoBehaviour {
public static readonly Dictionary<Type, (Logger, string)> loggers = new Dictionary<Type, (Logger, string)>();
public Logger logger {
get {
Type thisType = GetType();
return GetLoggerByType(thisType).logger;
}
}
public string logTag {
get {
Type thisType = GetType();
return GetLoggerByType(thisType).logTag;
}
}
protected void LogLog(string format, params object[] args) {
logger.LogFormat(LogType.Log, string.Format("{0} {1}", logTag, format), args);
}
protected void LogWarn(string format, params object[] args) {
logger.LogFormat(LogType.Warning, string.Format("{0} {1}", logTag, format), args);
}
protected void LogError(string format, params object[] args) {
logger.LogFormat(LogType.Error, string.Format("{0} {1}", logTag, format), args);
}
private static void AddType(Type type) {
if (!loggers.ContainsKey(type)) {
loggers.Add(type, (new Logger(Debug.unityLogger.logHandler), string.Format("[{0}]", type.Name)));
}
}
public static (Logger logger, string logTag) GetLoggerByType(Type type) {
AddType(type);
return loggers[type];
}
public static (Logger logger, string logTag) GetLoggerByType<T>() {
return GetLoggerByType(typeof(T));
}
protected static void LogLog<T>(string format, params object[] args) {
(Logger logger, string LogTag) = GetLoggerByType<T>();
logger.LogFormat(LogType.Log, string.Format("{0} {1}", LogTag, format), args);
}
protected static void LogWarn<T>(string format, params object[] args) {
(Logger logger, string LogTag) = GetLoggerByType<T>();
logger.LogFormat(LogType.Warning, string.Format("{0} {1}", LogTag, format), args);
}
protected static void LogError<T>(string format, params object[] args) {
(Logger logger, string LogTag) = GetLoggerByType<T>();
logger.LogFormat(LogType.Error, string.Format("{0} {1}", LogTag, format), args);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment