- 最后登录
- 2017-6-30
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 10267
  
- 纳金币
- 6520
- 精华
- 14
|
已知public static ILogger logger(Debug会自己Get default debug logger给它赋值)
又logger Implements interfaces: ILogger, ILogHandle(Initializes a new instance of the Logger)
其中ILogger Implements interfaces: ILogHandle(并且ILogger 包含一个ILogHandle属性)
可推测在执行类似Debug.Log的Api时会收到消息事件执行logger的ILogHandle,在ILogHandle中往下执行ILogger。
我们可以自定义继承ILogHandle接口的类替换logger的ILogHandle从而达到对日志的加工处理(如日志存档,发送等)
截取消息事件也可用Application中的委托处理。
官方ILogHandle示列(Interface for custom log handler implementation)- using UnityEngine;
- using System.Collections;
- using System.IO;
- using System;
- public class MyFileLogHandler : ILogHandler
- {
- private FileStream m_FileStream;
- private StreamWriter m_StreamWriter;
- private ILogHandler m_DefaultLogHandler = Debug.logger.logHandler;
- public MyFileLogHandler()
- {
- string filePath = Application.persistentDataPath + "/MyLogs.txt";
- m_FileStream = new FileStream (filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
- m_StreamWriter = new StreamWriter (m_FileStream);
- // Replace the default debug log handler
- Debug.logger.logHandler = this;
- }
- public void LogFormat (LogType logType, UnityEngine.Object context, string format, params object[] args)
- {
- m_StreamWriter.WriteLine ( String.Format (format, args) );
- m_StreamWriter.Flush ();
- m_DefaultLogHandler.LogFormat (logType, context, format, args);
- }
- public void LogException (Exception exception, UnityEngine.Object context)
- {
- m_DefaultLogHandler.LogException (exception, context);
- }
- }
- public class MyGameClass : MonoBehaviour {
- private static ILogger logger = Debug.logger;
- private static string kTAG = "MyGameTag";
- private MyFileLogHandler myFileLogHandler;
- void Start() {
- myFileLogHandler = new MyFileLogHandler();
- logger.Log(kTAG, "MyGameClass Start.");
- }
- }
复制代码 |
|