Log.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using XGame.Framework.Logger;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using XGame.Framework;
  6. namespace XGame
  7. {
  8. public static class Log
  9. {
  10. private static ILogger _log;
  11. private static ILogger log => _log ?? (_log = LogCreator.Create());
  12. #region utility
  13. private static string ParseException(System.Exception exception)
  14. {
  15. string result = string.Format("{0}: {1}\nStackTrace: {2}", exception.GetType().FullName, exception.Message, exception.StackTrace);
  16. if (exception.InnerException != null)
  17. {
  18. var innerResult = ParseException(exception.InnerException);
  19. result = string.Format("{0}\n[InnerException]\n{1}", result, innerResult);
  20. }
  21. return result;
  22. }
  23. #endregion
  24. #region Debug
  25. /// <summary>
  26. /// 细粒度级别,用于开发过程中的调试使用,开发中与发布Debug版可见
  27. /// </summary>
  28. /// <param name="format">格式化字符串</param>
  29. /// <param name="args">不定长格式化参数</param>
  30. [Conditional(MacroDefine.UNITY_EDITOR)]
  31. #if !PROFILER
  32. [Conditional(MacroDefine.DEBUG)]
  33. #endif
  34. public static void Debug(string format, params object[] args)
  35. {
  36. var result = log.Log(LogLevel.Debug, format, args);
  37. UnityEngine.Debug.Log(result);
  38. }
  39. /// <summary>
  40. /// 细粒度级别,用于开发过程中的调试使用,开发中与发布Debug版可见
  41. /// </summary>
  42. /// <param name="format">格式化字符串</param>
  43. /// <param name="args">不定长格式化参数</param>
  44. [Conditional(MacroDefine.UNITY_EDITOR)]
  45. #if !PROFILER
  46. [Conditional(MacroDefine.DEBUG)]
  47. #endif
  48. public static void Debug(object obj)
  49. {
  50. var result = log.Log(LogLevel.Debug, obj.ToString());
  51. UnityEngine.Debug.Log(result);
  52. }
  53. #endregion
  54. #region Info
  55. /// <summary>
  56. /// 粗粒度级别,用于运行过程中突出强调应用程序的运行过程(eg:LogSystem init success/fail),所有版本可见
  57. /// </summary>
  58. /// <param name="format">格式化字符串</param>
  59. /// <param name="args">不定长格式化参数</param>
  60. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
  61. public static void Info(string format, params object[] args)
  62. {
  63. var result = log.Log(LogLevel.Info, format, args);
  64. UnityEngine.Debug.Log(result);
  65. }
  66. /// <summary>
  67. /// 粗粒度级别,用于运行过程中突出强调应用程序的运行过程(eg:LogSystem init success/fail),所有版本可见
  68. /// </summary>
  69. /// <param name="format">格式化字符串</param>
  70. /// <param name="args">不定长格式化参数</param>
  71. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
  72. public static void Info(object obj)
  73. {
  74. var result = log.Log(LogLevel.Info, obj.ToString());
  75. UnityEngine.Debug.Log(result);
  76. }
  77. #endregion
  78. #region Warn
  79. /// <summary>
  80. /// 粗粒度级别,用于运行过程中表明会出现潜在错误的情形,开发中与发布Debug版可见
  81. /// </summary>
  82. /// <param name="format">格式化字符串</param>
  83. /// <param name="args">不定长格式化参数</param>
  84. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
  85. public static void Warn(string format, params object[] args)
  86. {
  87. var result = log.Log(LogLevel.Warn, format, args);
  88. UnityEngine.Debug.LogWarning(result);
  89. }
  90. /// <summary>
  91. /// 粗粒度级别,用于运行过程中表明会出现潜在错误的情形,开发中与发布Debug版可见
  92. /// </summary>
  93. /// <param name="format">格式化字符串</param>
  94. /// <param name="args">不定长格式化参数</param>
  95. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
  96. public static void Warn(object obj)
  97. {
  98. var result = log.Log(LogLevel.Warn, obj.ToString());
  99. UnityEngine.Debug.LogWarning(result);
  100. }
  101. /// <summary>
  102. /// 粗粒度级别,用于运行过程中表明会出现潜在错误的情形,开发中与发布Debug版可见
  103. /// </summary>
  104. /// <param name="format">格式化字符串</param>
  105. /// <param name="args">不定长格式化参数</param>
  106. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
  107. public static void WarnException(System.Exception exception)
  108. {
  109. WarnException(null, exception);
  110. }
  111. /// <summary>
  112. /// 粗粒度级别,用于运行过程中表明会出现潜在错误的情形,开发中与发布Debug版可见
  113. /// </summary>
  114. /// <param name="format">格式化字符串</param>
  115. /// <param name="args">不定长格式化参数</param>
  116. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
  117. public static void WarnException(string prefix, System.Exception exception)
  118. {
  119. if (null == exception)
  120. {
  121. Warn("{0} Exception is null.", prefix);
  122. return;
  123. }
  124. string exceptionStr = ParseException(exception);
  125. Warn("{0} {1}", prefix, exceptionStr);
  126. }
  127. #endregion
  128. #region Error
  129. /// <summary>
  130. /// 粗粒度级别,用于运行过程中虽然发生错误事件,但仍然不影响系统的继续运行,所有版本可见
  131. /// </summary>
  132. /// <param name="format">格式化字符串</param>
  133. /// <param name="args">不定长格式化参数</param>
  134. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
  135. public static void Error(string format, params object[] args)
  136. {
  137. var result = log.Log(LogLevel.Error, format, args);
  138. UnityEngine.Debug.LogError(result);
  139. }
  140. /// <summary>
  141. /// 粗粒度级别,用于运行过程中虽然发生错误事件,但仍然不影响系统的继续运行,所有版本可见
  142. /// </summary>
  143. /// <param name="format">格式化字符串</param>
  144. /// <param name="args">不定长格式化参数</param>
  145. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
  146. public static void Error(object obj)
  147. {
  148. var result = log.Log(LogLevel.Error, obj.ToString());
  149. UnityEngine.Debug.LogError(result);
  150. }
  151. #endregion
  152. #region Exception
  153. /// <summary>
  154. /// 粗粒度级别,用于运行过程中指出每个严重的错误事件将会导致应用程序的退出,将上报后端保存,所有版本可见
  155. /// 级别与Error相当
  156. /// </summary>
  157. /// <param name="format">格式化字符串</param>
  158. /// <param name="args">不定长格式化参数</param>
  159. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
  160. public static void Exception(System.Exception exception)
  161. {
  162. Exception(null, exception);
  163. }
  164. /// <summary>
  165. /// 粗粒度级别,用于运行过程中指出每个严重的错误事件将会导致应用程序的退出,将上报后端保存,所有版本可见
  166. /// 级别与Error相当
  167. /// </summary>
  168. /// <param name="format">格式化字符串</param>
  169. /// <param name="args">不定长格式化参数</param>
  170. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
  171. public static void Exception(string prefix, System.Exception exception)
  172. {
  173. if (null == exception)
  174. {
  175. Log.Error("{0} Exception is null.", prefix);
  176. return;
  177. }
  178. string exceptionStr = ParseException(exception);
  179. var result = log.Log(LogLevel.Exception, $"{prefix} {exceptionStr}");
  180. UnityEngine.Debug.LogError(result);
  181. }
  182. #endregion
  183. #region 埋点接口
  184. /// <summary>
  185. /// 埋点接口 缓存至本地-实时上传至后端-多用于数据分析统计
  186. /// </summary>
  187. /// <param name="format">格式化字符串</param>
  188. /// <param name="args">不定长格式化参数</param>
  189. [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.FINAL_RELEASE)]
  190. public static void Event(string type, string id, Dictionary<string, string> infos)
  191. {
  192. var result = log.Event(type, id, infos);
  193. UnityEngine.Debug.Log(result);
  194. }
  195. #endregion
  196. #region 其他
  197. /// <summary>
  198. /// 日志是否将会保存至本地缓存
  199. /// 用于日志上传模块判断是否需要开启上传
  200. /// </summary>
  201. public static bool IsSaveLocalCache()
  202. {
  203. return _log is ILogFlushable;
  204. }
  205. public static void Initialize()
  206. {
  207. log.Initialize();
  208. }
  209. public static void Dispose()
  210. {
  211. (_log as IDisposable)?.Dispose();
  212. _log = null;
  213. }
  214. /// <summary>
  215. /// 强制写入日志
  216. /// </summary>
  217. internal static void Flush(int type)
  218. {
  219. (_log as ILogFlushable)?.Flush((LogType)type);
  220. }
  221. #endregion
  222. }
  223. }