123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using XGame.Framework.Logger;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using XGame.Framework;
- namespace XGame
- {
- public static class Log
- {
- private static ILogger _log;
- private static ILogger log => _log ?? (_log = LogCreator.Create());
- #region utility
- private static string ParseException(System.Exception exception)
- {
- string result = string.Format("{0}: {1}\nStackTrace: {2}", exception.GetType().FullName, exception.Message, exception.StackTrace);
- if (exception.InnerException != null)
- {
- var innerResult = ParseException(exception.InnerException);
- result = string.Format("{0}\n[InnerException]\n{1}", result, innerResult);
- }
- return result;
- }
- #endregion
- #region Debug
- /// <summary>
- /// 细粒度级别,用于开发过程中的调试使用,开发中与发布Debug版可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR)]
- #if !PROFILER
- [Conditional(MacroDefine.DEBUG)]
- #endif
- public static void Debug(string format, params object[] args)
- {
- var result = log.Log(LogLevel.Debug, format, args);
- UnityEngine.Debug.Log(result);
- }
- /// <summary>
- /// 细粒度级别,用于开发过程中的调试使用,开发中与发布Debug版可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR)]
- #if !PROFILER
- [Conditional(MacroDefine.DEBUG)]
- #endif
- public static void Debug(object obj)
- {
- var result = log.Log(LogLevel.Debug, obj.ToString());
- UnityEngine.Debug.Log(result);
- }
- #endregion
- #region Info
- /// <summary>
- /// 粗粒度级别,用于运行过程中突出强调应用程序的运行过程(eg:LogSystem init success/fail),所有版本可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
- public static void Info(string format, params object[] args)
- {
- var result = log.Log(LogLevel.Info, format, args);
- UnityEngine.Debug.Log(result);
- }
- /// <summary>
- /// 粗粒度级别,用于运行过程中突出强调应用程序的运行过程(eg:LogSystem init success/fail),所有版本可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
- public static void Info(object obj)
- {
- var result = log.Log(LogLevel.Info, obj.ToString());
- UnityEngine.Debug.Log(result);
- }
- #endregion
- #region Warn
- /// <summary>
- /// 粗粒度级别,用于运行过程中表明会出现潜在错误的情形,开发中与发布Debug版可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
- public static void Warn(string format, params object[] args)
- {
- var result = log.Log(LogLevel.Warn, format, args);
- UnityEngine.Debug.LogWarning(result);
- }
- /// <summary>
- /// 粗粒度级别,用于运行过程中表明会出现潜在错误的情形,开发中与发布Debug版可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
- public static void Warn(object obj)
- {
- var result = log.Log(LogLevel.Warn, obj.ToString());
- UnityEngine.Debug.LogWarning(result);
- }
- /// <summary>
- /// 粗粒度级别,用于运行过程中表明会出现潜在错误的情形,开发中与发布Debug版可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
- public static void WarnException(System.Exception exception)
- {
- WarnException(null, exception);
- }
- /// <summary>
- /// 粗粒度级别,用于运行过程中表明会出现潜在错误的情形,开发中与发布Debug版可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER)]
- public static void WarnException(string prefix, System.Exception exception)
- {
- if (null == exception)
- {
- Warn("{0} Exception is null.", prefix);
- return;
- }
- string exceptionStr = ParseException(exception);
- Warn("{0} {1}", prefix, exceptionStr);
- }
- #endregion
- #region Error
- /// <summary>
- /// 粗粒度级别,用于运行过程中虽然发生错误事件,但仍然不影响系统的继续运行,所有版本可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
- public static void Error(string format, params object[] args)
- {
- var result = log.Log(LogLevel.Error, format, args);
- UnityEngine.Debug.LogError(result);
- }
- /// <summary>
- /// 粗粒度级别,用于运行过程中虽然发生错误事件,但仍然不影响系统的继续运行,所有版本可见
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
- public static void Error(object obj)
- {
- var result = log.Log(LogLevel.Error, obj.ToString());
- UnityEngine.Debug.LogError(result);
- }
- #endregion
- #region Exception
- /// <summary>
- /// 粗粒度级别,用于运行过程中指出每个严重的错误事件将会导致应用程序的退出,将上报后端保存,所有版本可见
- /// 级别与Error相当
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
- public static void Exception(System.Exception exception)
- {
- Exception(null, exception);
- }
- /// <summary>
- /// 粗粒度级别,用于运行过程中指出每个严重的错误事件将会导致应用程序的退出,将上报后端保存,所有版本可见
- /// 级别与Error相当
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.PROFILER), Conditional(MacroDefine.FINAL_RELEASE)]
- public static void Exception(string prefix, System.Exception exception)
- {
- if (null == exception)
- {
- Log.Error("{0} Exception is null.", prefix);
- return;
- }
- string exceptionStr = ParseException(exception);
- var result = log.Log(LogLevel.Exception, $"{prefix} {exceptionStr}");
- UnityEngine.Debug.LogError(result);
- }
- #endregion
- #region 埋点接口
- /// <summary>
- /// 埋点接口 缓存至本地-实时上传至后端-多用于数据分析统计
- /// </summary>
- /// <param name="format">格式化字符串</param>
- /// <param name="args">不定长格式化参数</param>
- [Conditional(MacroDefine.UNITY_EDITOR), Conditional(MacroDefine.DEBUG), Conditional(MacroDefine.FINAL_RELEASE)]
- public static void Event(string type, string id, Dictionary<string, string> infos)
- {
- var result = log.Event(type, id, infos);
- UnityEngine.Debug.Log(result);
- }
- #endregion
- #region 其他
- /// <summary>
- /// 日志是否将会保存至本地缓存
- /// 用于日志上传模块判断是否需要开启上传
- /// </summary>
- public static bool IsSaveLocalCache()
- {
- return _log is ILogFlushable;
- }
- public static void Initialize()
- {
- log.Initialize();
- }
- public static void Dispose()
- {
- (_log as IDisposable)?.Dispose();
- _log = null;
- }
- /// <summary>
- /// 强制写入日志
- /// </summary>
- internal static void Flush(int type)
- {
- (_log as ILogFlushable)?.Flush((LogType)type);
- }
- #endregion
- }
- }
|