Unity 自定义Logger

前言

在开发Unity的时候,经常需要Debug.Log()来输出Log.但在开发过程中,采用这种方式来输出日志会有很多痛点,主要体现在以下方面:
在游戏发布正式包的时候 产生的Log不能关闭. 一定要逐个注释或取消才行

统一管理Log显示级别

对于问题1, 可以采用将Debug.Log() 包一层的方法. 并自定义显示级别来过滤.

日志显示级别定义

  public enum Loglevels
    {
        /// <summary>
        /// All message will be logged.
        /// </summary>
        All,

        /// <summary>
        /// Only Informations and above will be logged.
        /// </summary>
        Information,

        /// <summary>
        /// Only Warnings and above will be logged.
        /// </summary>
        Warning,

        /// <summary>
        /// Only Errors and above will be logged.
        /// </summary>
        Error,

        /// <summary>
        /// Only Exceptions will be logged.
        /// </summary>
        Exception,

        /// <summary>
        /// No logging will be occur.
        /// </summary>
        None
    }

日志显示接口

  public interface ILogger
    {
        /// <summary>
        /// The minimum severity to log
        /// </summary>
        Loglevels Level { get; set; }
        string FormatVerbose { get; set; }
        string FormatInfo { get; set; }
        string FormatWarn { get; set; }
        string FormatErr { get; set; }
        string FormatEx { get; set; }

        void V(string tag , string verb);
        void I(string tag, string info);
        void W(string tag, string warn);
        void E(string tag, string err);
        void E(string tag, string msg, Exception ex);
    }

默认实现

  public class DefaultLogger : ILogger
    {
        public Loglevels Level { get; set; }
        public string FormatVerbose { get; set; }
        public string FormatInfo { get; set; }
        public string FormatWarn { get; set; }
        public string FormatErr { get; set; }
        public string FormatEx { get; set; }

        public DefaultLogger()
        {
            FormatVerbose = "V [{0}]: {1}";
            FormatInfo = "I [{0}]: {1}";
            FormatWarn = "W [{0}]: {1}";
            FormatErr = "Err [{0}]: {1}";
            FormatEx = "Ex [{0}]: {1} - Message: {2}  StackTrace: {3}";

            Level = UnityEngine.Debug.isDebugBuild ? Loglevels.Warning : Loglevels.Error;
        }

        public void V(string division, string verb)
        {
            if (Level <= Loglevels.All)
            {
                try
                {
                    UnityEngine.Debug.Log(string.Format(FormatVerbose, division, verb));
                }
                catch
                { }
            }
        }

        public void I(string division, string info)
        {
            if (Level <= Loglevels.Information)
            {
                try
                {
                    UnityEngine.Debug.Log(string.Format(FormatInfo, division, info));
                }
                catch
                { }
            }
        }

        public void W(string division, string warn)
        {
            if (Level <= Loglevels.Warning)
            {
                try
                {
                    UnityEngine.Debug.LogWarning(string.Format(FormatWarn, division, warn));
                }
                catch
                { }
            }
        }

        public void E(string division, string err)
        {
            if (Level <= Loglevels.Error)
            {
                try
                {
                    UnityEngine.Debug.LogError(string.Format(FormatErr, division, err));
                }
                catch
                { }
            }
        }

        public void E(string division, string msg, Exception ex)
        {
            if (Level <= Loglevels.Exception)
            {
                try
                {
                    string exceptionMessage = string.Empty;
                    if (ex == null)
                        exceptionMessage = "null";
                    else
                    {
                        StringBuilder sb = new StringBuilder();

                        Exception exception = ex;
                        int counter = 1;
                        while (exception != null)
                        {
                            sb.AppendFormat("{0}: {1} {2}", counter++.ToString(), ex.Message, ex.StackTrace);

                            exception = exception.InnerException;

                            if (exception != null)
                                sb.AppendLine();
                        }

                        exceptionMessage = sb.ToString();
                    }

                    UnityEngine.Debug.LogError(string.Format(FormatEx,
                                                                division,
                                                                msg,
                                                                exceptionMessage,
                                                                ex != null ? ex.StackTrace : "null"));
                }
                catch
                { }
            }
        }
    }

*** 使用 ***

    void Start()
    {
        logger = new DefaultLogger();
        logger.Level = Loglevels.All;
        logger.I("Tag", "Test Info");
        logger.W("Tag", "Test Warning");
        logger.E("Tag", "Test Error");
    }

编译成dll


在终端中输入以下代码 可以将.cs文件编译为.dll , 最后是需要打包编译的.cs文件的完整路径,用空格分开
执行完毕后,会在平级路径下生成一个dll文件 这时把.cs文件删除即可

mcs -r:/Applications/Unity/Unity.app/Contents/Managed/UnityEngine.dll -target:library /Users/mopel/UnityWorkZone/UITheme/Assets/PinLogger/DefaultLogger.cs /Users/mopel/UnityWorkZone/UITheme/Assets/PinLogger/ILogger.cs -sdk:2

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 在应用程序中添加日志记录总的来说基于三个目的:监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析...
    时待吾阅读 5,068评论 0 6
  • 在应用程序中添加日志记录总的来说基于三个目的:监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析...
    时待吾阅读 5,126评论 1 13
  • 在洁白的毡房里 痛饮马奶酒 醉倒了 便躺在草原温柔的裙裾上 神游天地 做一个清白的梦
    T悦兒阅读 160评论 0 0
  • 青盦阅读 762评论 0 1