詳解Unity日志打印工具功能
一、日志工具功能
封裝Debug類(lèi),需要實(shí)現(xiàn)功能:
1.控制所有日志是否打??;
2.除了Log,Warning,Error外,給更多日志種類(lèi)(不同顏色);
3.格式化打印日志;
4.不定參數(shù),自動(dòng)拼接成字符串;
5.上傳日志到服務(wù)器;
二、Logger類(lèi)
1.控制日志打印
封裝Debug中關(guān)于Log的方法;
使用靜態(tài)方法,聲明靜態(tài)字段,控制log,warning,error是否打印;

Debug源碼中Log方法有兩個(gè)重載;

第二個(gè)參數(shù)context可以傳參GameObject,Hierarchy或者Project窗口中的預(yù)制體,雙擊Console日志會(huì)直接跳轉(zhuǎn)選中傳入的游戲物體;
我們可以將這個(gè)方法合并,第二個(gè)參數(shù)默認(rèn)空;

2.色彩打印
打印不同顏色使用富文本;
string.Format("<color={0}>{1}</color>",color,obj);
3.多參數(shù)拼接
之前使用Go語(yǔ)言的fmt.Println有個(gè)功能很好用,連續(xù)傳多個(gè)參數(shù)自動(dòng)拼接;
public static void Log(params object[] messags)
{
if (!s_debugLogEnable) return;
string message = string.Empty;
foreach (var it in messags)
{
message += it.ToString();
}
Debug.Log(message, null);
}
//調(diào)用
Logger.Log("Net error:",error,"msgId:",msgId);4.格式化打印
格式化打印封裝原本Debug.LogFormat方法;
public static void LogFormat(string format, params object[] args)
{
if (!s_debugLogEnable) return;
Debug.LogFormat(format, args);
}三、LoggerMgr類(lèi)
繼承MonoBehavior的單例;
初始化Logger中的三個(gè)控制打印的字段;
Application類(lèi)中有收到日志消息觸發(fā)的事件LogMessageReceived;
監(jiān)聽(tīng)這個(gè)事件;如果日志開(kāi)關(guān)為關(guān)閉狀態(tài)return;
if (isOpenLog)
{
Logger.s_debugLogEnable = true;
Logger.s_warningLogEnable = true;
Logger.s_errorLogEnable = true;
}
Application.logMessageReceived += (string condition, string stackTrace, LogType type) =>
{
switch (type)
{
case LogType.Log:
{
if (!Logger.s_debugLogEnable) return;
}
break;
case LogType.Warning:
{
if (!Logger.s_warningLogEnable) return;
}
break;
case LogType.Error:
{
if (!Logger.s_errorLogEnable) return;
}
break;
}
};四、上傳日志
LoggerMgr中初始化上傳日志信息;
方法寫(xiě)在logger中,在LoggerMgr開(kāi)始調(diào)用;
public static void Init(string url)
{
LogUploader.SetUploadUrl(url);
// 日期
var t = System.DateTime.Now.ToString("yyyyMMddhhmmss");
s_logFileSavePath = string.Format("{0}/output_{1}.log", Application.persistentDataPath, t);
Application.logMessageReceived += OnLogCallBack;
}OnLogCallBack方法中將日志和棧信息存儲(chǔ)成文件,等待上傳;
private static void OnLogCallBack(string condition, string stackTrace, LogType type)
{
s_logStr.Append(condition);
s_logStr.Append("\n");
s_logStr.Append(stackTrace);
s_logStr.Append("\n");
if (s_logStr.Length <= 0) return;
if (!File.Exists(s_logFileSavePath))
{
var fs = File.Create(s_logFileSavePath);
fs.Close();
}
using (var sw = File.AppendText(s_logFileSavePath))
{
sw.WriteLine(s_logStr.ToString());
}
s_logStr.Remove(0, s_logStr.Length);
}LogUploader類(lèi)
開(kāi)啟協(xié)程上傳日志文件;
public static void StartUploadLog(string logFilePath, string desc)
{
if (LOG_UPLOAD_URL == string.Empty)
return;
var go = new GameObject("LogUploader");
var bhv = go.AddComponent<LogUploader>();
bhv.StartCoroutine(bhv.UploadLog(logFilePath, LOG_UPLOAD_URL, desc));
}在Logger類(lèi)中同樣封裝上面的方法,所有的日志都通過(guò)Logger打?。?/p>
public static void UploadLog(string desc)
{
LogUploader.StartUploadLog(s_logFileSavePath, desc);
}五、日志雙擊溯源問(wèn)題
以上的代碼有個(gè)很大的問(wèn)題,現(xiàn)在我們雙擊不會(huì)回到調(diào)用Logger的地方,只會(huì)跳轉(zhuǎn)到Logger類(lèi)中調(diào)用Debug.Log的地方;
有個(gè)很簡(jiǎn)單的辦法解決,將上面代碼編譯成dll;
另外查找源碼也可以自己決定掉轉(zhuǎn)位置;
具體方法,通過(guò)反射獲日志取棧信息,根據(jù)Logger類(lèi)返回的棧信息路徑,篩選出要跳轉(zhuǎn)的位置;
通過(guò)官方提供的方法跳轉(zhuǎn)到相應(yīng)位置;
UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(string filename, int line);
六、調(diào)用
void Start()
{
Logger.Log("aaaa");
Logger.LogFormat("{0}===={1}",111,0.232);
Logger.LogGreen("bbbb");
Logger.LogError("log error");
Logger.Log("aa", 13, "fff",16,"sfddf",64654);
Logger.UploadLog("NetWork LogTest");
}
到此這篇關(guān)于Unity日志打印工具的文章就介紹到這了,更多相關(guān)Unity日志打印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
unity實(shí)現(xiàn)鼠標(biāo)跟隨(ITween)
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)鼠標(biāo)跟隨,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
WinForm項(xiàng)目開(kāi)發(fā)中Excel用法實(shí)例解析
這篇文章主要介紹了WinForm項(xiàng)目開(kāi)發(fā)中Excel用法,非常實(shí)用,需要的朋友可以參考下2014-08-08
unity實(shí)現(xiàn)簡(jiǎn)單抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)簡(jiǎn)單抽獎(jiǎng)系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
C#中隱藏TabControl選項(xiàng)卡標(biāo)簽的解決方案
這篇文章主要介紹了C#中隱藏TabControl選項(xiàng)卡標(biāo)簽的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
C#使用Selenium+PhantomJS抓取數(shù)據(jù)
本文主要介紹了C#使用Selenium+PhantomJS抓取數(shù)據(jù)的方法步驟,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02

