Unity自己有log系统,为什么要自己封装一个
1.不好用,只能在pc上记录log文件,移动平台是没有的
2.在开发时期的log,不想在正式版里面出现。没有一个统一的开关来控制是不是要显示log,要显示什么类型的log
那么怎么做?
我的思路是:Unity里面的所有的log,都会被 Application.logMessageReceived 捕获
只要添加一个回调来把这些log信息记录下来保存就好了
Application.logMessageReceived += ProcessExceptionReport;
代码1:log等级枚举,被用来控制是否显示log,显示哪些类型的log
namespace ShiHuanJue.Debuger { /// <summary> /// log等级 /// </summary> public enum LogLevel { None = 0, Debug = 1, Error = 2, Warning = 4, Exception = 8, All = LogLevel.Debug | LogLevel.Error | LogLevel.Warning | LogLevel.Exception } }
代码2:把log记录到文本里
using UnityEngine; using System.Collections; using System.IO; using System; using System.Text; namespace ShiHuanJue.Debuger { public class LogWriter { private string m_logPath = Application.persistentDataPath + "/log/"; private string m_logFileName = "log_{0}.txt"; private string m_logFilePath = string.Empty; public LogWriter() { if (!Directory.Exists(m_logPath)) { Directory.CreateDirectory(m_logPath); } this.m_logFilePath = this.m_logPath + string.Format(this.m_logFileName, DateTime.Today.ToString("yyyyMMdd")); } public void ExcuteWrite(string content) { using (StreamWriter writer = new StreamWriter(m_logFilePath, true, Encoding.UTF8)) { writer.WriteLine(content); } } } }
代码3:log帮助类
using UnityEngine; using System; namespace ShiHuanJue.Debuger { public class LogHelper { static public LogLevel m_logLevel = LogLevel.All; static LogWriter m_logWriter = new LogWriter(); static LogHelper() { Application.logMessageReceived += ProcessExceptionReport; } private static void ProcessExceptionReport(string message, string stackTrace, LogType type) { LogLevel dEBUG = LogLevel.Debug; switch (type) { case LogType.Error: dEBUG = LogLevel.Error; break; case LogType.Assert: dEBUG = LogLevel.Debug; break; case LogType.Warning: dEBUG = LogLevel.Warning; break; case LogType.Log: dEBUG = LogLevel.Debug; break; case LogType.Exception: dEBUG = LogLevel.Exception; break; } if (dEBUG == (m_logLevel & dEBUG)) { Log(string.Concat(new object[] { " [", dEBUG, "]: ", message, ' ', stackTrace })); } } /// <summary> /// 加上时间戳 /// </summary> /// <param name="message"></param> private static void Log(string message) { string msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff") + message; m_logWriter.ExcuteWrite(msg); } static public void Log(object message) { Log(message, null); } static public void Log(object message, UnityEngine.Object context) { if (LogLevel.Debug == (m_logLevel & LogLevel.Debug)) { Debug.Log(message, context); } } static public void LogError(object message) { LogError(message, null); } static public void LogError(object message, UnityEngine.Object context) { if (LogLevel.Error == (m_logLevel & LogLevel.Error)) { Debug.LogError(message, context); } } static public void LogWarning(object message) { LogWarning(message, null); } static public void LogWarning(object message, UnityEngine.Object context) { if (LogLevel.Warning == (m_logLevel & LogLevel.Warning)) { Debug.LogWarning(message, context); } } } }
游戏上线之前把LogHelper.m_logLevel = LogLevel.None;Log就不显示了。
LogHelper.m_logLevel = LogLevel.Error;是显示Error。
LogHelper.m_logLevel = LogLevel.Debug | LogLevel.Error;同时显示Debug和Error。
log文件是保存在 private string m_logPath = Application.persistentDataPath + “/log/”;在手机上就是沙盒路径。
log文件是以天为单位,同一天的log会被记录到一个文件里面。
怎么用呢?
using UnityEngine; using ShiHuanJue.Debuger; public class Test : MonoBehaviour { void Start() { LogHelper.m_logLevel = LogLevel.All; LogHelper.Log("debug"); LogHelper.LogError("error"); LogHelper.LogWarning("warning"); GameObject go = GameObject.Find("fsdfsd"); GameObject.Instantiate(go); } }
这里测试了几种典型的情况,包括找不到go,实例化的时候系统报的异常
有图有真相~!
查看一下log文件
最后我封装了一个dll,直接放到工程下头就可以用了。
大家也可以在这里下载:链接: http://pan.baidu.com/s/1qWFtQOG 密码: erpx
代码都在上面,你们也可以去生成。
- 本文固定链接: http://www.shihuanjue.com/?p=69
- 转载请注明: 乔 2015年09月04日 于 是幻觉 发表