Unity 自定义Logger

前言

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

统一管理Log显示级别

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

日志显示级别定义

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

        /// 
        /// Only Informations and above will be logged.
        /// 
        Information,

        /// 
        /// Only Warnings and above will be logged.
        /// 
        Warning,

        /// 
        /// Only Errors and above will be logged.
        /// 
        Error,

        /// 
        /// Only Exceptions will be logged.
        /// 
        Exception,

        /// 
        /// No logging will be occur.
        /// 
        None
    }

日志显示接口

  public interface ILogger
    {
        /// 
        /// The minimum severity to log
        /// 
        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

你可能感兴趣的:(Unity 自定义Logger)