Unity 日志管理

以前实习的时候写的日志系统是在Debug的事件中加一个监听,把收到的日志保存到文件,这个很多博客都有写,因为大佬要求有行信息,就是 xxxxx cs.168显示一个Log的行号。然而在ReleaseBuild里面是没有行号的,别想了。。只有Development Build日志才有行号。

后来发现行号不是必要的,只要知道哪个函数发出的就好,而且在Debug添加监听有很多局限性,不能修改控制台的Log信息,这时候想到封装Debug,但是封装Debug在控制台双击Log会直接跳到封装语句里面的Log,而不是调用Debuger的地方,这个自己没试过说也说不清楚。但是有解决方法,打包成dll文件,就不会出现上面这种情况了,如果你看不懂,自己封装一次Debug就知道了。附上代码。

public static class Debuger
{
    public static bool EnableLog;
    public static bool EnableTime;

    static Debuger()
    {
    }

    [Conditional("DEBUG")]
    public static void Log(string tag, string message)
    {
        if (EnableLog)
        {
            if (EnableTime)
            {
                Debug.LogFormat("{0} {1}::{2}", DateTime.Now.ToString("HH:mm:ss:fff"), tag, message);
            }
            else
            {
                Debug.LogFormat("{0}::{1}", tag, message);
            }
        }
    }

    [Conditional("DEBUG")]
    public static void Log(string tag, string format, params object[] args)
    {
        if (EnableLog)
        {
            string info = tag + "::" + format;
            if (EnableTime)
            {
                info = DateTime.Now.ToString("HH:mm:ss:fff") + " " + info;
            }
            Debug.LogFormat(info, args);
        }
    }

    [Conditional("DEBUG")]
    public static void LogError(string tag, string message)
    {
        if (EnableLog)
        {
            if (EnableTime)
            {
                Debug.LogErrorFormat("{0} {1}::{2}", DateTime.Now.ToString("HH:mm:ss:fff"), tag, message);
            }
            else
            {
                Debug.LogErrorFormat("{0}::{1}", tag, message);
            }
        }
    }

    [Conditional("DEBUG")]
    public static void LogError(string tag, string format, params object[] args)
    {
        if (EnableLog)
        {
            string info = tag + "::" + format;
            if (EnableTime)
            {
                info = DateTime.Now.ToString("HH:mm:ss:fff") + " " + info;
            }
            Debug.LogErrorFormat(info, args);
        }
    }

    [Conditional("DEBUG")]
    public static void LogWarning(string tag, string message)
    {
        if (EnableLog)
        {
            if (EnableTime)
            {
                Debug.LogWarningFormat("{0} {1}::{2}", DateTime.Now.ToString("HH:mm:ss:fff"), tag, message);
            }
            else
            {
                Debug.LogWarningFormat("{0}::{1}", tag, message);
            }
        }
    }

    [Conditional("DEBUG")]
    public static void LogWarning(string tag, string format, params object[] args)
    {
        if (EnableLog)
        {
            string info = tag + "::" + format;
            if (EnableTime)
            {
                info = DateTime.Now.ToString("HH:mm:ss:fff") + " " + info;
            }
            Debug.LogWarningFormat(info, args);
        }
    }

}


猜你喜欢

转载自blog.csdn.net/qq_30858573/article/details/79411742